index.js 4.9 KB

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