index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <template>
  2. <!-- 订单列表 -->
  3. <view class="content">
  4. <!-- 搜索栏 -->
  5. <view class="search-container">
  6. <view class="search-box">
  7. <image :src="getFileUrl('img/personal/search.png')" mode="widthFix" class="search-icon"></image>
  8. <input type="text" placeholder="搜索" class="search-input" v-model="searchKeyword"
  9. maxlength="15" @confirm="doSearch" />
  10. </view>
  11. </view>
  12. <!-- 标签页 -->
  13. <view class="tabs">
  14. <view class="tab-item" :class="{ active: activeTab === 'current' }" @click="switchTab('current')">
  15. 当前订单
  16. </view>
  17. <view class="tab-item" :class="{ active: activeTab === 'history' }" @click="switchTab('history')">
  18. 历史订单
  19. </view>
  20. </view>
  21. <!-- 订单列表 -->
  22. <scroll-view class="order-list" scroll-y @scrolltolower="handleScrollToLower">
  23. <view v-for="(order, index) in currentOrderList" :key="order.id || index" class="order-card" @click="handleOrderDetail(order)">
  24. <!-- 订单头部 -->
  25. <view class="order-header">
  26. <view class="order-number">
  27. <image src="/static/number.png" mode="aspectFit" class="order-number-img"></image>
  28. <text>{{ order.orderNo }}</text>
  29. </view>
  30. </view>
  31. <!-- 店铺信息 -->
  32. <view class="store-info">
  33. <view><text class="store-name">{{ order.storeName }}</text>
  34. <text class="store-arrow">›</text>
  35. </view>
  36. <view class="order-status" :class="order.statusClass">{{ order.statusText }}</view>
  37. </view>
  38. <!-- 订单时间 -->
  39. <view class="order-time">{{ order.createTime }}</view>
  40. <!-- 商品信息 -->
  41. <view class="order-goods" >
  42. <view class="goods-images">
  43. <image v-for="(image, imgIndex) in order.goodsImages" :key="imgIndex" :src="getFileUrl(image)"
  44. mode="aspectFill" class="goods-image" />
  45. </view>
  46. <view class="goods-info">
  47. <view class="goods-price">¥{{ order.totalPrice }}</view>
  48. <view class="goods-count">共{{ order.goodsCount }}件</view>
  49. </view>
  50. </view>
  51. <!-- 操作按钮:已完成(3)不显示 -->
  52. <view class="order-actions" v-if="order.orderStatus !== 3">
  53. <view class="action-btn outline" @click="handleAddFood(order)">
  54. 去加餐
  55. </view>
  56. <view class="action-btn primary" @click="handleCheckout(order)">
  57. 去结算
  58. </view>
  59. </view>
  60. </view>
  61. <!-- 空状态 -->
  62. <view v-if="currentOrderList.length === 0" class="empty-state">
  63. <text class="empty-text">暂无订单</text>
  64. </view>
  65. </scroll-view>
  66. </view>
  67. </template>
  68. <script setup>
  69. import { onLoad } from "@dcloudio/uni-app";
  70. import { ref, computed, watch } from "vue";
  71. import { getFileUrl } from "@/utils/file.js";
  72. import { go } from "@/utils/utils.js";
  73. import { navigateToAddFood } from "@/utils/orderAddFoodNavigate.js";
  74. import { GetOrderPage } from "@/api/dining.js";
  75. const activeTab = ref('current');
  76. const searchKeyword = ref('');
  77. // 分页参数
  78. const pageSize = 10;
  79. const currentPage = ref({ current: 1, history: 1 });
  80. const loading = ref(false);
  81. const noMore = ref({ current: false, history: false });
  82. // 取第一张图:逗号分隔取首段,数组取首项,对象取 url/path/src(与 orderFood/ FoodCard 一致)
  83. function firstImage(val) {
  84. if (val == null || val === '') return '';
  85. if (Array.isArray(val)) {
  86. const first = val[0];
  87. if (first != null && first !== '') {
  88. if (typeof first === 'object' && first !== null) return first.url ?? first.path ?? first.src ?? first.link ?? '';
  89. return String(first).split(/[,,]/)[0].trim();
  90. }
  91. return '';
  92. }
  93. if (typeof val === 'object') return val.url ?? val.path ?? val.src ?? val.link ?? '';
  94. const str = String(val).trim();
  95. return str ? str.split(/[,,]/)[0].trim() : '';
  96. }
  97. // records 每项:order 为订单信息,cuisineItems 为菜品数组;兼容直接平铺的旧结构
  98. function normalizeOrder(record) {
  99. const order = record?.order ?? record;
  100. const cuisineItems = record?.cuisineItems ?? [];
  101. const items = Array.isArray(cuisineItems) ? cuisineItems : [];
  102. const orderStatus = order?.orderStatus ?? order?.status;
  103. const payStatus = order?.payStatus;
  104. const statusText = getDisplayStatusText(payStatus, orderStatus, order?.statusText ?? order?.orderStatusName);
  105. const statusClass = getDisplayStatusClass(payStatus, orderStatus);
  106. const goodsCount = items.reduce((sum, it) => sum + (Number(it?.quantity) || 0), 0) || order?.dinerCount || 0;
  107. const goodsImages = items
  108. .map((it) => {
  109. const raw = it?.cuisineImage ?? it?.image ?? it?.imageUrl ?? it?.pic ?? it?.cover ?? it?.images ?? '';
  110. return firstImage(raw) || (typeof raw === 'string' ? raw.split(/[,,]/)[0]?.trim() : '');
  111. })
  112. .filter((url) => url && String(url).trim());
  113. // storeName 取自 data.records[].order.storeName
  114. return {
  115. id: order?.id,
  116. orderNo: order?.orderNo ?? order?.orderNumber ?? order?.orderId ?? '',
  117. storeId: order?.storeId,
  118. storeName: order?.storeName ?? order?.store_name ?? record?.storeName ?? record?.store_name ?? '—',
  119. tableId: order?.tableId ?? order?.storeTableId ?? order?.diningTableId,
  120. tableNumber: order?.tableNumber ?? order?.tableNo ?? '—',
  121. contactPhone: order?.contactPhone ?? '',
  122. createTime: order?.createdTime ?? order?.createTime ?? order?.orderTime ?? '—',
  123. totalPrice: order?.payAmount ?? order?.totalPrice ?? order?.totalAmount ?? 0,
  124. goodsCount,
  125. statusText,
  126. statusClass,
  127. orderStatus,
  128. payStatus,
  129. dinerCount: order?.dinerCount,
  130. discountAmount: order?.discountAmount ?? 0,
  131. remark: order?.remark,
  132. goodsImages,
  133. cuisineItems: items
  134. };
  135. }
  136. // orderStatus 订单状态:0 待支付,1 已支付,2 已取消,3 已完成
  137. function getDisplayStatusText(payStatus, orderStatus, fallback) {
  138. if (fallback) return fallback;
  139. return getOrderStatusText(orderStatus);
  140. }
  141. function getOrderStatusText(status) {
  142. if (status == null && status !== 0) return '—';
  143. const n = Number(status);
  144. if (n === 0) return '待支付';
  145. if (n === 1) return '已支付';
  146. if (n === 2) return '已取消';
  147. if (n === 3) return '已完成';
  148. return String(status);
  149. }
  150. function getDisplayStatusClass(payStatus, orderStatus) {
  151. return getStatusClass(orderStatus);
  152. }
  153. function getStatusClass(status) {
  154. if (status == null && status !== 0) return '';
  155. const n = Number(status);
  156. if (n === 0) return 'status-unpaid'; // 待支付
  157. if (n === 1) return 'status-paid'; // 已支付
  158. if (n === 2) return 'status-cancelled'; // 已取消
  159. if (n === 3) return 'status-completed'; // 已完成
  160. return 'status-unpaid';
  161. }
  162. const currentOrders = ref([]);
  163. const historyOrders = ref([]);
  164. // 当前显示的订单列表(由接口返回,搜索由接口 keyword 参数完成)
  165. const currentOrderList = computed(() => {
  166. return activeTab.value === 'current' ? currentOrders.value : historyOrders.value;
  167. });
  168. // orderStatus:0=进行中(待支付+已支付),3=已完成
  169. const getOrderStatusByTab = (tab) => (tab === 'current' ? 0 : 3);
  170. async function loadOrderList(tab, append = false) {
  171. const pageKey = tab === 'current' ? 'current' : 'history';
  172. if (loading.value) return;
  173. if (append && noMore.value[pageKey]) return;
  174. if (!append) {
  175. currentPage.value[pageKey] = 1;
  176. noMore.value[pageKey] = false;
  177. }
  178. const page = currentPage.value[pageKey];
  179. if (append && page === 1) return;
  180. loading.value = true;
  181. try {
  182. const keyword = (searchKeyword.value || '').trim().slice(0, 15);
  183. const params = {
  184. current: page,
  185. size: pageSize,
  186. orderStatus: getOrderStatusByTab(tab)
  187. };
  188. if (keyword) params.keyword = keyword;
  189. const sid = storeIdRef.value;
  190. if (sid != null && sid !== '') params.storeId = Number(sid);
  191. const res = await GetOrderPage(params);
  192. // /store/order/my-orders:返回 data,可能为 { records, total } 或 { data: { records, total } }
  193. const raw = res && typeof res === 'object' ? res : {};
  194. const list = Array.isArray(raw.records)
  195. ? raw.records
  196. : Array.isArray(raw.data?.records)
  197. ? raw.data.records
  198. : Array.isArray(raw.list)
  199. ? raw.list
  200. : [];
  201. const normalized = list.map(normalizeOrder);
  202. if (tab === 'current') {
  203. currentOrders.value = append ? [...currentOrders.value, ...normalized] : normalized;
  204. } else {
  205. historyOrders.value = append ? [...historyOrders.value, ...normalized] : normalized;
  206. }
  207. const total =
  208. raw.total != null ? Number(raw.total) : (raw.data?.total != null ? Number(raw.data.total) : 0);
  209. if (normalized.length < pageSize || (page * pageSize >= total)) {
  210. noMore.value[pageKey] = true;
  211. } else {
  212. currentPage.value[pageKey] = page + 1;
  213. }
  214. } catch (e) {
  215. uni.showToast({ title: e?.message || '加载失败', icon: 'none' });
  216. } finally {
  217. loading.value = false;
  218. }
  219. }
  220. const storeIdRef = ref('');
  221. const tableIdRef = ref('');
  222. const STORAGE_STORE_ID = 'currentStoreId';
  223. const STORAGE_TABLE_ID = 'currentTableId';
  224. function doLoad() {
  225. currentPage.value = { current: 1, history: 1 };
  226. noMore.value = { current: false, history: false };
  227. loadOrderList(activeTab.value, false);
  228. }
  229. // 搜索触发(输入框确认时)
  230. function doSearch() {
  231. doLoad();
  232. }
  233. // 滚动到底部加载更多
  234. function handleScrollToLower() {
  235. loadOrderList(activeTab.value, true);
  236. }
  237. watch(activeTab, (tab) => {
  238. // 每次切换标签都重新拉取该 tab 的数据,保证来回切换时列表会更新
  239. loadOrderList(tab, false);
  240. });
  241. // 处理订单详情:携带 tableId 等,详情接口未回桌台主键时仍可加餐(与列表去加餐一致)
  242. const handleOrderDetail = (order) => {
  243. const orderId = order?.id ?? order?.orderId ?? '';
  244. if (!orderId) {
  245. uni.showToast({ title: '订单ID缺失', icon: 'none' });
  246. return;
  247. }
  248. const q = [`orderId=${encodeURIComponent(String(orderId))}`];
  249. const tableId = order?.tableId ?? order?.storeTableId ?? order?.diningTableId ?? '';
  250. if (tableId !== '' && tableId != null) {
  251. q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  252. }
  253. const storeId = order?.storeId ?? '';
  254. if (storeId !== '' && storeId != null) {
  255. q.push(`storeId=${encodeURIComponent(String(storeId))}`);
  256. }
  257. const diners = order?.dinerCount ?? order?.diners ?? '';
  258. if (diners !== '' && diners != null) {
  259. q.push(`dinerCount=${encodeURIComponent(String(diners))}`);
  260. }
  261. go(`/pages/orderInfo/orderDetail?${q.join('&')}`);
  262. };
  263. // 切换标签页
  264. const switchTab = (tab) => {
  265. activeTab.value = tab;
  266. };
  267. // 处理加餐(逻辑见 utils/orderAddFoodNavigate.js)
  268. const handleAddFood = (order) => navigateToAddFood(order);
  269. // 处理结算:带上订单ID、桌号、人数、订单金额、备注,确认订单页用于调起支付
  270. const handleCheckout = (order) => {
  271. const orderId = order?.id ?? order?.orderId ?? '';
  272. const orderNo = order?.orderNo ?? order?.orderNumber ?? orderId ?? '';
  273. const tableId = order?.tableId ?? order?.tableNumber ?? '';
  274. const tableNumber = order?.tableNumber ?? order?.tableNo ?? '';
  275. const diners = order?.dinerCount ?? order?.diners ?? '';
  276. const totalAmount = order?.totalPrice ?? order?.payAmount ?? 0;
  277. const remark = (order?.remark ?? '').trim().slice(0, 30);
  278. const q = [];
  279. if (orderId !== '') q.push(`orderId=${encodeURIComponent(String(orderId))}`);
  280. if (orderNo !== '') q.push(`orderNo=${encodeURIComponent(String(orderNo))}`);
  281. if (tableId !== '' && tableId != null) q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  282. if (tableNumber !== '') q.push(`tableNumber=${encodeURIComponent(String(tableNumber))}`);
  283. if (diners !== '' && diners != null) q.push(`diners=${encodeURIComponent(String(diners))}`);
  284. if (totalAmount != null && totalAmount !== '') q.push(`totalAmount=${encodeURIComponent(String(totalAmount))}`);
  285. if (remark !== '') q.push(`remark=${encodeURIComponent(remark)}`);
  286. go(q.length ? `/pages/checkout/index?${q.join('&')}` : '/pages/checkout/index');
  287. };
  288. onLoad((options) => {
  289. uni.setNavigationBarTitle({ title: '我的订单' });
  290. // 优先用 URL 参数,否则用本地缓存(点餐/下单流程会写入)
  291. storeIdRef.value = options?.storeId ?? uni.getStorageSync(STORAGE_STORE_ID) ?? '';
  292. tableIdRef.value = options?.tableId ?? uni.getStorageSync(STORAGE_TABLE_ID) ?? '';
  293. doLoad();
  294. });
  295. </script>
  296. <style lang="scss" scoped>
  297. .content {
  298. display: flex;
  299. flex-direction: column;
  300. height: 100vh;
  301. background-color: #f7f9fa;
  302. box-sizing: border-box;
  303. }
  304. // 搜索栏
  305. .search-container {
  306. width: 100%;
  307. padding: 20rpx 30rpx;
  308. background-color: #fff;
  309. box-sizing: border-box;
  310. .search-box {
  311. width: 100%;
  312. height: 80rpx;
  313. background-color: #f5f5f5;
  314. border-radius: 40rpx;
  315. display: flex;
  316. align-items: center;
  317. padding: 0 30rpx;
  318. box-sizing: border-box;
  319. .search-icon {
  320. width: 34rpx;
  321. height: 34rpx;
  322. margin-right: 16rpx;
  323. }
  324. .search-input {
  325. flex: 1;
  326. font-size: 28rpx;
  327. color: #333;
  328. background: transparent;
  329. border: none;
  330. }
  331. }
  332. }
  333. // 标签页
  334. .tabs {
  335. display: flex;
  336. background-color: #fff;
  337. border-bottom: 1rpx solid #f0f0f0;
  338. padding: 0 30rpx;
  339. .tab-item {
  340. flex: 1;
  341. height: 88rpx;
  342. display: flex;
  343. align-items: center;
  344. justify-content: center;
  345. font-size: 27rpx;
  346. color: #AAAAAA;
  347. font-weight: bold;
  348. position: relative;
  349. transition: color 0.3s;
  350. &.active {
  351. font-size: 27rpx;
  352. color: #151515;
  353. font-weight: bold;
  354. &::after {
  355. content: '';
  356. position: absolute;
  357. bottom: 0;
  358. left: 50%;
  359. transform: translateX(-50%);
  360. width: 40rpx;
  361. height: 8rpx;
  362. background-color: #FF6B35;
  363. border-radius: 5rpx;
  364. }
  365. }
  366. }
  367. }
  368. // 订单列表
  369. .order-list {
  370. flex: 1;
  371. padding: 20rpx 30rpx;
  372. box-sizing: border-box;
  373. }
  374. // 订单卡片
  375. .order-card {
  376. width: 100%;
  377. background-color: #fff;
  378. border-radius: 20rpx;
  379. padding: 30rpx;
  380. margin-bottom: 20rpx;
  381. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  382. box-sizing: border-box;
  383. // 订单头部
  384. .order-header {
  385. display: flex;
  386. justify-content: space-between;
  387. align-items: center;
  388. margin-bottom: 20rpx;
  389. .order-number {
  390. display: flex;
  391. align-items: center;
  392. font-size: 28rpx;
  393. color: #151515;
  394. font-weight: 500;
  395. .order-number-img {
  396. width: 42rpx;
  397. height: 42rpx;
  398. margin-right: 8rpx;
  399. }
  400. }
  401. }
  402. // 店铺信息
  403. .store-info {
  404. display: flex;
  405. align-items: center;
  406. justify-content: space-between;
  407. margin-bottom: 12rpx;
  408. .order-status {
  409. font-size: 28rpx;
  410. font-weight: 500;
  411. &.status-active {
  412. font-size: 23rpx;
  413. color: #008844;
  414. }
  415. &.status-unpaid {
  416. color: #008844;
  417. }
  418. &.status-paid {
  419. color: #008844;
  420. }
  421. &.status-completed {
  422. color: #E61F19;
  423. }
  424. &.status-cancelled {
  425. color: #FF3B30;
  426. }
  427. }
  428. .store-name {
  429. font-size: 27rpx;
  430. color: #151515;
  431. font-weight: bold;
  432. }
  433. .store-arrow {
  434. font-size: 36rpx;
  435. color: #999;
  436. margin-left: 10rpx;
  437. }
  438. }
  439. // 订单时间
  440. .order-time {
  441. font-size: 23rpx;
  442. color: #AAAAAA;
  443. margin-bottom: 20rpx;
  444. }
  445. // 商品信息
  446. .order-goods {
  447. display: flex;
  448. align-items: center;
  449. margin-bottom: 30rpx;
  450. .goods-images {
  451. display: flex;
  452. gap: 12rpx;
  453. margin-right: 20rpx;
  454. .goods-image {
  455. width: 100rpx;
  456. height: 100rpx;
  457. border-radius: 8rpx;
  458. background-color: #f5f5f5;
  459. }
  460. }
  461. .goods-info {
  462. flex: 1;
  463. display: flex;
  464. flex-direction: column;
  465. align-items: flex-end;
  466. .goods-price {
  467. font-weight: bold;
  468. font-size: 27rpx;
  469. color: #151515;
  470. margin-bottom: 8rpx;
  471. }
  472. .goods-count {
  473. font-size: 23rpx;
  474. color: #666666;
  475. }
  476. }
  477. }
  478. // 操作按钮
  479. .order-actions {
  480. display: flex;
  481. gap: 20rpx;
  482. padding-top: 20rpx;
  483. border-top: 1rpx solid #f0f0f0;
  484. justify-content: flex-end;
  485. .action-btn {
  486. height: 68rpx;
  487. display: flex;
  488. align-items: center;
  489. justify-content: center;
  490. border-radius: 34rpx;
  491. font-size: 28rpx;
  492. font-weight: 500;
  493. transition: all 0.3s;
  494. &.outline {
  495. border: 2rpx solid #FF6B35;
  496. color: #FF6B35;
  497. background-color: transparent;
  498. text-align: center;
  499. width: 172rpx;
  500. height: 73rpx;
  501. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  502. border-radius: 19rpx 19rpx 19rpx 19rpx;
  503. border: 2rpx solid #F47D1F;
  504. &:active {
  505. background-color: #fff4e6;
  506. }
  507. }
  508. &.primary {
  509. background: linear-gradient(135deg, #FF6B35 0%, #FF8C42 100%);
  510. color: #fff;
  511. width: 172rpx;
  512. height: 73rpx;
  513. background: linear-gradient(0deg, #FCB73F 0%, #FC733D 100%);
  514. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  515. border-radius: 19rpx 19rpx 19rpx 19rpx;
  516. &:active {
  517. opacity: 0.8;
  518. }
  519. }
  520. }
  521. }
  522. }
  523. // 空状态
  524. .empty-state {
  525. width: 100%;
  526. padding: 100rpx 0;
  527. display: flex;
  528. justify-content: center;
  529. align-items: center;
  530. .empty-text {
  531. font-size: 28rpx;
  532. color: #999;
  533. }
  534. }
  535. </style>