order-detail.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. },
  18. goodsInfo: {} as any,
  19. paymentType: null as any, // 支付类型
  20. paymentChannel: null as any,
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad(options: any) {
  26. this.queryPayType()
  27. if (options.orderInfo) {
  28. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  29. this.setData({
  30. goodsInfo: goods,
  31. status: goods.status
  32. });
  33. }
  34. },
  35. // 获取后台配置的支付方式
  36. async queryPayType() {
  37. try {
  38. const { data } = await api_queryByParamName({
  39. paramName: 'payment_service_provider'
  40. });
  41. if (data.code == 200) {
  42. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  43. this.setData({
  44. paymentType: paramValue.vendor,
  45. paymentChannel: paramValue.channel
  46. });
  47. }
  48. } catch (error) {
  49. console.log(error, "error");
  50. }
  51. },
  52. onPayError() {
  53. wx.hideLoading()
  54. wx.showToast({
  55. title: '支付失败',
  56. icon: 'none'
  57. })
  58. },
  59. // 购买
  60. async onSubmit() {
  61. wx.showLoading({
  62. mask: true,
  63. title: "订单提交中...",
  64. });
  65. try {
  66. const { salePrice, shopId, name, id } = this.data.goodsInfo
  67. const { data } = await api_executeOrder({
  68. "orderType": "WECHAT_MINI",
  69. "paymentType": this.data.paymentType,
  70. "paymentCashAmount": salePrice,
  71. "paymentCouponAmount": 0,
  72. "shopId": shopId,
  73. "opneId": app.globalData.userInfo?.liteOpenid,
  74. "goodsInfos": [{
  75. "goodsId": id,
  76. "goodsNum": 1,
  77. "goodsType": "ACTIVATION_CODE",
  78. "paymentCashAmount": salePrice,
  79. "paymentCouponAmount": 0
  80. }],
  81. "orderName": name,
  82. "orderDesc": name
  83. })
  84. console.log(data, 'data')
  85. if (data.code === 200) {
  86. const { paymentConfig, paymentType, orderNo } = data.data
  87. this.onExecutePay(paymentConfig, paymentType, orderNo)
  88. } else {
  89. this.onPayError()
  90. }
  91. } catch {
  92. wx.hideLoading()
  93. }
  94. },
  95. async onExecutePay( paymentConfig: any, paymentType: string, orderNo: string) {
  96. wx.login({
  97. success: async (wxres: any) => {
  98. const res = await api_executePayment({
  99. merOrderNo: paymentConfig.merOrderNo,
  100. paymentChannel: 'wx_lite',
  101. paymentType,
  102. userId: app.globalData.userInfo?.id,
  103. code: wxres.code,
  104. wxMiniAppId: app.globalData.appId
  105. })
  106. wx.hideLoading()
  107. if(res.data.code === 200) {
  108. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  109. } else {
  110. this.onPayError()
  111. }
  112. },
  113. fail: () => {
  114. this.onPayError()
  115. }
  116. })
  117. },
  118. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  119. const isYeePay = paymentType.indexOf('yeepay') !== -1
  120. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  121. : paymentConfig?.expend
  122. ? JSON.parse(paymentConfig?.expend?.pay_info)
  123. : paymentConfig
  124. const that = this
  125. wx.requestPayment({
  126. timeStamp: prePayInfo.timeStamp,
  127. nonceStr: prePayInfo.nonceStr,
  128. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  129. paySign: prePayInfo.paySign,
  130. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  131. success(resInfo) {
  132. console.log('支付成功', resInfo, {
  133. orderNo,
  134. paymentConfig,
  135. paymentType
  136. })
  137. wx.showToast({ title: '支付成功', icon: 'success' });
  138. wx.redirectTo({
  139. url: '/pages/orders/order-result?orderNo=' + orderNo
  140. })
  141. },
  142. fail(ressonInfo) {
  143. console.log('支付失败', ressonInfo)
  144. // wx.showToast({ title: '支付失败!', icon: 'none' });
  145. that.onPayError()
  146. }
  147. })
  148. },
  149. /**
  150. * 生命周期函数--监听页面初次渲染完成
  151. */
  152. onReady() {
  153. },
  154. /**
  155. * 生命周期函数--监听页面显示
  156. */
  157. onShow() {
  158. },
  159. /**
  160. * 生命周期函数--监听页面隐藏
  161. */
  162. onHide() {
  163. },
  164. /**
  165. * 生命周期函数--监听页面卸载
  166. */
  167. onUnload() {
  168. },
  169. /**
  170. * 页面相关事件处理函数--监听用户下拉动作
  171. */
  172. onPullDownRefresh() {
  173. },
  174. /**
  175. * 页面上拉触底事件的处理函数
  176. */
  177. onReachBottom() {
  178. },
  179. /**
  180. * 用户点击右上角分享
  181. */
  182. onShareAppMessage() {
  183. }
  184. })