index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. androidbuyVip,
  14. } from '~/api/user'
  15. import event from '~/mixins/event'
  16. Page({
  17. behaviors: [event],
  18. data: {
  19. products: [],
  20. product: {},
  21. mask: false
  22. },
  23. onLoad() {
  24. // 手工绑定
  25. this.storeBindings = createStoreBindings(this, {
  26. store,
  27. actions: {
  28. setUser: 'setUser'
  29. }
  30. })
  31. },
  32. onShow() {
  33. this.getProducts()
  34. },
  35. async getProducts() {
  36. let {
  37. productList: products,
  38. } = await getProducts()
  39. this.setData({
  40. products,
  41. active: products[0].id,
  42. product: products[0]
  43. })
  44. },
  45. selected({
  46. currentTarget
  47. }) {
  48. this.setData({
  49. active: currentTarget.dataset.product.id,
  50. product: currentTarget.dataset.product
  51. })
  52. },
  53. async toBuy() {
  54. if (!this.data.active) {
  55. return
  56. }
  57. wx.showLoading({
  58. title: '提交中',
  59. mask: true
  60. })
  61. // #if MP
  62. let res = await buyVip({
  63. productId: this.data.active
  64. }).finally(() => {
  65. wx.hideLoading()
  66. })
  67. userEvent({
  68. action: 'ANDROID_PAY_ACTIVITY',
  69. })
  70. let {
  71. timeStamp,
  72. nonceStr,
  73. signType,
  74. paySign
  75. } = res
  76. // package保留字
  77. wx.requestPayment({
  78. timeStamp,
  79. nonceStr,
  80. package: res.package,
  81. signType,
  82. paySign,
  83. success: async (res) => {
  84. setTimeout(() => {
  85. this.setUserInfo()
  86. this.setData({
  87. mask: true
  88. })
  89. }, 1500)
  90. userEvent({
  91. action: 'ANDROID_PAY_SUCCESS',
  92. })
  93. },
  94. fail(res) {
  95. wx.showToast({
  96. title: "支付失败",
  97. icon: "none",
  98. duration: 3000
  99. })
  100. }
  101. })
  102. // #elif ANDROID
  103. let res = await androidbuyVip({
  104. productId: this.data.active
  105. }).finally(() => {
  106. wx.hideLoading()
  107. })
  108. wx.miniapp.requestPayment({
  109. timeStamp: res.timestamp,
  110. mchId: res.partnerid,
  111. prepayId: res.prepayid,
  112. package: res.package,
  113. nonceStr: res.noncestr,
  114. sign: res.sign,
  115. success: async (res) => {
  116. setTimeout(() => {
  117. this.setUserInfo()
  118. this.setData({
  119. mask: true
  120. })
  121. }, 1500)
  122. userEvent({
  123. action: 'ANDROID_PAY_SUCCESS',
  124. })
  125. },
  126. fail(res) {
  127. wx.showToast({
  128. title: "支付失败",
  129. icon: "none",
  130. duration: 3000
  131. })
  132. },
  133. complete: (res) => {
  134. console.error('wx.miniapp.requestPayment complete:', res)
  135. }
  136. })
  137. // #endif
  138. },
  139. // 设置用户信息及vip状态
  140. async setUserInfo() {
  141. let userInfo = await getMyInfo()
  142. this.setUser(userInfo.user)
  143. },
  144. closeMask() {
  145. this.setData({
  146. mask: false,
  147. })
  148. // this.getWxCode()
  149. },
  150. onUnload() {
  151. this.storeBindings.destroyStoreBindings();
  152. }
  153. })