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