order.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <template>
  2. <view class="order-page client-theme">
  3. <view class="order-top sq-hero-header sq-hero-header--compact sq-hero-header--client">
  4. <view class="order-top-content">
  5. <text class="order-page-title">我的订单</text>
  6. <scroll-view scroll-x class="order-tabs-scroll" :show-scrollbar="false" :enable-flex="true">
  7. <view class="order-tabs-inner">
  8. <view
  9. v-for="tab in TAB_KEYS"
  10. :key="tab"
  11. class="tab-item"
  12. :class="{ active: activeTab === tab }"
  13. hover-class="tab-item--hover"
  14. :hover-stay-time="100"
  15. @click="switchTab(tab)"
  16. >
  17. <text>
  18. {{
  19. tab === 'all'
  20. ? '全部'
  21. : tab === 'pending_payment'
  22. ? '待支付'
  23. : tab === 'pending_confirm'
  24. ? '待收货'
  25. : tab === 'completed'
  26. ? '已完成'
  27. : '已取消'
  28. }}
  29. </text>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. </view>
  34. </view>
  35. <scroll-view
  36. class="order-list"
  37. scroll-y
  38. :enable-flex="true"
  39. :lower-threshold="100"
  40. @scrolltolower="onScrollToLower"
  41. >
  42. <ClientSkeleton
  43. v-if="activeTabState.loading && activeTabState.list.length === 0"
  44. variant="list"
  45. :rows="3"
  46. />
  47. <view
  48. v-else
  49. v-for="order in activeTabState.list"
  50. :key="order.id"
  51. class="order-card"
  52. hover-class="order-card--pressed"
  53. :hover-stay-time="120"
  54. @click="goToDetail(order.id)"
  55. >
  56. <view class="order-header">
  57. <view class="order-no-wrap">
  58. <image class="order-no-icon" :src="ICON_ORDER_NO" mode="aspectFit" />
  59. <text class="order-no">订单号:{{ order.orderNo }}</text>
  60. <SqCopyBtn class="order-no-copy" :text="order.orderNo" label="订单号" icon-only />
  61. </view>
  62. <view :class="['status-tag', 'status-' + getOrderStatusClass(order)]">
  63. <text>{{ getOrderStatusText(order) }}</text>
  64. </view>
  65. </view>
  66. <!-- 不用横向 scroll-view:H5 嵌套「纵 scroll-view + 横 scroll-view」时 uni 会写 scrollLeft,DOM 未就绪会报错 -->
  67. <view v-if="(order.items || []).length" class="product-scroll">
  68. <view class="product-scroll-inner">
  69. <view
  70. v-for="item in (order.items || [])"
  71. :key="item.id"
  72. class="product-card"
  73. >
  74. <ClientImageLoader
  75. class="product-image"
  76. :src="getItemImageUrl(item)"
  77. mode="aspectFill"
  78. radius="20rpx"
  79. />
  80. <view class="product-meta">
  81. <text class="product-name">{{ item.productName }}</text>
  82. <text class="product-qty">x{{ item.quantity }}</text>
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. <view class="order-footer">
  88. <text class="order-time">{{ formatDate(order.createAt) }}</text>
  89. <view class="order-amount">
  90. <text class="amount-count">共 {{ getOrderTotalQuantity(order) }} 件</text>
  91. <text class="sum-line">
  92. 合计:<text class="sum-value">¥{{ formatPrice(order.totalAmount) }}</text>
  93. </text>
  94. </view>
  95. </view>
  96. </view>
  97. <view v-if="activeTabState.list.length === 0 && !activeTabState.loading" class="empty-state">
  98. <image class="empty-image" :src="IMG_EMPTY" mode="aspectFit" />
  99. <text class="empty-text">暂无订单</text>
  100. </view>
  101. <view v-if="activeTabState.loading && activeTabState.list.length > 0" class="loading-more">
  102. <text>加载中...</text>
  103. </view>
  104. <view
  105. v-else-if="
  106. activeTabState.initialLoaded &&
  107. activeTabState.total > 0 &&
  108. (activeTabState.totalPages > 0
  109. ? activeTabState.page >= activeTabState.totalPages
  110. : activeTabState.list.length >= activeTabState.total)
  111. "
  112. class="nomore"
  113. >
  114. <text>没有更多了</text>
  115. </view>
  116. </scroll-view>
  117. <!-- #ifdef H5 -->
  118. <ClientTabBar active="order" />
  119. <!-- #endif -->
  120. </view>
  121. </template>
  122. <script setup>
  123. import { computed, reactive, ref } from 'vue'
  124. import { onShow } from '@dcloudio/uni-app'
  125. import { useUserStore } from '@/stores/user'
  126. import { TOKEN_KEY } from '@/config/api'
  127. import { request, parseResponseJson } from '@/utils/request'
  128. import { formatYuan } from '@/utils/format'
  129. import { formatDateTime } from '@/utils/date'
  130. import { getClientOrderUnifiedStatus } from '@/utils/client-order-status'
  131. import { IMG_DEFAULT_PRODUCT, IMG_EMPTY, staticAssetUrl } from '@/utils/staticAsset'
  132. import ClientTabBar from '@/components/client/client-tab-bar.vue'
  133. import ClientSkeleton from '@/components/client/client-skeleton.vue'
  134. import ClientImageLoader from '@/components/client/client-image-loader.vue'
  135. import SqCopyBtn from '@/components/sq-copy-btn.vue'
  136. const userStore = useUserStore()
  137. const ICON_ORDER_NO = staticAssetUrl('dingdanxinxi.svg')
  138. const TAB_KEYS = ['all', 'pending_payment', 'pending_confirm', 'completed', 'cancelled']
  139. const PAGE_SIZE = 10
  140. function emptyTabState() {
  141. return {
  142. list: [],
  143. page: 0,
  144. total: 0,
  145. totalPages: 0,
  146. loading: false,
  147. initialLoaded: false,
  148. needsRefresh: true,
  149. }
  150. }
  151. const tabStates = reactive({
  152. all: emptyTabState(),
  153. pending_payment: emptyTabState(),
  154. pending_confirm: emptyTabState(),
  155. completed: emptyTabState(),
  156. cancelled: emptyTabState(),
  157. })
  158. const activeTab = ref('all')
  159. const activeTabState = computed(() => tabStates[activeTab.value])
  160. function getUnifiedStatus(order) {
  161. return getClientOrderUnifiedStatus({
  162. cancelStatus: order.cancelStatus,
  163. orderStatus: order.orderStatus,
  164. deliveryStatus: order.deliveryStatus,
  165. payStatus: order.payStatus,
  166. status: order.status
  167. })
  168. }
  169. async function fetchTabPage(tab, reset) {
  170. const state = tabStates[tab]
  171. if (state.loading) return
  172. if (!reset) {
  173. if (!state.initialLoaded) return
  174. const tp = state.totalPages || (state.total > 0 ? Math.ceil(state.total / PAGE_SIZE) : 0)
  175. if (tp > 0 && state.page >= tp) return
  176. if (state.list.length >= state.total) return
  177. }
  178. if (reset) {
  179. state.list = []
  180. state.page = 0
  181. state.total = 0
  182. state.totalPages = 0
  183. state.initialLoaded = false
  184. }
  185. state.loading = true
  186. const nextPage = reset ? 1 : state.page + 1
  187. try {
  188. const token = uni.getStorageSync(TOKEN_KEY)
  189. if (!token) return
  190. const data = {
  191. page: nextPage,
  192. pageSize: PAGE_SIZE,
  193. }
  194. if (tab !== 'all') {
  195. data.listTab = tab
  196. data.orderStatus = tab
  197. }
  198. const res = await request({
  199. url: '/mini/client/orders',
  200. method: 'GET',
  201. data
  202. })
  203. const parsed = parseResponseJson(res)
  204. if (parsed.code !== 200) return
  205. const body = parsed.data || {}
  206. const list = body.list || []
  207. const total = Number(body?.total ?? 0)
  208. const totalPages = Number(body?.totalPages ?? 0)
  209. if (reset) {
  210. state.list = list
  211. } else {
  212. const ids = new Set(state.list.map((o) => o.id))
  213. for (const o of list) {
  214. if (o.id && !ids.has(o.id)) {
  215. state.list.push(o)
  216. ids.add(o.id)
  217. }
  218. }
  219. }
  220. state.page = nextPage
  221. state.total = total
  222. state.totalPages = totalPages
  223. state.initialLoaded = true
  224. state.needsRefresh = false
  225. } catch (error) {
  226. console.error('获取订单失败:', error)
  227. } finally {
  228. state.loading = false
  229. }
  230. }
  231. function onScrollToLower() {
  232. const tab = activeTab.value
  233. const s = tabStates[tab]
  234. if (!s.initialLoaded || s.loading) return
  235. const tp = s.totalPages || (s.total > 0 ? Math.ceil(s.total / PAGE_SIZE) : 0)
  236. if (tp > 0 && s.page >= tp) return
  237. if (s.list.length >= s.total) return
  238. void fetchTabPage(tab, false)
  239. }
  240. function switchTab(tab) {
  241. activeTab.value = tab
  242. if (!tabStates[tab].initialLoaded || tabStates[tab].needsRefresh) {
  243. void fetchTabPage(tab, true)
  244. }
  245. }
  246. function formatPrice(price) {
  247. return formatYuan(price)
  248. }
  249. function formatDate(dateStr) {
  250. return formatDateTime(dateStr)
  251. }
  252. function getOrderTotalQuantity(order) {
  253. if (typeof order.totalQuantity === 'number') return order.totalQuantity
  254. const items = order.items
  255. if (Array.isArray(items) && items.length) {
  256. const sum = items.reduce((acc, it) => acc + (Number(it.quantity) || 0), 0)
  257. return sum || items.length
  258. }
  259. if (typeof order.itemCount === 'number') return order.itemCount
  260. return 0
  261. }
  262. function getItemImageUrl(item) {
  263. return item?.imageUrl || IMG_DEFAULT_PRODUCT
  264. }
  265. function getStatusText(status) {
  266. const statusMap = {
  267. pending_payment: '待支付',
  268. pending_review_cancel: '待审核取消',
  269. pending_delivery: '待分配配送',
  270. delivering: '配送中',
  271. partial_delivered: '部分送达',
  272. pending_confirm: '待确认收货',
  273. completed: '已完成',
  274. cancelled: '已取消',
  275. refunding: '退款中',
  276. }
  277. return statusMap[status] || status
  278. }
  279. function getStatusClass(status) {
  280. const classMap = {
  281. pending_payment: 'warning',
  282. pending_review_cancel: 'warning',
  283. pending_delivery: 'primary',
  284. delivering: 'primary',
  285. partial_delivered: 'warning',
  286. pending_confirm: 'primary',
  287. completed: 'success',
  288. cancelled: 'info',
  289. }
  290. return classMap[status] || 'info'
  291. }
  292. function getOrderStatusText(order) {
  293. return getStatusText(getUnifiedStatus(order))
  294. }
  295. function getOrderStatusClass(order) {
  296. return getStatusClass(getUnifiedStatus(order))
  297. }
  298. function goToDetail(orderId) {
  299. uni.navigateTo({ url: `/pages/client/order/detail/detail?id=${orderId}` })
  300. }
  301. function ensureClientAndLoad() {
  302. userStore.checkLoginStatus()
  303. if (!userStore.isLoggedIn) {
  304. uni.reLaunch({ url: '/pages/login/login' })
  305. return false
  306. }
  307. if (!userStore.isClient) {
  308. uni.showToast({ title: '请使用客户账号登录', icon: 'none' })
  309. setTimeout(() => userStore.navigateToHome(), 1500)
  310. return false
  311. }
  312. return true
  313. }
  314. // 首次进入、从详情/支付返回时刷新当前 Tab(避免与 onMounted 重复请求)
  315. onShow(() => {
  316. if (!ensureClientAndLoad()) return
  317. TAB_KEYS.forEach((k) => {
  318. tabStates[k].needsRefresh = true
  319. })
  320. void fetchTabPage(activeTab.value, true)
  321. })
  322. </script>
  323. <style lang="scss" scoped>
  324. .order-page {
  325. height: 100%;
  326. min-height: 0;
  327. display: flex;
  328. flex-direction: column;
  329. background: var(--c-bg);
  330. overflow: hidden;
  331. box-sizing: border-box;
  332. }
  333. .order-top {
  334. flex-shrink: 0;
  335. overflow: hidden;
  336. }
  337. .order-top-content {
  338. padding: 8rpx 0 8rpx 0;
  339. }
  340. .order-page-title {
  341. display: block;
  342. font-size: 36rpx;
  343. font-weight: 700;
  344. color: #ffffff;
  345. margin-bottom: 20rpx;
  346. line-height: 1.25;
  347. }
  348. .order-tabs-scroll {
  349. width: 100%;
  350. white-space: nowrap;
  351. }
  352. .order-tabs-inner {
  353. display: inline-flex;
  354. align-items: center;
  355. gap: 16rpx;
  356. padding: 4rpx 0 2rpx;
  357. box-sizing: border-box;
  358. }
  359. .tab-item {
  360. flex-shrink: 0;
  361. min-height: 68rpx;
  362. padding: 0 24rpx;
  363. display: inline-flex;
  364. align-items: center;
  365. justify-content: center;
  366. font-size: 24rpx;
  367. color: rgba(255, 255, 255, 0.95);
  368. border: 1rpx solid rgba(255, 255, 255, 0.35);
  369. border-radius: 34rpx;
  370. box-sizing: border-box;
  371. transition: opacity 0.15s ease;
  372. &.active {
  373. background: #ffffff;
  374. color: var(--c-brand-green);
  375. font-weight: 600;
  376. border-color: #ffffff;
  377. }
  378. }
  379. .tab-item--hover {
  380. opacity: 0.88;
  381. }
  382. .order-list {
  383. flex: 1;
  384. flex-basis: 0;
  385. min-height: 0;
  386. height: 0;
  387. padding: 24rpx 32rpx 32rpx;
  388. width: 100%;
  389. box-sizing: border-box;
  390. overflow-x: hidden;
  391. }
  392. .order-card {
  393. background: var(--c-card);
  394. border-radius: 28rpx;
  395. padding: 24rpx;
  396. margin-bottom: 20rpx;
  397. border: 1rpx solid var(--c-border);
  398. box-shadow: none;
  399. width: 100%;
  400. max-width: 100%;
  401. box-sizing: border-box;
  402. transition: opacity 0.15s ease;
  403. .order-header {
  404. display: flex;
  405. justify-content: space-between;
  406. align-items: center;
  407. margin-bottom: 16rpx;
  408. .order-no-wrap {
  409. flex: 0 1 auto;
  410. min-width: 0;
  411. display: flex;
  412. align-items: center;
  413. gap: 4rpx;
  414. margin-right: 16rpx;
  415. max-width: calc(100% - 170rpx);
  416. }
  417. .order-no {
  418. flex: 0 1 auto;
  419. min-width: 0;
  420. font-size: 26rpx;
  421. font-weight: 600;
  422. line-height: 1.2;
  423. color: var(--c-text);
  424. overflow: hidden;
  425. text-overflow: ellipsis;
  426. white-space: nowrap;
  427. width: auto;
  428. max-width: 100%;
  429. }
  430. .order-no-icon {
  431. width: 32rpx;
  432. height: 32rpx;
  433. flex: 0 0 auto;
  434. }
  435. .order-no-copy {
  436. flex: 0 0 auto;
  437. display: inline-flex;
  438. align-items: center;
  439. align-self: center;
  440. margin-left: 10rpx;
  441. background: transparent !important;
  442. border: none !important;
  443. box-shadow: none !important;
  444. padding: 0 !important;
  445. width: 32rpx !important;
  446. height: 32rpx !important;
  447. min-width: 32rpx !important;
  448. min-height: 32rpx !important;
  449. border-radius: 0 !important;
  450. justify-content: center;
  451. }
  452. /* 覆盖 SqCopyBtn 组件 icon-only 默认尺寸 */
  453. .order-no-copy :deep(.sq-copy-btn__icon) {
  454. width: 22rpx !important;
  455. height: 22rpx !important;
  456. }
  457. .status-tag {
  458. padding: 0 20rpx;
  459. min-height: 48rpx;
  460. border-radius: 24rpx;
  461. font-size: 22rpx;
  462. display: inline-flex;
  463. align-items: center;
  464. &.status-primary {
  465. background: #fff7e2;
  466. color: #b7791f;
  467. }
  468. &.status-warning {
  469. background: #fffbeb;
  470. color: #d97706;
  471. }
  472. &.status-success {
  473. background: #ecfdf5;
  474. color: #059669;
  475. }
  476. &.status-info {
  477. background: #f1f5f9;
  478. color: #64748b;
  479. }
  480. }
  481. }
  482. .product-scroll {
  483. margin-top: 0;
  484. padding-bottom: 16rpx;
  485. width: 100%;
  486. overflow-x: auto;
  487. overflow-y: hidden;
  488. -webkit-overflow-scrolling: touch;
  489. .product-scroll-inner {
  490. display: flex;
  491. flex-direction: row;
  492. flex-wrap: nowrap;
  493. gap: 16rpx;
  494. padding: 0 2rpx;
  495. width: max-content;
  496. max-width: none;
  497. }
  498. .product-card {
  499. width: 132rpx;
  500. height: 132rpx;
  501. border-radius: 20rpx;
  502. overflow: hidden;
  503. background: #f8fcfa;
  504. border: 1rpx solid rgba(220, 235, 228, 0.65);
  505. flex: 0 0 auto;
  506. position: relative;
  507. }
  508. .product-image {
  509. width: 100%;
  510. height: 100%;
  511. background: linear-gradient(145deg, #f4fbf8, #e4f6ef);
  512. display: block;
  513. }
  514. .product-meta {
  515. display: flex;
  516. align-items: center;
  517. padding: 8rpx 10rpx;
  518. gap: 6rpx;
  519. position: absolute;
  520. left: 0;
  521. right: 0;
  522. bottom: 0;
  523. background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.35) 100%);
  524. }
  525. .product-name {
  526. flex: 1;
  527. min-width: 0;
  528. font-size: 24rpx;
  529. color: #ffffff;
  530. overflow: hidden;
  531. text-overflow: ellipsis;
  532. white-space: nowrap;
  533. }
  534. .product-qty {
  535. font-size: 22rpx;
  536. color: rgba(255, 255, 255, 0.92);
  537. flex: 0 0 auto;
  538. }
  539. }
  540. .order-footer {
  541. display: flex;
  542. justify-content: space-between;
  543. align-items: flex-end;
  544. margin-top: 0;
  545. padding-top: 16rpx;
  546. border-top: 1rpx solid #edf5f1;
  547. .order-time {
  548. font-size: 22rpx;
  549. color: var(--c-muted);
  550. line-height: 1.45;
  551. max-width: 62%;
  552. }
  553. .order-amount {
  554. display: flex;
  555. flex-direction: column;
  556. align-items: flex-end;
  557. gap: 4rpx;
  558. .amount-count {
  559. font-size: 22rpx;
  560. color: var(--c-muted);
  561. }
  562. .sum-line {
  563. font-size: 24rpx;
  564. color: var(--c-muted);
  565. line-height: 1.4;
  566. }
  567. .sum-value {
  568. color: var(--c-danger);
  569. font-weight: 700;
  570. font-size: 26rpx;
  571. }
  572. }
  573. }
  574. }
  575. .order-card--pressed {
  576. opacity: 0.94;
  577. }
  578. .empty-state {
  579. display: flex;
  580. flex-direction: column;
  581. align-items: center;
  582. justify-content: center;
  583. width: 100%;
  584. padding: 72rpx 0;
  585. .empty-image {
  586. width: 160rpx;
  587. height: 160rpx;
  588. margin-bottom: 12rpx;
  589. }
  590. .empty-text {
  591. width: 100%;
  592. display: block;
  593. text-align: center;
  594. font-size: 28rpx;
  595. color: #909399;
  596. }
  597. }
  598. .loading-state,
  599. .loading-more,
  600. .nomore {
  601. display: flex;
  602. align-items: center;
  603. justify-content: center;
  604. padding: 32rpx 0;
  605. color: #909399;
  606. font-size: 26rpx;
  607. }
  608. </style>
  609. <style lang="scss">
  610. /* 与 disableScroll 配合:本页根节点占满导航栏以下区域,内部仅 scroll-view 纵向滚动 */
  611. page {
  612. height: 100%;
  613. }
  614. </style>