index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. getMyInfo,
  3. getVipInfo
  4. } from '~/api/user'
  5. import {
  6. getProducts,
  7. getTasks,
  8. buyVip,
  9. buyNum,
  10. submitTask
  11. } from '~/api/global'
  12. const app = getApp()
  13. Page({
  14. data: {
  15. userInfo: {},
  16. vipTime: '',
  17. tasks: [],
  18. isIos: app.globalData.isIOS,
  19. productNum: {},
  20. productVip: {}
  21. },
  22. onLoad() {
  23. this.getProducts()
  24. },
  25. async onShow() {
  26. if (typeof this.getTabBar === 'function') {
  27. this.getTabBar().setData({
  28. selected: 0
  29. })
  30. }
  31. let uid = wx.getStorageSync('uid') || ''
  32. if (!uid) {
  33. getApp().callBack = (res) => {
  34. this.setUserInfo()
  35. }
  36. } else {
  37. this.setUserInfo()
  38. }
  39. },
  40. // 设置用户信息及vip状态和任务完成情况
  41. async setUserInfo() {
  42. let userInfo = await getMyInfo()
  43. let vipTime = await getVipInfo()
  44. this.getTasks()
  45. this.setData({
  46. userInfo,
  47. vipTime,
  48. })
  49. },
  50. async getTasks() {
  51. let tasks = await getTasks()
  52. this.setData({
  53. tasks
  54. })
  55. },
  56. async getProducts() {
  57. let products = await getProducts()
  58. let productVip = products.find(item => {
  59. return item.type == 1
  60. })
  61. let productNum = products.find(item => {
  62. return item.type == 2
  63. })
  64. this.setData({
  65. productNum,
  66. productVip
  67. })
  68. },
  69. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  70. async toBuy({
  71. currentTarget
  72. }) {
  73. let productId = currentTarget.dataset.type
  74. wx.showLoading({
  75. title: '提交中',
  76. mask: true
  77. })
  78. let res = ''
  79. if (productId == '1001') {
  80. res = await buyVip({
  81. productId
  82. }).finally(() => {
  83. wx.hideLoading()
  84. })
  85. } else if (productId == '1010') {
  86. res = await buyNum({
  87. productId
  88. }).finally(() => {
  89. wx.hideLoading()
  90. })
  91. } else {
  92. wx.hideLoading()
  93. wx.showToast({
  94. title: "支付失败,请重试",
  95. icon: "none"
  96. })
  97. }
  98. let {
  99. timeStamp,
  100. nonceStr,
  101. signType,
  102. paySign
  103. } = res
  104. // package保留字
  105. wx.requestPayment({
  106. timeStamp,
  107. nonceStr,
  108. package: res.package,
  109. signType,
  110. paySign,
  111. success(res) {
  112. wx.showToast({
  113. title: "支付成功",
  114. duration: 2500
  115. })
  116. setTimeout(() => {
  117. this.setUserInfo()
  118. }, 1500)
  119. },
  120. fail(res) {
  121. wx.showToast({
  122. title: "支付失败",
  123. icon: "none"
  124. })
  125. }
  126. })
  127. },
  128. // 提交任务
  129. async submitTask({
  130. currentTarget
  131. }) {
  132. let id = currentTarget.dataset.type
  133. await submitTask({
  134. id
  135. })
  136. wx.showToast({
  137. title: id == '1' ? '签到成功!' : id == 3 ? "观看成功!" : "",
  138. icon: "none"
  139. })
  140. this.setUserInfo()
  141. },
  142. jump({
  143. currentTarget
  144. }) {
  145. let url = currentTarget.dataset.url
  146. wx.navigateTo({
  147. url: url
  148. });
  149. },
  150. // 调起广告
  151. rewardedVideo() {
  152. if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
  153. return
  154. }
  155. this.selectComponent('#advert').rewardedVideo();
  156. },
  157. clipboar() {
  158. wx.setClipboardData({
  159. data: this.data.userInfo.user.eid,
  160. success: function (res) { //成功回调函数
  161. wx.showToast({
  162. title: '已复制',
  163. icon: "none"
  164. })
  165. }
  166. })
  167. },
  168. // 分享配置
  169. onShareAppMessage: function (res) {
  170. const user = wx.getStorageSync('user');
  171. return {
  172. title: '课文朗读,从未如此有趣。',
  173. path: `/pages/index/index?uid=${user.uid}`,
  174. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  175. }
  176. },
  177. onShareTimeline: function () {
  178. return {
  179. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  180. query: `uid=${wx.getStorageSync('uid')}`,
  181. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  182. }
  183. },
  184. })