order-detail.ts 5.9 KB

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