index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {
  2. createStoreBindings
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. import {
  8. getProducts,
  9. userEvent
  10. } from '~/api/global'
  11. import {
  12. buyVip,
  13. } from '~/api/user'
  14. import event from '~/mixins/event'
  15. Page({
  16. behaviors: [event],
  17. data: {
  18. products: [],
  19. product: {},
  20. mask: false
  21. },
  22. onLoad() {
  23. // 手工绑定
  24. this.storeBindings = createStoreBindings(this, {
  25. store,
  26. actions: {
  27. setUser: 'setUser'
  28. }
  29. })
  30. },
  31. onShow() {
  32. this.getProducts()
  33. },
  34. async getProducts() {
  35. let {
  36. productList: products,
  37. } = await getProducts()
  38. this.setData({
  39. products,
  40. active: products[0].id,
  41. product: products[0]
  42. })
  43. },
  44. selected({
  45. currentTarget
  46. }) {
  47. this.setData({
  48. active: currentTarget.dataset.product.id,
  49. product: currentTarget.dataset.product
  50. })
  51. },
  52. async toBuy() {
  53. if (!this.data.active) {
  54. return
  55. }
  56. wx.showLoading({
  57. title: '提交中',
  58. mask: true
  59. })
  60. let res = await buyVip({
  61. productId: this.data.active
  62. }).finally(() => {
  63. wx.hideLoading()
  64. })
  65. userEvent({
  66. action: 'ANDROID_PAY_ACTIVITY',
  67. })
  68. let {
  69. timeStamp,
  70. nonceStr,
  71. signType,
  72. paySign
  73. } = res
  74. // package保留字
  75. wx.requestPayment({
  76. timeStamp,
  77. nonceStr,
  78. package: res.package,
  79. signType,
  80. paySign,
  81. success: async (res) => {
  82. setTimeout(() => {
  83. this.setUserInfo()
  84. this.setData({
  85. mask: true
  86. })
  87. }, 1500)
  88. userEvent({
  89. action: 'ANDROID_PAY_SUCCESS',
  90. })
  91. },
  92. fail(res) {
  93. wx.showToast({
  94. title: "支付失败",
  95. icon: "none",
  96. duration: 3000
  97. })
  98. }
  99. })
  100. },
  101. // 设置用户信息及vip状态
  102. async setUserInfo() {
  103. let userInfo = await getMyInfo()
  104. this.setUser(userInfo.user)
  105. },
  106. closeMask() {
  107. this.setData({
  108. mask: false,
  109. })
  110. // this.getWxCode()
  111. },
  112. onUnload() {
  113. this.storeBindings.destroyStoreBindings();
  114. }
  115. })