index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <template>
  2. <div class="store-page order-detail-page">
  3. <div class="order-detail-page__inner app-page__inner--wide">
  4. <Breadcrumb />
  5. <a-spin :loading="loading" class="store-spin">
  6. <div class="detail-stack">
  7. <!-- 待支付横幅 -->
  8. <div v-if="data?.state === 0 && hasPay" class="pay-banner">
  9. <div class="pay-banner__info">
  10. <icon-clock-circle class="pay-banner__icon" />
  11. <div>
  12. <div class="pay-banner__title">等待支付</div>
  13. <div class="pay-banner__desc">{{ paymentCountdownText }}</div>
  14. </div>
  15. </div>
  16. <div class="pay-banner__actions">
  17. <a-button type="primary" size="large" class="pay-banner__btn" @click="pay">
  18. 立即支付 ¥{{ data?.price }}
  19. </a-button>
  20. <a-button
  21. size="large"
  22. class="pay-banner__btn"
  23. :loading="cancelLoading"
  24. @click="handleCancelOrder"
  25. >
  26. 取消订单
  27. </a-button>
  28. </div>
  29. </div>
  30. <a-card :bordered="false" class="status-card">
  31. <OrderProgressSteps
  32. class="steps"
  33. :mobile="isMobile"
  34. :current="stepCurrent"
  35. :status="stepStatus"
  36. :labels="stepLabels"
  37. :descriptions="stepDescriptions"
  38. />
  39. <div class="status-badge-wrap">
  40. <OrderStateTag :state="data?.state ?? 0" />
  41. </div>
  42. </a-card>
  43. <a-card :bordered="false" class="info-card" title="订单信息">
  44. <a-descriptions :column="1" bordered size="medium">
  45. <a-descriptions-item label="商品名称">{{ data?.name || '-' }}</a-descriptions-item>
  46. <a-descriptions-item v-if="data?.original_price && data?.discount_amount > 0" label="商品原价">
  47. ¥{{ data.original_price }}
  48. </a-descriptions-item>
  49. <a-descriptions-item v-if="data?.coupon_code" label="优惠码">
  50. {{ data.coupon_code }}
  51. <span v-if="data?.discount_amount > 0" class="discount-tag">-¥{{ data.discount_amount }}</span>
  52. </a-descriptions-item>
  53. <a-descriptions-item label="支付金额">
  54. <span class="price-text">¥{{ data?.price }}</span>
  55. </a-descriptions-item>
  56. <a-descriptions-item label="支付方式">
  57. <span class="pay-row">
  58. <icon-wechatpay v-if="data?.pay_type === 'wxpay'" />
  59. <icon-alipay-circle v-else-if="data?.pay_type === 'alipay'" />
  60. {{ getPayTypeLabel(data?.pay_type) }}
  61. </span>
  62. </a-descriptions-item>
  63. <a-descriptions-item label="订单号">{{ data?.orderId }}</a-descriptions-item>
  64. <a-descriptions-item v-if="data?.pay_id" label="支付平台单号">{{ data.pay_id }}</a-descriptions-item>
  65. <a-descriptions-item label="下单时间">{{ formatStoreTimeFull(data?.create_time) }}</a-descriptions-item>
  66. <a-descriptions-item v-if="data?.pay_time" label="支付时间">
  67. {{ formatStoreTimeFull(data.pay_time) }}
  68. </a-descriptions-item>
  69. </a-descriptions>
  70. </a-card>
  71. <a-card v-if="content" :bordered="false" class="info-card" title="商品说明">
  72. <div class="rich-content" v-html="content" />
  73. </a-card>
  74. <div class="detail-actions">
  75. <a-button
  76. v-if="data?.state === 0"
  77. status="danger"
  78. :loading="cancelLoading"
  79. @click="handleCancelOrder"
  80. >
  81. 取消订单
  82. </a-button>
  83. <a-button @click="$router.push('/store/myOrder')">返回订单列表</a-button>
  84. <a-button type="outline" @click="$router.push('/store/goodsList')">继续购物</a-button>
  85. </div>
  86. </div>
  87. </a-spin>
  88. </div>
  89. </div>
  90. </template>
  91. <script setup>
  92. import { ref, computed, onUnmounted, onMounted } from 'vue'
  93. import { orderDeatil, cancelOrder } from '@/api/order'
  94. import { useRoute } from 'vue-router'
  95. import { Notification, Message, Modal } from '@arco-design/web-vue'
  96. import OrderStateTag from '@/components/store/OrderStateTag.vue'
  97. import OrderProgressSteps from '@/components/store/OrderProgressSteps.vue'
  98. import {
  99. formatStoreTimeFull,
  100. getPayTypeLabel,
  101. decodeGoodsContent,
  102. hasPayData
  103. } from '@/utils/storeFormat'
  104. const route = useRoute()
  105. const { id } = route.params
  106. const loading = ref(true)
  107. const cancelLoading = ref(false)
  108. const data = ref({})
  109. const payData = ref({})
  110. const content = ref('')
  111. const paymentCountdownText = ref('')
  112. const isMobile = ref(false)
  113. const hasPay = computed(() => hasPayData(payData.value))
  114. const stepCurrent = ref(1)
  115. const stepStatus = ref('process')
  116. const stepLabels = ref(['待支付', '待处理', '已完成'])
  117. const stepDescriptions = ref(['', '', ''])
  118. const PAY_TIMEOUT_SEC = 300
  119. let pollTimer = null
  120. let countdownTimer = null
  121. let resizeHandler = null
  122. const updateStepState = () => {
  123. const state = data.value?.state
  124. stepDescriptions.value = ['', '', '']
  125. switch (state) {
  126. case 0:
  127. stepCurrent.value = 1
  128. stepStatus.value = 'process'
  129. stepLabels.value = ['待支付', '待处理', '已完成']
  130. updatePaymentCountdown()
  131. startCountdownTicker()
  132. break
  133. case 1:
  134. stepCurrent.value = 2
  135. stepStatus.value = 'process'
  136. stepDescriptions.value[1] = '支付成功,等待系统处理'
  137. stopPendingOrderTimers()
  138. break
  139. case 2:
  140. stepCurrent.value = 3
  141. stepStatus.value = 'finish'
  142. stepDescriptions.value[2] = '权益已发放至账户'
  143. stopPendingOrderTimers()
  144. break
  145. case 3:
  146. stepCurrent.value = 1
  147. stepStatus.value = 'error'
  148. stepLabels.value = ['已关闭', '待处理', '已完成']
  149. stepDescriptions.value[0] = '订单超时或已取消'
  150. stopPendingOrderTimers()
  151. break
  152. default:
  153. break
  154. }
  155. }
  156. const updatePaymentCountdown = () => {
  157. const ts = data.value?.create_time
  158. if (!ts) {
  159. paymentCountdownText.value = '请在规定时间内完成支付'
  160. return
  161. }
  162. const diffSec = Math.floor((Date.now() - ts) / 1000)
  163. if (diffSec > PAY_TIMEOUT_SEC) {
  164. paymentCountdownText.value = '支付已超时,订单可能已关闭'
  165. stepDescriptions.value[0] = '支付超时'
  166. return
  167. }
  168. const remaining = PAY_TIMEOUT_SEC - diffSec
  169. const m = Math.floor(remaining / 60)
  170. const s = String(remaining % 60).padStart(2, '0')
  171. paymentCountdownText.value = `请在 ${m} 分 ${s} 秒内完成支付`
  172. stepDescriptions.value[0] = paymentCountdownText.value
  173. }
  174. const getOrderDetail = async () => {
  175. try {
  176. const res = await orderDeatil({ orderId: id })
  177. if (!res || res.code !== 0) {
  178. return Notification.error({
  179. title: '获取订单详情失败',
  180. content: res?.msg ?? '请稍后再试'
  181. })
  182. }
  183. data.value = res.data ?? {}
  184. content.value = decodeGoodsContent(res.data?.content)
  185. payData.value = res.payData && hasPayData(res.payData) ? res.payData : {}
  186. updateStepState()
  187. } catch (error) {
  188. Notification.error({
  189. title: '获取订单详情失败',
  190. content: error.message || '请稍后再试'
  191. })
  192. }
  193. }
  194. const startCountdownTicker = () => {
  195. if (countdownTimer) return
  196. countdownTimer = setInterval(() => {
  197. if (data.value?.state !== 0) {
  198. stopCountdownTicker()
  199. return
  200. }
  201. updatePaymentCountdown()
  202. }, 1000)
  203. }
  204. const stopCountdownTicker = () => {
  205. if (countdownTimer) {
  206. clearInterval(countdownTimer)
  207. countdownTimer = null
  208. }
  209. }
  210. const startPolling = () => {
  211. if (pollTimer) return
  212. pollTimer = setInterval(async () => {
  213. if (data.value?.state !== 0) {
  214. stopPolling()
  215. return
  216. }
  217. await getOrderDetail()
  218. }, 3000)
  219. }
  220. const stopPolling = () => {
  221. if (pollTimer) {
  222. clearInterval(pollTimer)
  223. pollTimer = null
  224. }
  225. }
  226. const stopPendingOrderTimers = () => {
  227. stopCountdownTicker()
  228. stopPolling()
  229. }
  230. onMounted(async () => {
  231. const syncMobile = () => {
  232. isMobile.value = window.innerWidth <= 768
  233. }
  234. resizeHandler = syncMobile
  235. syncMobile()
  236. window.addEventListener('resize', resizeHandler)
  237. loading.value = true
  238. await getOrderDetail()
  239. loading.value = false
  240. if (data.value?.state === 0) startPolling()
  241. })
  242. onUnmounted(() => {
  243. stopPendingOrderTimers()
  244. if (resizeHandler) window.removeEventListener('resize', resizeHandler)
  245. })
  246. const pay = () => {
  247. if (!hasPay.value) {
  248. Message.warning('暂无支付信息,请刷新页面重试')
  249. return
  250. }
  251. Message.success('正在跳转支付页面,请在新页面完成支付')
  252. openPaymentWindow(payData.value.payUrl, payData.value.payData)
  253. }
  254. const handleCancelOrder = () => {
  255. Modal.confirm({
  256. title: '取消订单',
  257. content: '确定要取消该订单吗?取消后需重新下单购买。',
  258. okText: '确认取消',
  259. cancelText: '再想想',
  260. okButtonProps: { status: 'danger' },
  261. onOk: async () => {
  262. try {
  263. cancelLoading.value = true
  264. const res = await cancelOrder({ orderId: id })
  265. if (!res || res.code !== 0) {
  266. Notification.error({
  267. title: '取消订单失败',
  268. content: res?.msg ?? '请稍后再试'
  269. })
  270. return
  271. }
  272. Message.success(res.msg || '订单已取消')
  273. payData.value = {}
  274. stopPendingOrderTimers()
  275. await getOrderDetail()
  276. } catch (error) {
  277. Notification.error({
  278. title: '取消订单失败',
  279. content: error.message || '请稍后再试'
  280. })
  281. } finally {
  282. cancelLoading.value = false
  283. }
  284. }
  285. })
  286. }
  287. function openPaymentWindow(payUrl, formData) {
  288. const form = document.createElement('form')
  289. form.method = 'POST'
  290. form.action = payUrl
  291. form.style.display = 'none'
  292. for (const key in formData) {
  293. if (Object.prototype.hasOwnProperty.call(formData, key)) {
  294. const input = document.createElement('input')
  295. input.type = 'hidden'
  296. input.name = key
  297. input.value = formData[key]
  298. form.appendChild(input)
  299. }
  300. }
  301. document.body.appendChild(form)
  302. form.submit()
  303. document.body.removeChild(form)
  304. }
  305. </script>
  306. <style lang="less" scoped>
  307. @import '@/styles/store-theme.less';
  308. .detail-stack {
  309. display: flex;
  310. flex-direction: column;
  311. gap: 16px;
  312. width: 100%;
  313. }
  314. .pay-banner {
  315. display: flex;
  316. flex-wrap: wrap;
  317. align-items: center;
  318. justify-content: space-between;
  319. gap: 16px;
  320. padding: 20px 24px;
  321. background: linear-gradient(135deg, #fff7e6 0%, #ffe7ba 100%);
  322. border: 1px solid #ffd591;
  323. border-radius: @store-radius;
  324. &__info {
  325. display: flex;
  326. align-items: center;
  327. gap: 12px;
  328. }
  329. &__icon {
  330. font-size: 32px;
  331. color: rgb(var(--orange-6));
  332. }
  333. &__title {
  334. font-weight: 600;
  335. font-size: 1.1rem;
  336. color: @store-primary;
  337. }
  338. &__desc {
  339. font-size: 0.85rem;
  340. color: @store-text-muted;
  341. margin-top: 4px;
  342. }
  343. &__actions {
  344. display: flex;
  345. flex-wrap: wrap;
  346. gap: 10px;
  347. flex-shrink: 0;
  348. }
  349. &__btn {
  350. border-radius: 999px;
  351. flex-shrink: 0;
  352. }
  353. }
  354. .status-card,
  355. .info-card {
  356. border-radius: @store-radius;
  357. border: 1px solid @store-card-border;
  358. box-shadow: @store-shadow;
  359. }
  360. .status-badge-wrap {
  361. text-align: center;
  362. }
  363. .price-text {
  364. font-size: 1.15rem;
  365. font-weight: 700;
  366. color: @store-price;
  367. }
  368. .discount-tag {
  369. margin-left: 8px;
  370. color: rgb(var(--green-6));
  371. font-size: 0.9rem;
  372. }
  373. .pay-row {
  374. display: inline-flex;
  375. align-items: center;
  376. gap: 6px;
  377. }
  378. .rich-content {
  379. line-height: 1.75;
  380. color: @store-text-muted;
  381. :deep(img) {
  382. max-width: 100%;
  383. }
  384. }
  385. .detail-actions {
  386. display: flex;
  387. gap: 12px;
  388. flex-wrap: wrap;
  389. margin-top: 8px;
  390. }
  391. @media (max-width: 768px) {
  392. .pay-banner {
  393. padding: 14px 12px;
  394. }
  395. .detail-actions {
  396. display: grid;
  397. grid-template-columns: 1fr;
  398. }
  399. .detail-actions :deep(.arco-btn) {
  400. width: 100%;
  401. }
  402. }
  403. </style>