index.js 5.8 KB

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