index.js 5.7 KB

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