order-detail.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid, api_getUserReceiveAddressPage } from "../../api/login";
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. popupShow: false,
  11. isAddressInfoTip: false,
  12. addressInfo: {} as any,
  13. serviceShow: true,
  14. status: 'ing',
  15. statusList: {
  16. ing: {
  17. logo: './images/ing.png',
  18. title: '待付款',
  19. content: '为了确保您的订单顺利进行,请尽快完成支付'
  20. },
  21. },
  22. goodsInfo: {} as any,
  23. instrumentsInfo: {} as any,
  24. memberInfo: {} as any,
  25. paymentType: null as any, // 支付类型
  26. paymentChannel: null as any,
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad(options: any) {
  32. this.queryPayType()
  33. if (options.orderInfo) {
  34. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  35. const instrumentsInfo = JSON.parse(decodeURIComponent(options.instrumentsInfo));
  36. const memberInfo = JSON.parse(decodeURIComponent(options.memberInfo));
  37. console.log(goods, 'goods', instrumentsInfo, memberInfo)
  38. this.setData({
  39. goodsInfo: goods,
  40. instrumentsInfo,
  41. memberInfo,
  42. status: goods.status
  43. });
  44. }
  45. },
  46. onShow() {
  47. this.setData({
  48. serviceShow: true
  49. })
  50. },
  51. onHide() {
  52. this.setData({
  53. serviceShow: false
  54. })
  55. },
  56. async onAddressInfo() {
  57. this.setData({
  58. isAddressInfoTip: false
  59. })
  60. try {
  61. const resData = await api_getUserReceiveAddressPage()
  62. const pageRows = resData?.data?.data?.rows || []
  63. // 当有收货地址的时候跳转到收货地址选择列表页面,当没有收货地址的时候到添加收货地址
  64. if (pageRows.length) {
  65. wx.navigateTo({
  66. url: `/pages/address/addressList?id=${this.data.addressInfo.id}`
  67. });
  68. } else {
  69. this.setData({
  70. popupShow: true
  71. })
  72. }
  73. } catch (e) {
  74. console.log(e, 'e')
  75. }
  76. },
  77. onAddAddress(e: any) {
  78. const { addressInfo } = e.detail
  79. this.setData({
  80. addressInfo
  81. })
  82. },
  83. // 获取后台配置的支付方式
  84. async queryPayType() {
  85. try {
  86. // wxlite_payment_service_provider
  87. const { data } = await api_queryByParamName({
  88. paramName: app.globalData.appId
  89. });
  90. if (data.code == 200) {
  91. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  92. this.setData({
  93. paymentType: paramValue.vendor,
  94. paymentChannel: paramValue.channel
  95. });
  96. }
  97. } catch (error) {
  98. console.log(error, "error");
  99. }
  100. },
  101. onPayError(message?: string) {
  102. wx.hideLoading()
  103. wx.showToast({
  104. title: message || '支付取消',
  105. icon: 'none'
  106. })
  107. },
  108. // 购买
  109. async onSubmit() {
  110. // 有乐器必须填收货信息
  111. if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  112. wx.showToast({
  113. title: "请填写收货信息",
  114. icon: 'none'
  115. })
  116. this.setData({
  117. isAddressInfoTip: true
  118. })
  119. return
  120. }
  121. console.log(this.data.addressInfo)
  122. wx.showLoading({
  123. mask: true,
  124. title: "订单提交中...",
  125. });
  126. // 清除上一个页面的弹窗 有弹窗的时候数据返回回去数据才会刷新
  127. const pages = getCurrentPages();
  128. const prevPage = pages[pages.length - 2];
  129. prevPage.setData({ popupShow: false });
  130. try {
  131. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  132. if (orderNo) {
  133. const { data } = await api_userPaymentOrderUnpaid({
  134. orderNo: orderNo,
  135. paymentType: 'WECHAT_MINI'
  136. })
  137. if (data.code === 200) {
  138. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  139. this.onExecutePay(paymentConfig, paymentType, orderNo)
  140. } else {
  141. this.onPayError()
  142. }
  143. } else {
  144. const goodsInfos = [{
  145. "goodsId": id,
  146. "goodsNum": 1,
  147. "goodsType": "ACTIVATION_CODE",
  148. "paymentCashAmount": salePrice,
  149. "paymentCouponAmount": 0
  150. }]
  151. // 乐器
  152. if (this.data.instrumentsInfo.id) {
  153. goodsInfos.push({
  154. "goodsId": this.data.instrumentsInfo.id,
  155. "goodsNum": 1,
  156. "goodsType": "INSTRUMENTS",
  157. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  158. "paymentCouponAmount": 0
  159. })
  160. }
  161. const { data } = await api_executeOrder({
  162. "orderType": "WECHAT_MINI",
  163. "paymentType": this.data.paymentType,
  164. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  165. "paymentCouponAmount": 0,
  166. "shopId": shopId,
  167. "openId": app.globalData.userInfo?.liteOpenid,
  168. goodsInfos,
  169. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  170. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  171. "receiveAddress": this.data.addressInfo.id || "",
  172. "userBeneficiaryId": this.data.memberInfo.id
  173. })
  174. if (data.code === 200) {
  175. const { paymentConfig, paymentType, orderNo } = data.data
  176. this.onExecutePay(paymentConfig, paymentType, orderNo)
  177. } else if (data.code === 5200) {
  178. wx.hideLoading()
  179. wx.showToast({
  180. title: data.message,
  181. icon: 'none'
  182. })
  183. } else if (data.code === 5435) {
  184. wx.hideLoading()
  185. wx.showToast({
  186. title: data.message,
  187. icon: 'none'
  188. })
  189. setTimeout(() => { wx.navigateBack() }, 1000)
  190. } else {
  191. this.onPayError()
  192. }
  193. }
  194. } catch {
  195. wx.hideLoading()
  196. }
  197. },
  198. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  199. wx.login({
  200. success: async (wxres: any) => {
  201. const res = await api_executePayment({
  202. merOrderNo: paymentConfig.merOrderNo,
  203. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  204. paymentType,
  205. userId: app.globalData.userInfo?.id,
  206. code: wxres.code,
  207. wxMiniAppId: app.globalData.appId
  208. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  209. // wxPubAppId: 'wxbde13f59d40cb4f2'
  210. })
  211. wx.hideLoading()
  212. if (res.data.code === 200) {
  213. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  214. } else {
  215. this.onPayError(res.data.message)
  216. }
  217. },
  218. fail: () => {
  219. this.onPayError()
  220. }
  221. })
  222. },
  223. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  224. const isYeePay = paymentType.indexOf('yeepay') !== -1
  225. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  226. : paymentConfig?.expend
  227. ? JSON.parse(paymentConfig?.expend?.pay_info)
  228. : paymentConfig
  229. const that = this
  230. wx.requestPayment({
  231. timeStamp: prePayInfo.timeStamp,
  232. nonceStr: prePayInfo.nonceStr,
  233. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  234. paySign: prePayInfo.paySign,
  235. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  236. success() {
  237. wx.showToast({ title: '支付成功', icon: 'success' });
  238. wx.redirectTo({
  239. url: '/pages/orders/order-result?orderNo=' + orderNo
  240. })
  241. },
  242. fail(ressonInfo) {
  243. console.log('支付失败', ressonInfo)
  244. that.onPayError()
  245. const goodsInfo = that.data.goodsInfo
  246. goodsInfo.orderNo = orderNo
  247. that.setData({
  248. goodsInfo
  249. })
  250. }
  251. })
  252. },
  253. /**
  254. * 用户点击右上角分享
  255. */
  256. onShareAppMessage() {
  257. return {
  258. title: '音乐数字AI器乐工具',
  259. path: '/pages/index/index',
  260. imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png'
  261. }
  262. }
  263. })