index.js 4.8 KB

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