index.js 4.7 KB

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