order-detail.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if (this.data.goodsInfo.orderNo) {
  61. return
  62. }
  63. try {
  64. const resData = await api_getUserReceiveAddressPage()
  65. const pageRows = resData?.data?.data?.rows || []
  66. // 当有收货地址的时候跳转到收货地址选择列表页面,当没有收货地址的时候到添加收货地址
  67. if (pageRows.length) {
  68. wx.navigateTo({
  69. url: `/pages/address/addressList?id=${this.data.addressInfo.id}`
  70. });
  71. } else {
  72. this.setData({
  73. popupShow: true
  74. })
  75. }
  76. } catch (e) {
  77. console.log(e, 'e')
  78. }
  79. },
  80. onAddAddress(e: any) {
  81. const { addressInfo } = e.detail
  82. this.setData({
  83. addressInfo
  84. })
  85. },
  86. // 获取后台配置的支付方式
  87. async queryPayType() {
  88. try {
  89. // wxlite_payment_service_provider
  90. const { data } = await api_queryByParamName({
  91. paramName: app.globalData.appId
  92. });
  93. if (data.code == 200) {
  94. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  95. this.setData({
  96. paymentType: paramValue.vendor,
  97. paymentChannel: paramValue.channel
  98. });
  99. }
  100. } catch (error) {
  101. console.log(error, "error");
  102. }
  103. },
  104. onPayError(message?: string) {
  105. wx.hideLoading()
  106. wx.showToast({
  107. title: message || '支付取消',
  108. icon: 'none'
  109. })
  110. },
  111. // 购买
  112. async onSubmit() {
  113. // 有乐器必须填收货信息
  114. if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  115. wx.showToast({
  116. title: "请填写收货信息",
  117. icon: 'none'
  118. })
  119. this.setData({
  120. isAddressInfoTip: true
  121. })
  122. return
  123. }
  124. console.log(this.data.addressInfo)
  125. wx.showLoading({
  126. mask: true,
  127. title: "订单提交中...",
  128. });
  129. // 清除上一个页面的弹窗 有弹窗的时候数据返回回去数据才会刷新
  130. const pages = getCurrentPages();
  131. const prevPage = pages[pages.length - 2];
  132. prevPage.setData({ popupShow: false, memberInfo: {} });
  133. try {
  134. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  135. if (orderNo) {
  136. const { data } = await api_userPaymentOrderUnpaid({
  137. orderNo: orderNo,
  138. paymentType: 'WECHAT_MINI'
  139. })
  140. if (data.code === 200) {
  141. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  142. this.onExecutePay(paymentConfig, paymentType, orderNo)
  143. } else {
  144. this.onPayError()
  145. }
  146. } else {
  147. const goodsInfos = [{
  148. "goodsId": id,
  149. "goodsNum": 1,
  150. "goodsType": "ACTIVATION_CODE",
  151. "paymentCashAmount": salePrice,
  152. "paymentCouponAmount": 0
  153. }]
  154. // 乐器
  155. if (this.data.instrumentsInfo.id) {
  156. goodsInfos.push({
  157. "goodsId": this.data.instrumentsInfo.id,
  158. "goodsNum": 1,
  159. "goodsType": "INSTRUMENTS",
  160. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  161. "paymentCouponAmount": 0
  162. })
  163. }
  164. const { data } = await api_executeOrder({
  165. "orderType": "WECHAT_MINI",
  166. "paymentType": this.data.paymentType,
  167. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  168. "paymentCouponAmount": 0,
  169. "shopId": shopId,
  170. "openId": app.globalData.userInfo?.liteOpenid,
  171. goodsInfos,
  172. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  173. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  174. "receiveAddress": this.data.addressInfo.id || "",
  175. "userBeneficiaryId": this.data.memberInfo.id
  176. })
  177. if (data.code === 200) {
  178. const { paymentConfig, paymentType, orderNo } = data.data
  179. this.onExecutePay(paymentConfig, paymentType, orderNo)
  180. } else if (data.code === 5435) {
  181. this.onPayError(data.message)
  182. setTimeout(() => { wx.navigateBack() }, 1000)
  183. }else {
  184. this.onPayError(data.message)
  185. }
  186. }
  187. } catch {
  188. wx.hideLoading()
  189. }
  190. },
  191. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  192. wx.login({
  193. success: async (wxres: any) => {
  194. const res = await api_executePayment({
  195. merOrderNo: paymentConfig.merOrderNo,
  196. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  197. paymentType,
  198. userId: app.globalData.userInfo?.id,
  199. code: wxres.code,
  200. wxMiniAppId: app.globalData.appId
  201. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  202. // wxPubAppId: 'wxbde13f59d40cb4f2'
  203. })
  204. wx.hideLoading()
  205. if (res.data.code === 200) {
  206. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  207. } else if (res.data.code === 5435) {
  208. this.onPayError(res.data.message)
  209. setTimeout(() => { wx.navigateBack() }, 1000)
  210. }else {
  211. this.onPayError(res.data.message)
  212. }
  213. },
  214. fail: () => {
  215. this.onPayError()
  216. }
  217. })
  218. },
  219. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  220. const isYeePay = paymentType.indexOf('yeepay') !== -1
  221. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  222. : paymentConfig?.expend
  223. ? JSON.parse(paymentConfig?.expend?.pay_info)
  224. : paymentConfig
  225. const that = this
  226. wx.requestPayment({
  227. timeStamp: prePayInfo.timeStamp,
  228. nonceStr: prePayInfo.nonceStr,
  229. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  230. paySign: prePayInfo.paySign,
  231. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  232. success() {
  233. wx.showToast({ title: '支付成功', icon: 'success' });
  234. wx.redirectTo({
  235. url: '/pages/orders/order-result?orderNo=' + orderNo
  236. })
  237. },
  238. fail(ressonInfo) {
  239. console.log('支付失败', ressonInfo)
  240. that.onPayError()
  241. const goodsInfo = that.data.goodsInfo
  242. goodsInfo.orderNo = orderNo
  243. that.setData({
  244. goodsInfo
  245. })
  246. wx.redirectTo({
  247. url: '/pages/orders/order-result?orderNo=' + orderNo
  248. })
  249. }
  250. })
  251. },
  252. /**
  253. * 用户点击右上角分享
  254. */
  255. onShareAppMessage() {
  256. return {
  257. title: '乐迷器乐AI',
  258. path: '/pages/index/index',
  259. imageUrl: 'https://oss.dayaedu.com/ktyq/1739773011143.png'
  260. }
  261. }
  262. })