order-detail.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid } from "../../api/login";
  3. import { formatPrice } from "../../utils/util";
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. status: 'ing',
  12. statusList: {
  13. ing: {
  14. logo: './images/ing.png',
  15. title: '等待付款',
  16. content: '请尽快完成支付,以便我们为您处理订单'
  17. },
  18. },
  19. backParams: null,
  20. goodsInfo: {} as any,
  21. hasInstrument: false, // 是否有乐器
  22. receiveAddress: '', // 选择的地址信息
  23. receiveAddressInfo: {
  24. addressDetail: '',
  25. name: '',
  26. phoneNumber: ''
  27. },
  28. userBeneficiaryId: '', // 添加购买人信息
  29. paymentType: null as any, // 支付类型
  30. paymentChannel: null as any,
  31. showService: false,
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad(options: any) {
  37. this.queryPayType()
  38. if (options.orderInfo) {
  39. console.log('goods', options)
  40. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  41. console.log(goods, 'goods', options)
  42. const infos = {
  43. allSalePrice: 0,
  44. allOriginPrice: 0,
  45. allDiscountPrice: '',
  46. discountIntegerPart: '',
  47. discountDecimalPart: '',
  48. integerPart: '',
  49. decimalPart: '',
  50. name: '',
  51. shopId: '',
  52. orderNo: options.orderNo || '',
  53. goodsList: [] as any
  54. }
  55. // 是否有乐器
  56. let hasInstrument = false
  57. for (let i in goods) {
  58. const item = goods[i]
  59. if (item.goodsType === "INSTRUMENTS") {
  60. hasInstrument = true
  61. }
  62. infos.name = infos.name ? infos.name + '+' + item.name : item.name
  63. infos.shopId = item.shopId
  64. const afterPrice: any = formatPrice(item.salePrice)
  65. // console.log(infos.goodsList, 'infos.goodsList')
  66. infos.goodsList.push({
  67. ...item,
  68. typePeriod: this.formatPeriod(item.num, item.period),
  69. ...afterPrice
  70. })
  71. infos.allSalePrice += Number(item.salePrice)
  72. infos.allOriginPrice += Number(item.originalPrice)
  73. }
  74. const allAfterPrice: any = formatPrice(infos.allSalePrice)
  75. // console.log(infos.allOriginPrice, infos.allSalePrice)
  76. infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string
  77. const allDiscount: any = formatPrice(infos.allOriginPrice - infos.allSalePrice)
  78. infos.integerPart = allAfterPrice.integerPart
  79. infos.decimalPart = allAfterPrice.decimalPart
  80. infos.discountIntegerPart = allDiscount.integerPart
  81. infos.discountDecimalPart = allDiscount.decimalPart
  82. // console.log(infos, 'infos')
  83. this.setData({
  84. goodsInfo: infos,
  85. userBeneficiaryId: options.userBeneficiaryId,
  86. status: options.status || '',
  87. hasInstrument
  88. });
  89. }
  90. },
  91. // 格式化类型
  92. formatPeriod(num: number, type: string) {
  93. if(!type) return ''
  94. const template: any = {
  95. DAY: "天",
  96. MONTH: "个月",
  97. YEAR: "年"
  98. }
  99. if (type === "YEAR" && num >= 99) {
  100. return '永久'
  101. }
  102. return num + (template[type] || '')
  103. },
  104. // 获取后台配置的支付方式
  105. async queryPayType() {
  106. try {
  107. // wxlite_payment_service_provider
  108. const { data } = await api_queryByParamName({
  109. paramName: app.globalData.appId
  110. });
  111. if (data.code == 200) {
  112. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  113. this.setData({
  114. paymentType: paramValue.vendor,
  115. paymentChannel: paramValue.channel
  116. });
  117. }
  118. } catch (error) {
  119. console.log(error, "error");
  120. }
  121. },
  122. /** 添加收货地址 */
  123. onSelectAddress() {
  124. wx.navigateTo({
  125. url: `../address/index?receiveAddress=${this.data.receiveAddress}`,
  126. })
  127. },
  128. onPayError(message?: string) {
  129. wx.hideLoading()
  130. wx.showToast({
  131. title: message || '支付取消',
  132. icon: 'none'
  133. })
  134. },
  135. // 购买
  136. async onSubmit() {
  137. if (!this.data.receiveAddress && this.data.hasInstrument) {
  138. wx.showToast({
  139. title: '请选择收货地址',
  140. icon: 'none'
  141. })
  142. return
  143. }
  144. wx.showLoading({
  145. mask: true,
  146. title: "订单提交中...",
  147. });
  148. try {
  149. const { allSalePrice, shopId, name, orderNo, goodsList } = this.data.goodsInfo
  150. const goodsInfos: any = []
  151. goodsList.forEach((item: any) => {
  152. goodsInfos.push({
  153. "goodsId": item.id,
  154. "goodsNum": 1,
  155. "goodsType": item.goodsType,
  156. "paymentCashAmount": item.salePrice,
  157. "paymentCouponAmount": 0
  158. })
  159. })
  160. if (orderNo) {
  161. const { data } = await api_userPaymentOrderUnpaid({
  162. orderNo: orderNo,
  163. paymentType: 'WECHAT_MINI'
  164. })
  165. if (data.code === 200) {
  166. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  167. this.onExecutePay(paymentConfig, paymentType, orderNo)
  168. } else {
  169. this.onPayError()
  170. }
  171. } else {
  172. const { data } = await api_executeOrder({
  173. "orderType": "WECHAT_MINI",
  174. "paymentType": this.data.paymentType,
  175. "paymentCashAmount": allSalePrice,
  176. "paymentCouponAmount": 0,
  177. "shopId": shopId,
  178. "openId": app.globalData.userInfo?.liteOpenid,
  179. "goodsInfos": goodsInfos,
  180. receiveAddress: this.data.receiveAddress,
  181. userBeneficiaryId: this.data.userBeneficiaryId,
  182. "orderName": name,
  183. "orderDesc": name
  184. })
  185. if (data.code === 200) {
  186. const { paymentConfig, paymentType, orderNo } = data.data
  187. this.onExecutePay(paymentConfig, paymentType, orderNo)
  188. } else if (data.code === 5200) {
  189. wx.hideLoading()
  190. wx.showToast({
  191. title: data.message,
  192. icon: 'none'
  193. })
  194. } else if([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  195. wx.hideLoading()
  196. wx.showToast({
  197. title: data.message,
  198. icon: 'none'
  199. })
  200. setTimeout(() => {
  201. wx.navigateBack()
  202. }, 1000);
  203. } else {
  204. this.onPayError()
  205. }
  206. }
  207. } catch {
  208. wx.hideLoading()
  209. }
  210. },
  211. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  212. wx.login({
  213. success: async (wxres: any) => {
  214. const res = await api_executePayment({
  215. merOrderNo: paymentConfig.merOrderNo,
  216. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  217. paymentType,
  218. userId: app.globalData.userInfo?.id,
  219. code: wxres.code,
  220. wxMiniAppId: app.globalData.appId
  221. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  222. // wxPubAppId: 'wxbde13f59d40cb4f2'
  223. })
  224. wx.hideLoading()
  225. if (res.data.code === 200) {
  226. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  227. } else {
  228. this.onPayError(res.data.message)
  229. }
  230. },
  231. fail: () => {
  232. this.onPayError()
  233. }
  234. })
  235. },
  236. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  237. const isYeePay = paymentType.indexOf('yeepay') !== -1
  238. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  239. : paymentConfig?.expend
  240. ? JSON.parse(paymentConfig?.expend?.pay_info)
  241. : paymentConfig
  242. const that = this
  243. wx.requestPayment({
  244. timeStamp: prePayInfo.timeStamp,
  245. nonceStr: prePayInfo.nonceStr,
  246. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  247. paySign: prePayInfo.paySign,
  248. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  249. success() {
  250. wx.showToast({ title: '支付成功', icon: 'success' });
  251. wx.redirectTo({
  252. url: '/pages/orders/order-result?orderNo=' + orderNo
  253. })
  254. },
  255. fail(ressonInfo) {
  256. console.log('支付失败', ressonInfo)
  257. that.onPayError()
  258. const goodsInfo = that.data.goodsInfo
  259. goodsInfo.orderNo = orderNo
  260. that.setData({
  261. goodsInfo
  262. })
  263. }
  264. })
  265. },
  266. /** 客服 */
  267. onService() {
  268. console.log("showService")
  269. this.setData({
  270. showService: true
  271. })
  272. },
  273. changePop(event: { detail: any }) {
  274. this.setData({
  275. showService: event.detail
  276. })
  277. },
  278. /**
  279. * 生命周期函数--监听页面初次渲染完成
  280. */
  281. onReady() {
  282. },
  283. /**
  284. * 生命周期函数--监听页面显示
  285. */
  286. onShow() {
  287. if (this.data.backParams) {
  288. console.log(this.data.backParams, 'backParams'); // { key: 'value' }
  289. const backParams: any = this.data.backParams || {};
  290. this.setData({
  291. receiveAddress: backParams.receiveAddress,
  292. receiveAddressInfo: backParams.receiveAddressInfo,
  293. backParams: null // 清空参数
  294. })
  295. }
  296. },
  297. /**
  298. * 生命周期函数--监听页面隐藏
  299. */
  300. onHide() {
  301. },
  302. /**
  303. * 生命周期函数--监听页面卸载
  304. */
  305. onUnload() {
  306. },
  307. /**
  308. * 页面相关事件处理函数--监听用户下拉动作
  309. */
  310. onPullDownRefresh() {
  311. },
  312. /**
  313. * 页面上拉触底事件的处理函数
  314. */
  315. onReachBottom() {
  316. },
  317. /**
  318. * 用户点击右上角分享
  319. */
  320. onShareAppMessage() {
  321. return {
  322. title: '器乐数字AI工具',
  323. path: '/pages/index/index',
  324. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  325. }
  326. }
  327. })