order-detail.ts 6.0 KB

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