index.js 3.7 KB

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