index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (this.getTabBar()) {
  44. this.getTabBar().setData({
  45. mask: true
  46. })
  47. }
  48. this.setData({
  49. state: true,
  50. })
  51. // #endif
  52. },
  53. radioChange(e) {
  54. const items = this.data.items
  55. for (let i = 0, len = items.length; i < len; ++i) {
  56. items[i].checked = items[i].value === e.detail.value
  57. }
  58. this.setData({
  59. items,
  60. type: e.detail.value
  61. })
  62. },
  63. closeTranscript() {
  64. if (this.getTabBar()) {
  65. this.getTabBar().setData({
  66. mask: true
  67. })
  68. }
  69. this.setData({
  70. state: false
  71. })
  72. },
  73. async toBuy() {
  74. // #if MP
  75. wx.showLoading({
  76. title: '提交中',
  77. mask: true
  78. })
  79. let res = await buyVip({
  80. productId: this.data.product.id
  81. }).finally(() => {
  82. wx.hideLoading()
  83. })
  84. userEvent({
  85. action: 'ANDROID_PAY_ACTIVITY',
  86. })
  87. let {
  88. timeStamp,
  89. nonceStr,
  90. signType,
  91. paySign
  92. } = res
  93. // package保留字
  94. wx.requestPayment({
  95. timeStamp,
  96. nonceStr,
  97. package: res.package,
  98. signType,
  99. paySign,
  100. success: async (res) => {
  101. userEvent({
  102. action: 'ANDROID_PAY_SUCCESS',
  103. })
  104. setTimeout(() => {
  105. this.triggerEvent('reload')
  106. this.closeTranscript()
  107. }, 1500)
  108. },
  109. fail(res) {
  110. wx.showToast({
  111. title: "支付失败",
  112. icon: "none",
  113. duration: 3000
  114. })
  115. }
  116. })
  117. // #elif ANDROID
  118. if (this.data.type == 'wxPay') {
  119. let res = await androidbuyVip({
  120. productId: this.data.product.id
  121. }).finally(() => {
  122. wx.hideLoading()
  123. })
  124. wx.miniapp.requestPayment({
  125. timeStamp: res.timestamp,
  126. mchId: res.partnerid,
  127. prepayId: res.prepayid,
  128. package: res.package,
  129. nonceStr: res.noncestr,
  130. sign: res.sign,
  131. success: async (res) => {
  132. userEvent({
  133. action: 'ANDROID_PAY_SUCCESS',
  134. })
  135. setTimeout(() => {
  136. this.triggerEvent('reload')
  137. this.closeTranscript()
  138. }, 1500)
  139. },
  140. fail(res) {
  141. wx.showToast({
  142. title: "支付失败",
  143. icon: "none",
  144. duration: 3000
  145. })
  146. },
  147. complete: (res) => {
  148. console.error('wx.miniapp.requestPayment complete:', res)
  149. }
  150. })
  151. } else {
  152. let res = await aliPay({
  153. productId: this.data.product.id
  154. }).finally(() => {
  155. wx.hideLoading()
  156. })
  157. app.globalData.plugin.aliPay({
  158. orderInfo: res
  159. }, (res) => {
  160. userEvent({
  161. action: 'ANDROID_PAY_SUCCESS',
  162. })
  163. setTimeout(() => {
  164. this.triggerEvent('reload')
  165. this.closeTranscript()
  166. }, 1500)
  167. })
  168. }
  169. // #endif
  170. },
  171. }
  172. })