index.js 3.8 KB

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