index.js 6.2 KB

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