order-detail.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(message?: string) {
  67. wx.hideLoading()
  68. wx.showToast({
  69. title: message || '支付取消',
  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',
  133. paymentType,
  134. userId: app.globalData.userInfo?.id,
  135. code: wxres.code,
  136. wxMiniAppId: app.globalData.appId
  137. })
  138. wx.hideLoading()
  139. if(res.data.code === 200) {
  140. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  141. } else {
  142. this.onPayError(res.data.message)
  143. }
  144. },
  145. fail: () => {
  146. this.onPayError()
  147. }
  148. })
  149. },
  150. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  151. const isYeePay = paymentType.indexOf('yeepay') !== -1
  152. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  153. : paymentConfig?.expend
  154. ? JSON.parse(paymentConfig?.expend?.pay_info)
  155. : paymentConfig
  156. const that = this
  157. wx.requestPayment({
  158. timeStamp: prePayInfo.timeStamp,
  159. nonceStr: prePayInfo.nonceStr,
  160. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  161. paySign: prePayInfo.paySign,
  162. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  163. success() {
  164. wx.showToast({ title: '支付成功', icon: 'success' });
  165. wx.redirectTo({
  166. url: '/pages/orders/order-result?orderNo=' + orderNo
  167. })
  168. },
  169. fail(ressonInfo) {
  170. console.log('支付失败', ressonInfo)
  171. that.onPayError()
  172. const goodsInfo = that.data.goodsInfo
  173. goodsInfo.orderNo = orderNo
  174. that.setData({
  175. goodsInfo
  176. })
  177. }
  178. })
  179. },
  180. /** 客服 */
  181. onService() {
  182. console.log("showService")
  183. this.setData({
  184. showService: true
  185. })
  186. },
  187. changePop(event: { detail: any }) {
  188. this.setData({
  189. showService: event.detail
  190. })
  191. },
  192. /**
  193. * 生命周期函数--监听页面初次渲染完成
  194. */
  195. onReady() {
  196. },
  197. /**
  198. * 生命周期函数--监听页面卸载
  199. */
  200. onUnload() {
  201. },
  202. /**
  203. * 页面相关事件处理函数--监听用户下拉动作
  204. */
  205. onPullDownRefresh() {
  206. },
  207. /**
  208. * 页面上拉触底事件的处理函数
  209. */
  210. onReachBottom() {
  211. },
  212. /**
  213. * 用户点击右上角分享
  214. */
  215. onShareAppMessage() {
  216. return {
  217. title: '音乐数字AI',
  218. path: '/pages/index/index',
  219. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  220. }
  221. }
  222. })