index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. import {
  13. getOpenidNoLogin
  14. } from '~/utils/httpUtilNoLogin';
  15. const app = getApp()
  16. Page({
  17. data: {
  18. userInfo: {},
  19. vipTime: '',
  20. isIos: false,
  21. tasks: [],
  22. // isIos: app.globalData.isIOS,
  23. productNum: {},
  24. productVip: {}
  25. },
  26. onLoad() {
  27. this.getProducts()
  28. },
  29. async onShow() {
  30. let uid = wx.getStorageSync('uid') || ''
  31. if (!uid) {
  32. getOpenidNoLogin(async() => {
  33. this.setUserInfo()
  34. })
  35. } else {
  36. this.setUserInfo()
  37. }
  38. },
  39. // 设置用户信息及vip状态和任务完成情况
  40. async setUserInfo() {
  41. let userInfo = await getUserInfo()
  42. let vipTime = await getVipInfo()
  43. this.getTasks()
  44. this.setData({
  45. userInfo,
  46. vipTime,
  47. })
  48. },
  49. async getTasks() {
  50. let tasks = await getTasks()
  51. this.setData({
  52. tasks
  53. })
  54. },
  55. async getProducts() {
  56. let products = await getProducts()
  57. let productVip = products.find(item => {
  58. return item.type == 1
  59. })
  60. let productNum = products.find(item => {
  61. return item.type == 2
  62. })
  63. this.setData({
  64. productNum,
  65. productVip
  66. })
  67. },
  68. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  69. async toBuy({
  70. currentTarget
  71. }) {
  72. let productId = currentTarget.dataset.type
  73. wx.showLoading({
  74. title: '提交中',
  75. mask: true
  76. })
  77. let res = ''
  78. if (productId == '1001') {
  79. res = await buyVip({
  80. productId
  81. }).finally(() => {
  82. wx.hideLoading()
  83. })
  84. } else if (productId == '1010') {
  85. res = await buyNum({
  86. productId
  87. }).finally(() => {
  88. wx.hideLoading()
  89. })
  90. } else {
  91. wx.hideLoading()
  92. wx.showToast({
  93. title: "支付失败,请重试",
  94. icon: "none"
  95. })
  96. }
  97. let {
  98. timeStamp,
  99. nonceStr,
  100. signType,
  101. paySign
  102. } = res
  103. // package保留字
  104. wx.requestPayment({
  105. timeStamp,
  106. nonceStr,
  107. package: res.package,
  108. signType,
  109. paySign,
  110. success(res) {
  111. wx.showToast({
  112. title: "支付成功",
  113. duration: 2500
  114. })
  115. setTimeout(() => {
  116. this.setUserInfo()
  117. }, 1500)
  118. },
  119. fail(res) {
  120. wx.showToast({
  121. title: "支付失败",
  122. icon: "none"
  123. })
  124. }
  125. })
  126. },
  127. // 提交任务
  128. async submitTask({
  129. currentTarget
  130. }) {
  131. let id = currentTarget.dataset.type
  132. await submitTask({
  133. id
  134. })
  135. wx.showToast({
  136. title: id == '1' ? '签到成功!' : id == 3 ? "观看成功!" : "",
  137. icon: "none"
  138. })
  139. this.setUserInfo()
  140. },
  141. jump({
  142. currentTarget
  143. }) {
  144. let url = currentTarget.dataset.url
  145. wx.navigateTo({
  146. url: url
  147. });
  148. },
  149. switcher({
  150. currentTarget
  151. }) {
  152. wx.navigateTo({
  153. url: `/pages/index/index?tabbarIndx=${currentTarget.dataset.index}`
  154. });
  155. },
  156. // 调起广告
  157. rewardedVideo() {
  158. if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
  159. return
  160. }
  161. this.selectComponent('#advert').rewardedVideo();
  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. })