index.js 6.3 KB

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