index.js 6.0 KB

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