order-detail.ts 5.8 KB

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