index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {
  2. androidbuyVip,
  3. buyVip,
  4. } from '~/api/user'
  5. import {
  6. userEvent
  7. } from '~/api/global'
  8. Component({
  9. /**
  10. * 组件的属性列表
  11. */
  12. properties: {
  13. },
  14. data: {
  15. state: false,
  16. product: {},
  17. items: [{
  18. value: 'wxpay',
  19. name: '微信',
  20. checked: true
  21. },
  22. {
  23. value: 'alipay',
  24. name: '支付宝',
  25. checked: false
  26. },
  27. ]
  28. },
  29. methods: {
  30. open(product) {
  31. if (!product) {
  32. return
  33. }
  34. this.setData({
  35. product
  36. })
  37. // #if MP
  38. this.toBuy()
  39. // #elif ANDROID
  40. this.getTabBar().setData({
  41. mask: true
  42. })
  43. this.setData({
  44. state: true,
  45. })
  46. // #endif
  47. },
  48. radioChange(e) {
  49. const items = this.data.items
  50. for (let i = 0, len = items.length; i < len; ++i) {
  51. items[i].checked = items[i].value === e.detail.value
  52. }
  53. this.setData({
  54. items
  55. })
  56. },
  57. closeTranscript() {
  58. this.getTabBar().setData({
  59. mask: false
  60. })
  61. this.setData({
  62. state: false
  63. })
  64. },
  65. async toBuy() {
  66. wx.showLoading({
  67. title: '提交中',
  68. mask: true
  69. })
  70. // #if MP
  71. let res = await buyVip({
  72. productId: this.data.product.id
  73. }).finally(() => {
  74. wx.hideLoading()
  75. })
  76. userEvent({
  77. action: 'ANDROID_PAY_ACTIVITY',
  78. })
  79. let {
  80. timeStamp,
  81. nonceStr,
  82. signType,
  83. paySign
  84. } = res
  85. // package保留字
  86. wx.requestPayment({
  87. timeStamp,
  88. nonceStr,
  89. package: res.package,
  90. signType,
  91. paySign,
  92. success: async (res) => {
  93. userEvent({
  94. action: 'ANDROID_PAY_SUCCESS',
  95. })
  96. setTimeout(() => {
  97. this.triggerEvent('reload')
  98. this.closeTranscript()
  99. }, 1500)
  100. },
  101. fail(res) {
  102. wx.showToast({
  103. title: "支付失败",
  104. icon: "none",
  105. duration: 3000
  106. })
  107. }
  108. })
  109. // #elif ANDROID
  110. let res = await androidbuyVip({
  111. productId: this.data.product.id
  112. }).finally(() => {
  113. wx.hideLoading()
  114. })
  115. wx.miniapp.requestPayment({
  116. timeStamp: res.timestamp,
  117. mchId: res.partnerid,
  118. prepayId: res.prepayid,
  119. package: res.package,
  120. nonceStr: res.noncestr,
  121. sign: res.sign,
  122. success: async (res) => {
  123. /* setTimeout(() => {
  124. this.setUserInfo()
  125. this.setData({
  126. mask: true
  127. })
  128. }, 1500)
  129. userEvent({
  130. action: 'ANDROID_PAY_SUCCESS',
  131. }) */
  132. },
  133. fail(res) {
  134. wx.showToast({
  135. title: "支付失败",
  136. icon: "none",
  137. duration: 3000
  138. })
  139. },
  140. complete: (res) => {
  141. console.error('wx.miniapp.requestPayment complete:', res)
  142. }
  143. })
  144. // #endif
  145. },
  146. }
  147. })