order-detail.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 } = data.data
  87. this.onExecutePay(paymentConfig, paymentType)
  88. } else {
  89. this.onPayError()
  90. }
  91. } catch {
  92. wx.hideLoading()
  93. }
  94. },
  95. async onExecutePay( paymentConfig: any, paymentType: 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. })
  105. wx.hideLoading()
  106. if(res.data.code === 200) {
  107. this.onPay(paymentType, res.data.data.reqParams)
  108. } else {
  109. this.onPayError()
  110. }
  111. },
  112. fail: () => {
  113. this.onPayError()
  114. }
  115. })
  116. },
  117. onPay(paymentType: string, paymentConfig: any) {
  118. const isYeePay = paymentType.indexOf('yeepay') !== -1
  119. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  120. : paymentConfig?.expend
  121. ? JSON.parse(paymentConfig?.expend?.pay_info)
  122. : paymentConfig
  123. const that = this
  124. wx.requestPayment({
  125. timeStamp: prePayInfo.timeStamp,
  126. nonceStr: prePayInfo.nonceStr,
  127. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  128. paySign: prePayInfo.paySign,
  129. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  130. success(resInfo) {
  131. console.log('支付成功', resInfo)
  132. wx.showToast({ title: '支付成功~', icon: 'success' });
  133. wx.redirectTo({
  134. url: '/pages/orders/order-result'
  135. })
  136. },
  137. fail(ressonInfo) {
  138. console.log('支付失败', ressonInfo)
  139. // wx.showToast({ title: '支付失败!', icon: 'none' });
  140. that.onPayError()
  141. }
  142. })
  143. },
  144. /**
  145. * 生命周期函数--监听页面初次渲染完成
  146. */
  147. onReady() {
  148. },
  149. /**
  150. * 生命周期函数--监听页面显示
  151. */
  152. onShow() {
  153. },
  154. /**
  155. * 生命周期函数--监听页面隐藏
  156. */
  157. onHide() {
  158. },
  159. /**
  160. * 生命周期函数--监听页面卸载
  161. */
  162. onUnload() {
  163. },
  164. /**
  165. * 页面相关事件处理函数--监听用户下拉动作
  166. */
  167. onPullDownRefresh() {
  168. },
  169. /**
  170. * 页面上拉触底事件的处理函数
  171. */
  172. onReachBottom() {
  173. },
  174. /**
  175. * 用户点击右上角分享
  176. */
  177. onShareAppMessage() {
  178. }
  179. })