index.vue 16 KB

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