index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. getMyInfo,
  3. buyVip,
  4. getVipInfo,
  5. getLearnCard
  6. } from '~/api/user'
  7. import {
  8. getProducts
  9. } from '~/api/global'
  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. activationModal: false,
  24. activationRes: {},
  25. products: [],
  26. isPreferential: false
  27. },
  28. onLoad() {
  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. this.selectComponent('#vipModal').open()
  52. },
  53. // 设置用户信息及vip状态
  54. async setUserInfo() {
  55. let userInfo = await getMyInfo()
  56. let vipTime = await getVipInfo()
  57. this.setUser(userInfo.user)
  58. this.getProducts()
  59. this.setData({
  60. userInfo,
  61. vipTime,
  62. })
  63. },
  64. async getProducts() {
  65. let {
  66. productList: products,
  67. isPreferential
  68. } = await getProducts()
  69. console.log(products, isPreferential);
  70. this.setData({
  71. products,
  72. isPreferential
  73. })
  74. },
  75. activationCode() {
  76. wx.showModal({
  77. title: '请输入激活码',
  78. editable: true,
  79. success: async ({
  80. confirm,
  81. content
  82. }) => {
  83. if (confirm) {
  84. let regexp = /^[a-zA-Z0-9]{4}$/
  85. if (regexp.test(content)) {
  86. let activationRes = await getLearnCard({
  87. cardNo: content
  88. })
  89. if (activationRes.code == 200) {
  90. activationRes = {
  91. code: 200,
  92. message: '快去朗读挑战吧!'
  93. }
  94. }
  95. this.setUserInfo()
  96. if (typeof this.getTabBar === 'function') {
  97. this.getTabBar().setData({
  98. mask: true
  99. })
  100. }
  101. this.setData({
  102. activationModal: true,
  103. activationRes
  104. })
  105. } else {
  106. if (typeof this.getTabBar === 'function') {
  107. this.getTabBar().setData({
  108. mask: true
  109. })
  110. }
  111. this.setData({
  112. activationModal: true,
  113. activationRes: {
  114. code: 581,
  115. message: '请检查激活码输入是否正确'
  116. }
  117. })
  118. }
  119. }
  120. }
  121. })
  122. },
  123. async toBuy({
  124. currentTarget
  125. }) {
  126. if (currentTarget.dataset.isclick) {
  127. return
  128. }
  129. wx.showLoading({
  130. title: '提交中',
  131. mask: true
  132. })
  133. let res = await buyVip({
  134. productId: currentTarget.dataset.id
  135. })
  136. let {
  137. timeStamp,
  138. nonceStr,
  139. signType,
  140. paySign
  141. } = res
  142. // package保留字
  143. wx.requestPayment({
  144. timeStamp,
  145. nonceStr,
  146. package: res.package,
  147. signType,
  148. paySign,
  149. success: (res) => {
  150. setTimeout(() => {
  151. this.setUserInfo()
  152. this.selectComponent('#vipModal').open()
  153. }, 1500)
  154. },
  155. fail(res) {
  156. wx.showToast({
  157. title: "支付失败",
  158. icon: "none",
  159. duration: 3000
  160. })
  161. },
  162. complete: () => {
  163. wx.hideLoading()
  164. }
  165. })
  166. },
  167. jump({
  168. currentTarget
  169. }) {
  170. let url = currentTarget.dataset.url
  171. wx.navigateTo({
  172. url: url
  173. });
  174. },
  175. clipboar() {
  176. wx.setClipboardData({
  177. data: this.data.userInfo.user.eid,
  178. success: function (res) { //成功回调函数
  179. wx.showToast({
  180. title: '已复制',
  181. icon: "none"
  182. })
  183. }
  184. })
  185. },
  186. closeModal() {
  187. this.setData({
  188. activationModal: false
  189. })
  190. if (typeof this.getTabBar === 'function') {
  191. this.getTabBar().setData({
  192. mask: false
  193. })
  194. }
  195. },
  196. // 分享配置
  197. onShareAppMessage: function (res) {
  198. return {
  199. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  200. path: '/pages/index/index',
  201. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  202. }
  203. },
  204. onShareTimeline: function () {
  205. return {
  206. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  207. query: `uid=${wx.getStorageSync('uid')}`,
  208. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  209. }
  210. },
  211. })