order-detail.ts 5.9 KB

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