index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // return this.selectComponent('#vipModal').open({
  125. // type: 'svip'
  126. // })
  127. wx.showLoading({
  128. title: '提交中',
  129. mask: true
  130. })
  131. let res = await buyVip({
  132. productId: currentTarget.dataset.id
  133. })
  134. let {
  135. timeStamp,
  136. nonceStr,
  137. signType,
  138. paySign
  139. } = res
  140. // package保留字
  141. wx.requestPayment({
  142. timeStamp,
  143. nonceStr,
  144. package: res.package,
  145. signType,
  146. paySign,
  147. success: (res) => {
  148. setTimeout(() => {
  149. this.setUserInfo()
  150. this.selectComponent('#vipModal').open({
  151. type: this.data.vipTime == '1' ? 'svip' : 'vip'
  152. })
  153. }, 1500)
  154. },
  155. fail(res) {
  156. wx.showToast({
  157. title: "支付失败",
  158. icon: "none",
  159. duration: 3000
  160. })
  161. },
  162. complete: () => {
  163. wx.hideLoading()
  164. }
  165. })
  166. },
  167. jump({
  168. currentTarget
  169. }) {
  170. let url = currentTarget.dataset.url
  171. wx.navigateTo({
  172. url: url
  173. });
  174. /* wx.openChannelsUserProfile({
  175. finderUserName: 'sphaBwcNkKMpmwi',
  176. success: (res) => {
  177. console.log(res);
  178. },
  179. fail: (res) => {
  180. console.log(res);
  181. }
  182. }) */
  183. },
  184. clipboar() {
  185. wx.setClipboardData({
  186. data: this.data.userInfo.user.eid,
  187. success: function (res) { //成功回调函数
  188. wx.showToast({
  189. title: '已复制',
  190. icon: "none"
  191. })
  192. }
  193. })
  194. },
  195. closeModal() {
  196. this.setData({
  197. activationModal: false
  198. })
  199. if (typeof this.getTabBar === 'function') {
  200. this.getTabBar().setData({
  201. mask: false
  202. })
  203. }
  204. },
  205. // 分享配置
  206. onShareAppMessage: function (res) {
  207. return {
  208. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  209. path: '/pages/index/index',
  210. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  211. }
  212. },
  213. onShareTimeline: function () {
  214. return {
  215. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  216. query: `uid=${wx.getStorageSync('uid')}`,
  217. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  218. }
  219. },
  220. })