index.js 3.7 KB

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