index.js 4.8 KB

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