index.js 4.8 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: 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. console.log(vipTime );
  57. this.setUser(userInfo.user)
  58. this.setData({
  59. userInfo,
  60. vipTime,
  61. })
  62. },
  63. async getProducts() {
  64. let products = await getProducts()
  65. console.log(products);
  66. this.setData({
  67. products,
  68. })
  69. },
  70. activationCode() {
  71. wx.showModal({
  72. title: '请输入激活码',
  73. editable: true,
  74. success: async ({
  75. confirm,
  76. content
  77. }) => {
  78. if (confirm) {
  79. let regexp = /^[a-zA-Z0-9]{4}$/
  80. if (regexp.test(content)) {
  81. let activationRes = await getLearnCard({
  82. cardNo: content
  83. })
  84. console.log(activationRes);
  85. if (activationRes.code == 200) {
  86. activationRes = {
  87. code: 200,
  88. message: '快去朗读挑战吧!'
  89. }
  90. }
  91. this.setUserInfo()
  92. if (typeof this.getTabBar === 'function') {
  93. this.getTabBar().setData({
  94. mask: true
  95. })
  96. }
  97. this.setData({
  98. activationModal: true,
  99. activationRes
  100. })
  101. } else {
  102. if (typeof this.getTabBar === 'function') {
  103. this.getTabBar().setData({
  104. mask: true
  105. })
  106. }
  107. this.setData({
  108. activationModal: true,
  109. activationRes: {
  110. code: 581,
  111. message: '请检查激活码输入是否正确'
  112. }
  113. })
  114. }
  115. }
  116. }
  117. })
  118. },
  119. async toBuy({
  120. currentTarget
  121. }) {
  122. console.log(currentTarget);
  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.setData({
  148. activationModal: true
  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. })