order-result.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName } from "../../api/login";
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. status: 'ing',
  11. statusList: {
  12. ing: {
  13. logo: './images/ing.png',
  14. title: '等待付款',
  15. content: '请尽快完成支付,以便我们为您处理订单'
  16. },
  17. success: {
  18. logo: './images/success.png',
  19. title: '交易完成',
  20. content: '登录「音乐数字课堂」APP使用AI学练'
  21. },
  22. error: {
  23. logo: './images/error.png',
  24. title: '交易取消',
  25. content: '您的交易订单已关闭'
  26. },
  27. wait: {
  28. logo: './images/wait.png',
  29. title: '等待使用',
  30. content: '请尽快扫描下方二维码进行激活'
  31. }
  32. },
  33. goodsInfo: {} as any,
  34. paymentType: null as any, // 支付类型
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad(options: any) {
  40. this.queryPayType()
  41. if (options.orderInfo) {
  42. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  43. this.setData({
  44. goodsInfo: goods,
  45. status: goods.status
  46. });
  47. }
  48. },
  49. // 获取后台配置的支付方式
  50. async queryPayType() {
  51. try {
  52. const { data } = await api_queryByParamName({
  53. paramName: 'payment_service_provider'
  54. });
  55. if (data.code == 200) {
  56. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  57. this.setData({
  58. paymentType: paramValue.vendor,
  59. });
  60. }
  61. } catch (error) {
  62. console.log(error, "error");
  63. }
  64. },
  65. // 购买
  66. async onSubmit() {
  67. wx.showLoading({
  68. mask: true,
  69. title: "订单提交中...",
  70. });
  71. try {
  72. const { salePrice, shopId, name, id } = this.data.goodsInfo
  73. const { data } = await api_executeOrder({
  74. "orderType": "WECHAT_MINI",
  75. "paymentType": this.data.paymentType,
  76. "paymentCashAmount": salePrice,
  77. "paymentCouponAmount": 0,
  78. "shopId": shopId,
  79. "opneId": app.globalData.userInfo?.liteOpenid,
  80. "goodsInfos": [{
  81. "goodsId": id,
  82. "goodsNum": 1,
  83. "goodsType": "ACTIVATION_CODE",
  84. "paymentCashAmount": salePrice,
  85. "paymentCouponAmount": 0
  86. }],
  87. "orderName": name,
  88. "orderDesc": name
  89. })
  90. console.log(data, 'data')
  91. if (data.code === 200) {
  92. const { paymentConfig, paymentType } = data.data
  93. const res = await api_executePayment({
  94. merOrderNo: paymentConfig.merOrderNo,
  95. paymentChannel: 'wxpay-app',
  96. paymentType,
  97. userId: app.globalData.userInfo?.id
  98. })
  99. wx.hideLoading()
  100. this.onPay(paymentType, res.data.data.reqParams)
  101. } else {
  102. wx.hideLoading()
  103. wx.showToast({
  104. title: '支付失败',
  105. icon: 'none'
  106. })
  107. }
  108. } catch {
  109. wx.hideLoading()
  110. }
  111. },
  112. onPay(paymentType: string, paymentConfig: any) {
  113. const isYeePay = paymentType.indexOf('yeepay') !== -1
  114. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  115. : paymentConfig?.expend
  116. ? JSON.parse(paymentConfig?.expend?.pay_info)
  117. : paymentConfig
  118. wx.requestPayment({
  119. timeStamp: prePayInfo.timeStamp,
  120. nonceStr: prePayInfo.nonceStr,
  121. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  122. paySign: prePayInfo.paySign,
  123. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  124. success(resInfo) {
  125. console.log('支付成功', resInfo)
  126. wx.showToast({ title: '支付成功~', icon: 'success' });
  127. // that.onClose('end')
  128. },
  129. fail(ressonInfo) {
  130. console.log('支付失败', ressonInfo)
  131. wx.showToast({ title: '支付失败!', icon: 'none' });
  132. }
  133. })
  134. },
  135. /**
  136. * 生命周期函数--监听页面初次渲染完成
  137. */
  138. onReady() {
  139. },
  140. /**
  141. * 生命周期函数--监听页面显示
  142. */
  143. onShow() {
  144. },
  145. /**
  146. * 生命周期函数--监听页面隐藏
  147. */
  148. onHide() {
  149. },
  150. /**
  151. * 生命周期函数--监听页面卸载
  152. */
  153. onUnload() {
  154. },
  155. /**
  156. * 页面相关事件处理函数--监听用户下拉动作
  157. */
  158. onPullDownRefresh() {
  159. },
  160. /**
  161. * 页面上拉触底事件的处理函数
  162. */
  163. onReachBottom() {
  164. },
  165. /**
  166. * 用户点击右上角分享
  167. */
  168. onShareAppMessage() {
  169. }
  170. })