index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. toGzh() {
  122. this.selectComponent('#gzh').open()
  123. },
  124. async toBuy({
  125. currentTarget
  126. }) {
  127. if (currentTarget.dataset.isclick) {
  128. return
  129. }
  130. wx.showLoading({
  131. title: '提交中',
  132. mask: true
  133. })
  134. let res = await buyVip({
  135. productId: currentTarget.dataset.id
  136. })
  137. let {
  138. timeStamp,
  139. nonceStr,
  140. signType,
  141. paySign
  142. } = res
  143. // package保留字
  144. wx.requestPayment({
  145. timeStamp,
  146. nonceStr,
  147. package: res.package,
  148. signType,
  149. paySign,
  150. success: (res) => {
  151. setTimeout(() => {
  152. this.setUserInfo()
  153. this.selectComponent('#vipModal').open()
  154. }, 1500)
  155. },
  156. fail(res) {
  157. wx.showToast({
  158. title: "支付失败",
  159. icon: "none",
  160. duration: 3000
  161. })
  162. },
  163. complete: () => {
  164. wx.hideLoading()
  165. }
  166. })
  167. },
  168. jump({
  169. currentTarget
  170. }) {
  171. let url = currentTarget.dataset.url
  172. wx.navigateTo({
  173. url: url
  174. });
  175. },
  176. clipboar() {
  177. wx.setClipboardData({
  178. data: this.data.userInfo.user.eid,
  179. success: function (res) { //成功回调函数
  180. wx.showToast({
  181. title: '已复制',
  182. icon: "none"
  183. })
  184. }
  185. })
  186. },
  187. closeModal() {
  188. this.setData({
  189. activationModal: false
  190. })
  191. if (typeof this.getTabBar === 'function') {
  192. this.getTabBar().setData({
  193. mask: false
  194. })
  195. }
  196. },
  197. // 分享配置
  198. onShareAppMessage: function (res) {
  199. return {
  200. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  201. path: '/pages/index/index',
  202. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  203. }
  204. },
  205. onShareTimeline: function () {
  206. return {
  207. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  208. query: `uid=${wx.getStorageSync('uid')}`,
  209. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  210. }
  211. },
  212. })