index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let res = await androidbuyVip({
  147. productId: currentTarget.dataset.id
  148. }).finally(() => {
  149. wx.hideLoading()
  150. })
  151. wx.miniapp.requestPayment({
  152. timeStamp: res.timestamp,
  153. mchId: res.partnerid,
  154. prepayId: res.prepayid,
  155. package: res.package,
  156. nonceStr: res.noncestr,
  157. sign: res.sign,
  158. success: async (res) => {
  159. setTimeout(() => {
  160. this.setUserInfo()
  161. this.selectComponent('#vipModal').open()
  162. }, 1500)
  163. userEvent({
  164. action: 'ANDROID_PAY_SUCCESS',
  165. })
  166. },
  167. fail(res) {
  168. wx.showToast({
  169. title: "支付失败",
  170. icon: "none",
  171. duration: 3000
  172. })
  173. },
  174. complete: (res) => {
  175. console.error('wx.miniapp.requestPayment complete:', res)
  176. }
  177. })
  178. return
  179. userEvent({
  180. action: 'ANDROID_PAY_ACTIVITY',
  181. })
  182. let {
  183. timeStamp,
  184. nonceStr,
  185. signType,
  186. paySign
  187. } = res
  188. // package保留字
  189. wx.requestPayment({
  190. timeStamp,
  191. nonceStr,
  192. package: res.package,
  193. signType,
  194. paySign,
  195. success: async (res) => {
  196. setTimeout(() => {
  197. this.setUserInfo()
  198. this.selectComponent('#vipModal').open()
  199. }, 1500)
  200. userEvent({
  201. action: 'ANDROID_PAY_SUCCESS',
  202. })
  203. },
  204. fail(res) {
  205. wx.showToast({
  206. title: "支付失败",
  207. icon: "none",
  208. duration: 3000
  209. })
  210. }
  211. })
  212. },
  213. jump({
  214. currentTarget
  215. }) {
  216. let url = currentTarget.dataset.url
  217. console.log(url);
  218. wx.navigateTo({
  219. url: url
  220. });
  221. },
  222. clipboar() {
  223. wx.setClipboardData({
  224. data: this.data.userInfo.user.eid,
  225. success: function (res) { //成功回调函数
  226. wx.showToast({
  227. title: '已复制',
  228. icon: "none"
  229. })
  230. }
  231. })
  232. },
  233. closeModal() {
  234. this.setData({
  235. activationModal: false
  236. })
  237. if (typeof this.getTabBar === 'function') {
  238. this.getTabBar().setData({
  239. mask: false
  240. })
  241. }
  242. },
  243. // 分享配置
  244. onShareAppMessage: function (res) {
  245. return {
  246. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  247. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  248. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  249. }
  250. },
  251. onShareTimeline: function () {
  252. return {
  253. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  254. query: `uid=${wx.getStorageSync('uid')}`,
  255. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  256. }
  257. },
  258. })