index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {
  2. getMyInfo,
  3. androidbuyVip,
  4. getVipInfo,
  5. getWxQrcode,
  6. getLearnCard
  7. } from '~/api/user'
  8. import event from '~/mixins/event'
  9. import {
  10. getProducts,
  11. userEvent
  12. } from '~/api/global'
  13. import {
  14. createStoreBindings
  15. } from 'mobx-miniprogram-bindings'
  16. import {
  17. store
  18. } from '~/store/index'
  19. const app = getApp()
  20. Page({
  21. behaviors: [event],
  22. data: {
  23. userInfo: {},
  24. vipTime: '',
  25. tasks: [],
  26. isIos: app.globalData.isIOS,
  27. qrCode: '',
  28. activationModal: false,
  29. activationRes: {},
  30. products: [],
  31. isPreferential: false
  32. },
  33. async onLoad() {
  34. // 手工绑定
  35. this.storeBindings = createStoreBindings(this, {
  36. store,
  37. actions: {
  38. setUser: 'setUser'
  39. }
  40. })
  41. if (this.data.isIos) {
  42. let qrCode = await getWxQrcode()
  43. this.setData({
  44. qrCode: qrCode.ticketUrl
  45. })
  46. }
  47. },
  48. async onShow() {
  49. if (typeof this.getTabBar === 'function') {
  50. this.getTabBar().setData({
  51. selected: 4
  52. })
  53. }
  54. let uid = wx.getStorageSync('uid') || ''
  55. if (!uid) {
  56. getApp().callBack = (res) => {
  57. this.setUserInfo()
  58. }
  59. } else {
  60. this.setUserInfo()
  61. }
  62. },
  63. // 设置用户信息及vip状态
  64. async setUserInfo() {
  65. let userInfo = await getMyInfo()
  66. let vipTime = await getVipInfo()
  67. this.setUser(userInfo.user)
  68. this.getProducts()
  69. this.setData({
  70. userInfo,
  71. vipTime,
  72. })
  73. },
  74. async getProducts() {
  75. let {
  76. productList: products,
  77. isPreferential
  78. } = await getProducts()
  79. console.log(products);
  80. this.setData({
  81. products,
  82. isPreferential
  83. })
  84. },
  85. activationCode() {
  86. wx.showModal({
  87. title: '请输入激活码',
  88. editable: true,
  89. success: async ({
  90. confirm,
  91. content
  92. }) => {
  93. if (confirm) {
  94. let regexp = /^[a-zA-Z0-9]{4}$/
  95. if (regexp.test(content)) {
  96. let activationRes = await getLearnCard({
  97. cardNo: content
  98. })
  99. if (activationRes.code == 200) {
  100. activationRes = {
  101. code: 200,
  102. message: '快去朗读挑战吧!'
  103. }
  104. }
  105. this.setUserInfo()
  106. if (typeof this.getTabBar === 'function') {
  107. this.getTabBar().setData({
  108. mask: true
  109. })
  110. }
  111. this.setData({
  112. activationModal: true,
  113. activationRes
  114. })
  115. } else {
  116. if (typeof this.getTabBar === 'function') {
  117. this.getTabBar().setData({
  118. mask: true
  119. })
  120. }
  121. this.setData({
  122. activationModal: true,
  123. activationRes: {
  124. code: 581,
  125. message: '请检查激活码输入是否正确'
  126. }
  127. })
  128. }
  129. }
  130. }
  131. })
  132. },
  133. toGzh() {
  134. this.selectComponent('#gzh').open()
  135. },
  136. async toBuy({
  137. currentTarget
  138. }) {
  139. if (currentTarget.dataset.isclick) {
  140. return
  141. }
  142. wx.showLoading({
  143. title: '提交中',
  144. mask: true
  145. })
  146. // #if MP
  147. userEvent({
  148. action: 'ANDROID_PAY_ACTIVITY',
  149. })
  150. let res = await buyVip({
  151. productId: currentTarget.dataset.id
  152. }).finally(() => {
  153. wx.hideLoading()
  154. })
  155. let {
  156. timeStamp,
  157. nonceStr,
  158. signType,
  159. paySign
  160. } = res
  161. // package保留字
  162. wx.requestPayment({
  163. timeStamp,
  164. nonceStr,
  165. package: res.package,
  166. signType,
  167. paySign,
  168. success: async (res) => {
  169. setTimeout(() => {
  170. this.setUserInfo()
  171. this.selectComponent('#vipModal').open()
  172. }, 1500)
  173. userEvent({
  174. action: 'ANDROID_PAY_SUCCESS',
  175. })
  176. },
  177. fail(res) {
  178. wx.showToast({
  179. title: "支付失败",
  180. icon: "none",
  181. duration: 3000
  182. })
  183. }
  184. })
  185. // #elif ANDROID
  186. let res = await androidbuyVip({
  187. productId: currentTarget.dataset.id
  188. }).finally(() => {
  189. wx.hideLoading()
  190. })
  191. wx.miniapp.requestPayment({
  192. timeStamp: res.timestamp,
  193. mchId: res.partnerid,
  194. prepayId: res.prepayid,
  195. package: res.package,
  196. nonceStr: res.noncestr,
  197. sign: res.sign,
  198. success: async (res) => {
  199. setTimeout(() => {
  200. this.setUserInfo()
  201. this.selectComponent('#vipModal').open()
  202. }, 1500)
  203. userEvent({
  204. action: 'ANDROID_PAY_SUCCESS',
  205. })
  206. },
  207. fail(res) {
  208. wx.showToast({
  209. title: "支付失败",
  210. icon: "none",
  211. duration: 3000
  212. })
  213. },
  214. complete: (res) => {
  215. console.error('wx.miniapp.requestPayment complete:', res)
  216. }
  217. })
  218. // #endif
  219. },
  220. jump({
  221. currentTarget
  222. }) {
  223. let url = currentTarget.dataset.url
  224. console.log(url);
  225. wx.navigateTo({
  226. url: url
  227. });
  228. },
  229. clipboar() {
  230. wx.setClipboardData({
  231. data: this.data.userInfo.user.eid,
  232. success: function (res) { //成功回调函数
  233. wx.showToast({
  234. title: '已复制',
  235. icon: "none"
  236. })
  237. }
  238. })
  239. },
  240. closeModal() {
  241. this.setData({
  242. activationModal: false
  243. })
  244. if (typeof this.getTabBar === 'function') {
  245. this.getTabBar().setData({
  246. mask: false
  247. })
  248. }
  249. },
  250. // 分享配置
  251. onShareAppMessage: function (res) {
  252. return {
  253. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  254. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  255. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  256. }
  257. },
  258. onShareTimeline: function () {
  259. return {
  260. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  261. query: `uid=${wx.getStorageSync('uid')}`,
  262. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  263. }
  264. },
  265. })