order-detail.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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: app.globalData.appId
  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(message?: string) {
  55. wx.hideLoading()
  56. wx.showToast({
  57. title: message || '支付取消',
  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. } else if(data.code === 5200) {
  102. wx.hideLoading()
  103. wx.showToast({
  104. title: data.message,
  105. icon: 'none'
  106. })
  107. } else {
  108. this.onPayError()
  109. }
  110. }
  111. } catch {
  112. wx.hideLoading()
  113. }
  114. },
  115. async onExecutePay( paymentConfig: any, paymentType: string, orderNo: string) {
  116. wx.login({
  117. success: async (wxres: any) => {
  118. const res = await api_executePayment({
  119. merOrderNo: paymentConfig.merOrderNo,
  120. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  121. paymentType,
  122. userId: app.globalData.userInfo?.id,
  123. code: wxres.code,
  124. wxMiniAppId: app.globalData.appId
  125. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  126. // wxPubAppId: 'wxbde13f59d40cb4f2'
  127. })
  128. wx.hideLoading()
  129. if(res.data.code === 200) {
  130. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  131. } else {
  132. this.onPayError(res.data.message)
  133. }
  134. },
  135. fail: () => {
  136. this.onPayError()
  137. }
  138. })
  139. },
  140. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  141. const isYeePay = paymentType.indexOf('yeepay') !== -1
  142. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  143. : paymentConfig?.expend
  144. ? JSON.parse(paymentConfig?.expend?.pay_info)
  145. : paymentConfig
  146. const that = this
  147. wx.requestPayment({
  148. timeStamp: prePayInfo.timeStamp,
  149. nonceStr: prePayInfo.nonceStr,
  150. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  151. paySign: prePayInfo.paySign,
  152. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  153. success() {
  154. wx.showToast({ title: '支付成功', icon: 'success' });
  155. wx.redirectTo({
  156. url: '/pages/orders/order-result?orderNo=' + orderNo
  157. })
  158. },
  159. fail(ressonInfo) {
  160. console.log('支付失败', ressonInfo)
  161. that.onPayError()
  162. const goodsInfo = that.data.goodsInfo
  163. goodsInfo.orderNo = orderNo
  164. that.setData({
  165. goodsInfo
  166. })
  167. }
  168. })
  169. },
  170. openService() {
  171. wx.navigateTo({
  172. url: '../service/service'
  173. })
  174. },
  175. /**
  176. * 生命周期函数--监听页面初次渲染完成
  177. */
  178. onReady() {
  179. },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow() {
  184. },
  185. /**
  186. * 生命周期函数--监听页面隐藏
  187. */
  188. onHide() {
  189. },
  190. /**
  191. * 生命周期函数--监听页面卸载
  192. */
  193. onUnload() {
  194. },
  195. /**
  196. * 页面相关事件处理函数--监听用户下拉动作
  197. */
  198. onPullDownRefresh() {
  199. },
  200. /**
  201. * 页面上拉触底事件的处理函数
  202. */
  203. onReachBottom() {
  204. },
  205. /**
  206. * 用户点击右上角分享
  207. */
  208. onShareAppMessage() {
  209. }
  210. })