index.js 4.8 KB

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