index.vue 16 KB

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