index.js 6.0 KB

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