index.js 5.9 KB

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