index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {
  2. getProducts,
  3. getTasks,
  4. } from '~/api/global'
  5. import {
  6. buyVip,
  7. buyNum,
  8. getMyInfo,
  9. getVipInfo,
  10. } from '~/api/user'
  11. import {
  12. createStoreBindings
  13. } from 'mobx-miniprogram-bindings'
  14. import {
  15. store
  16. } from '~/store/index'
  17. const app = getApp()
  18. Page({
  19. data: {
  20. userInfo: {},
  21. vipTime: '',
  22. tasks: [],
  23. isIos: app.globalData.isIOS,
  24. productNum: {},
  25. productVip: {}
  26. },
  27. onLoad() {
  28. this.getProducts()
  29. // 手工绑定
  30. this.storeBindings = createStoreBindings(this, {
  31. store,
  32. actions: {
  33. setUser: 'setUser'
  34. }
  35. })
  36. },
  37. async onShow() {
  38. if (typeof this.getTabBar === 'function') {
  39. this.getTabBar().setData({
  40. selected: 4
  41. })
  42. }
  43. let uid = wx.getStorageSync('uid') || ''
  44. if (!uid) {
  45. getApp().callBack = (res) => {
  46. this.setUserInfo()
  47. }
  48. } else {
  49. this.setUserInfo()
  50. }
  51. },
  52. // 设置用户信息及vip状态和任务完成情况
  53. async setUserInfo() {
  54. let userInfo = await getMyInfo()
  55. let vipTime = await getVipInfo()
  56. this.setUser(userInfo.user)
  57. this.getTasks()
  58. this.setData({
  59. userInfo,
  60. vipTime,
  61. })
  62. },
  63. async getTasks() {
  64. let tasks = await getTasks()
  65. this.setData({
  66. tasks
  67. })
  68. },
  69. async getProducts() {
  70. let products = await getProducts()
  71. let productVip = products.find(item => {
  72. return item.type == 1
  73. })
  74. let productNum = products.find(item => {
  75. return item.type == 2
  76. })
  77. this.setData({
  78. productNum,
  79. productVip
  80. })
  81. },
  82. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  83. async toBuy({
  84. currentTarget
  85. }) {
  86. let productId = currentTarget.dataset.type
  87. wx.showLoading({
  88. title: '提交中',
  89. mask: true
  90. })
  91. let res = ''
  92. if (productId == '1001') {
  93. res = await buyVip({
  94. productId
  95. }).finally(() => {
  96. wx.hideLoading()
  97. })
  98. } else if (productId == '1010') {
  99. res = await buyNum({
  100. productId
  101. }).finally(() => {
  102. wx.hideLoading()
  103. })
  104. } else {
  105. wx.hideLoading()
  106. wx.showToast({
  107. title: "支付失败,请重试",
  108. icon: "none",
  109. duration: 3000
  110. })
  111. }
  112. let {
  113. timeStamp,
  114. nonceStr,
  115. signType,
  116. paySign
  117. } = res
  118. // package保留字
  119. wx.requestPayment({
  120. timeStamp,
  121. nonceStr,
  122. package: res.package,
  123. signType,
  124. paySign,
  125. success(res) {
  126. wx.showToast({
  127. title: "支付成功",
  128. duration: 2500
  129. })
  130. setTimeout(() => {
  131. this.setUserInfo()
  132. }, 1500)
  133. },
  134. fail(res) {
  135. wx.showToast({
  136. title: "支付失败",
  137. icon: "none",
  138. duration: 3000
  139. })
  140. }
  141. })
  142. },
  143. jump({
  144. currentTarget
  145. }) {
  146. let url = currentTarget.dataset.url
  147. wx.navigateTo({
  148. url: url
  149. });
  150. },
  151. clipboar() {
  152. wx.setClipboardData({
  153. data: this.data.userInfo.user.eid,
  154. success: function (res) { //成功回调函数
  155. wx.showToast({
  156. title: '已复制',
  157. icon: "none"
  158. })
  159. }
  160. })
  161. },
  162. // 分享配置
  163. onShareAppMessage: function (res) {
  164. return {
  165. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  166. path: '/pages/index/index',
  167. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  168. }
  169. },
  170. onShareTimeline: function () {
  171. return {
  172. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  173. query: `uid=${wx.getStorageSync('uid')}`,
  174. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  175. }
  176. },
  177. })