index.js 6.1 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. */
  25. activationModal: false,
  26. activationRes: {},
  27. products: []
  28. },
  29. onLoad() {
  30. // 手工绑定
  31. this.storeBindings = createStoreBindings(this, {
  32. store,
  33. actions: {
  34. setUser: 'setUser'
  35. }
  36. })
  37. this.getProducts()
  38. this.selectComponent('#vipModal').open({
  39. type: 'vip'
  40. })
  41. },
  42. async onShow() {
  43. if (typeof this.getTabBar === 'function') {
  44. this.getTabBar().setData({
  45. selected: 4
  46. })
  47. }
  48. let uid = wx.getStorageSync('uid') || ''
  49. if (!uid) {
  50. getApp().callBack = (res) => {
  51. this.setUserInfo()
  52. }
  53. } else {
  54. this.setUserInfo()
  55. }
  56. },
  57. // 设置用户信息及vip状态
  58. async setUserInfo() {
  59. let userInfo = await getMyInfo()
  60. let vipTime = await getVipInfo()
  61. this.setUser(userInfo.user)
  62. this.setData({
  63. userInfo,
  64. vipTime,
  65. })
  66. },
  67. async getProducts() {
  68. let products = await getProducts()
  69. console.log(products);
  70. this.setData({
  71. products,
  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. console.log(activationRes);
  89. if (activationRes.code == 200) {
  90. activationRes = {
  91. code: 200,
  92. message: '快去朗读挑战吧!'
  93. }
  94. }
  95. this.setUserInfo()
  96. if (typeof this.getTabBar === 'function') {
  97. this.getTabBar().setData({
  98. mask: true
  99. })
  100. }
  101. this.setData({
  102. activationModal: true,
  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.selectComponent('#vipModal').open({
  151. type: 'svip'
  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. })