index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. product: products[0]
  42. })
  43. },
  44. selected({
  45. currentTarget
  46. }) {
  47. this.setData({
  48. product: currentTarget.dataset.product
  49. })
  50. },
  51. async toBuy() {
  52. if (!this.data.active) {
  53. return
  54. }
  55. wx.showLoading({
  56. title: '提交中',
  57. mask: true
  58. })
  59. // #if MP
  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. // #elif ANDROID
  101. let res = await androidbuyVip({
  102. productId: this.data.active
  103. }).finally(() => {
  104. wx.hideLoading()
  105. })
  106. wx.miniapp.requestPayment({
  107. timeStamp: res.timestamp,
  108. mchId: res.partnerid,
  109. prepayId: res.prepayid,
  110. package: res.package,
  111. nonceStr: res.noncestr,
  112. sign: res.sign,
  113. success: async (res) => {
  114. setTimeout(() => {
  115. this.setUserInfo()
  116. this.setData({
  117. mask: true
  118. })
  119. }, 1500)
  120. userEvent({
  121. action: 'ANDROID_PAY_SUCCESS',
  122. })
  123. },
  124. fail(res) {
  125. wx.showToast({
  126. title: "支付失败",
  127. icon: "none",
  128. duration: 3000
  129. })
  130. },
  131. complete: (res) => {
  132. console.error('wx.miniapp.requestPayment complete:', res)
  133. }
  134. })
  135. // #endif
  136. },
  137. paySuccess() {
  138. this.setUserInfo()
  139. this.setData({
  140. mask: true
  141. })
  142. },
  143. openDonutBuy() {
  144. let product = this.data.product
  145. console.log(product);
  146. this.selectComponent('#donutBuy').open(product)
  147. },
  148. // 设置用户信息及vip状态
  149. async setUserInfo() {
  150. let userInfo = await getMyInfo()
  151. this.setUser(userInfo.user)
  152. },
  153. closeMask() {
  154. this.setData({
  155. mask: false,
  156. })
  157. // this.getWxCode()
  158. },
  159. onUnload() {
  160. this.storeBindings.destroyStoreBindings();
  161. }
  162. })