index.js 4.7 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: false,
  24. activationRes: {},
  25. products: []
  26. },
  27. onLoad() {
  28. // 手工绑定
  29. this.storeBindings = createStoreBindings(this, {
  30. store,
  31. actions: {
  32. setUser: 'setUser'
  33. }
  34. })
  35. this.getProducts()
  36. },
  37. async onShow() {
  38. if (typeof this.getTabBar === 'function') {
  39. this.getTabBar().setData({
  40. selected: 4
  41. })
  42. }
  43. let uid = wx.getStorageSync('uid') || ''
  44. if (!uid) {
  45. getApp().callBack = (res) => {
  46. this.setUserInfo()
  47. }
  48. } else {
  49. this.setUserInfo()
  50. }
  51. },
  52. // 设置用户信息及vip状态
  53. async setUserInfo() {
  54. let userInfo = await getMyInfo()
  55. let vipTime = await getVipInfo()
  56. this.setUser(userInfo.user)
  57. this.setData({
  58. userInfo,
  59. vipTime,
  60. })
  61. },
  62. async getProducts() {
  63. let products = await getProducts()
  64. console.log(products);
  65. this.setData({
  66. products,
  67. })
  68. },
  69. activationCode() {
  70. wx.showModal({
  71. title: '请输入激活码',
  72. editable: true,
  73. success: async ({
  74. confirm,
  75. content
  76. }) => {
  77. if (confirm) {
  78. let regexp = /^[a-zA-Z0-9]{4}$/
  79. if (regexp.test(content)) {
  80. let activationRes = await getLearnCard({
  81. cardNo: content
  82. })
  83. console.log(activationRes);
  84. if (activationRes.code == 200) {
  85. activationRes = {
  86. code: 200,
  87. message: '快去朗读挑战吧!'
  88. }
  89. }
  90. this.setUserInfo()
  91. if (typeof this.getTabBar === 'function') {
  92. this.getTabBar().setData({
  93. mask: true
  94. })
  95. }
  96. this.setData({
  97. activationModal: true,
  98. activationRes
  99. })
  100. } else {
  101. if (typeof this.getTabBar === 'function') {
  102. this.getTabBar().setData({
  103. mask: true
  104. })
  105. }
  106. this.setData({
  107. activationModal: true,
  108. activationRes: {
  109. code: 581,
  110. message: '请检查激活码输入是否正确'
  111. }
  112. })
  113. }
  114. }
  115. }
  116. })
  117. },
  118. async toBuy({
  119. currentTarget
  120. }) {
  121. console.log(currentTarget);
  122. wx.showLoading({
  123. title: '提交中',
  124. mask: true
  125. })
  126. let res = await buyVip({
  127. productId: currentTarget.dataset.id
  128. }).finally(() => {
  129. wx.hideLoading()
  130. })
  131. let {
  132. timeStamp,
  133. nonceStr,
  134. signType,
  135. paySign
  136. } = res
  137. // package保留字
  138. wx.requestPayment({
  139. timeStamp,
  140. nonceStr,
  141. package: res.package,
  142. signType,
  143. paySign,
  144. success: (res) => {
  145. console.log(res);
  146. this.setData({
  147. activationModal: true
  148. })
  149. /* setTimeout(() => {
  150. this.setUserInfo()
  151. }, 1500) */
  152. },
  153. fail(res) {
  154. wx.showToast({
  155. title: "支付失败",
  156. icon: "none",
  157. duration: 3000
  158. })
  159. }
  160. })
  161. },
  162. jump({
  163. currentTarget
  164. }) {
  165. let url = currentTarget.dataset.url
  166. wx.navigateTo({
  167. url: url
  168. });
  169. /* wx.openChannelsUserProfile({
  170. finderUserName: 'sphaBwcNkKMpmwi',
  171. success: (res) => {
  172. console.log(res);
  173. },
  174. fail: (res) => {
  175. console.log(res);
  176. }
  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. })