index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. activationModal: false
  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.setData({
  58. userInfo,
  59. vipTime,
  60. })
  61. },
  62. async getProducts() {
  63. let products = await getProducts()
  64. let productVip = products.find(item => {
  65. return item.type == 1
  66. })
  67. let productNum = products.find(item => {
  68. return item.type == 2
  69. })
  70. this.setData({
  71. productNum,
  72. productVip
  73. })
  74. },
  75. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  76. async toBuy({
  77. currentTarget
  78. }) {
  79. let productId = currentTarget.dataset.type
  80. wx.showLoading({
  81. title: '提交中',
  82. mask: true
  83. })
  84. let res = ''
  85. if (productId == '1001') {
  86. res = await buyVip({
  87. productId
  88. }).finally(() => {
  89. wx.hideLoading()
  90. })
  91. } else if (productId == '1010') {
  92. res = await buyNum({
  93. productId
  94. }).finally(() => {
  95. wx.hideLoading()
  96. })
  97. } else {
  98. wx.hideLoading()
  99. wx.showToast({
  100. title: "支付失败,请重试",
  101. icon: "none",
  102. duration: 3000
  103. })
  104. }
  105. let {
  106. timeStamp,
  107. nonceStr,
  108. signType,
  109. paySign
  110. } = res
  111. // package保留字
  112. wx.requestPayment({
  113. timeStamp,
  114. nonceStr,
  115. package: res.package,
  116. signType,
  117. paySign,
  118. success(res) {
  119. wx.showToast({
  120. title: "支付成功",
  121. duration: 2500
  122. })
  123. setTimeout(() => {
  124. this.setUserInfo()
  125. }, 1500)
  126. },
  127. fail(res) {
  128. wx.showToast({
  129. title: "支付失败",
  130. icon: "none",
  131. duration: 3000
  132. })
  133. }
  134. })
  135. },
  136. activationCode() {
  137. wx.showModal({
  138. title: '请输入激活码',
  139. editable: true,
  140. success: () => {
  141. this.setData({
  142. activationModal: true
  143. })
  144. }
  145. })
  146. },
  147. jump({
  148. currentTarget
  149. }) {
  150. let url = currentTarget.dataset.url
  151. wx.navigateTo({
  152. url: url
  153. });
  154. },
  155. clipboar() {
  156. wx.setClipboardData({
  157. data: this.data.userInfo.user.eid,
  158. success: function (res) { //成功回调函数
  159. wx.showToast({
  160. title: '已复制',
  161. icon: "none"
  162. })
  163. }
  164. })
  165. },
  166. closeModal() {
  167. this.setData({
  168. activationModal: false
  169. })
  170. },
  171. // 分享配置
  172. onShareAppMessage: function (res) {
  173. return {
  174. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  175. path: '/pages/index/index',
  176. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  177. }
  178. },
  179. onShareTimeline: function () {
  180. return {
  181. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  182. query: `uid=${wx.getStorageSync('uid')}`,
  183. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  184. }
  185. },
  186. })