index.js 6.0 KB

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