index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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">
  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" @click="handleStoreClick(order)">
  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" @click="handleOrderDetail(order)">
  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. // 处理店铺点击
  238. const handleStoreClick = (order) => {
  239. console.log('点击店铺:', order);
  240. // TODO: 跳转到店铺详情
  241. };
  242. // 处理加餐:带上该订单的桌号和人数,点餐页依赖 tableid、diners 拉取列表和购物车
  243. const handleAddFood = (order) => {
  244. const tableid = order?.tableId ?? order?.tableNumber ?? uni.getStorageSync(STORAGE_TABLE_ID) ?? '';
  245. const diners = order?.dinerCount ?? order?.diners ?? uni.getStorageSync('currentDiners') ?? '';
  246. const q = [];
  247. if (tableid !== '' && tableid != null) q.push(`tableid=${encodeURIComponent(String(tableid))}`);
  248. if (diners !== '' && diners != null) q.push(`diners=${encodeURIComponent(String(diners))}`);
  249. go(q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index');
  250. };
  251. // 处理结算:带上订单ID、桌号、人数、订单金额、备注,确认订单页用于调起支付
  252. const handleCheckout = (order) => {
  253. const orderId = order?.id ?? order?.orderId ?? '';
  254. const orderNo = order?.orderNo ?? order?.orderNumber ?? orderId ?? '';
  255. const tableId = order?.tableId ?? order?.tableNumber ?? '';
  256. const tableNumber = order?.tableNumber ?? order?.tableNo ?? '';
  257. const diners = order?.dinerCount ?? order?.diners ?? '';
  258. const totalAmount = order?.totalPrice ?? order?.payAmount ?? 0;
  259. const remark = (order?.remark ?? '').trim().slice(0, 30);
  260. const q = [];
  261. if (orderId !== '') q.push(`orderId=${encodeURIComponent(String(orderId))}`);
  262. if (orderNo !== '') q.push(`orderNo=${encodeURIComponent(String(orderNo))}`);
  263. if (tableId !== '' && tableId != null) q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  264. if (tableNumber !== '') q.push(`tableNumber=${encodeURIComponent(String(tableNumber))}`);
  265. if (diners !== '' && diners != null) q.push(`diners=${encodeURIComponent(String(diners))}`);
  266. if (totalAmount != null && totalAmount !== '') q.push(`totalAmount=${encodeURIComponent(String(totalAmount))}`);
  267. if (remark !== '') q.push(`remark=${encodeURIComponent(remark)}`);
  268. go(q.length ? `/pages/checkout/index?${q.join('&')}` : '/pages/checkout/index');
  269. };
  270. onLoad((options) => {
  271. uni.setNavigationBarTitle({ title: '我的订单' });
  272. // 优先用 URL 参数,否则用本地缓存(点餐/下单流程会写入)
  273. storeIdRef.value = options?.storeId ?? uni.getStorageSync(STORAGE_STORE_ID) ?? '';
  274. tableIdRef.value = options?.tableId ?? uni.getStorageSync(STORAGE_TABLE_ID) ?? '';
  275. doLoad();
  276. });
  277. </script>
  278. <style lang="scss" scoped>
  279. .content {
  280. display: flex;
  281. flex-direction: column;
  282. height: 100vh;
  283. background-color: #f7f9fa;
  284. box-sizing: border-box;
  285. }
  286. // 搜索栏
  287. .search-container {
  288. width: 100%;
  289. padding: 20rpx 30rpx;
  290. background-color: #fff;
  291. box-sizing: border-box;
  292. .search-box {
  293. width: 100%;
  294. height: 80rpx;
  295. background-color: #f5f5f5;
  296. border-radius: 40rpx;
  297. display: flex;
  298. align-items: center;
  299. padding: 0 30rpx;
  300. box-sizing: border-box;
  301. .search-icon {
  302. width: 34rpx;
  303. height: 34rpx;
  304. margin-right: 16rpx;
  305. }
  306. .search-input {
  307. flex: 1;
  308. font-size: 28rpx;
  309. color: #333;
  310. background: transparent;
  311. border: none;
  312. }
  313. }
  314. }
  315. // 标签页
  316. .tabs {
  317. display: flex;
  318. background-color: #fff;
  319. border-bottom: 1rpx solid #f0f0f0;
  320. padding: 0 30rpx;
  321. .tab-item {
  322. flex: 1;
  323. height: 88rpx;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. font-size: 27rpx;
  328. color: #AAAAAA;
  329. font-weight: bold;
  330. position: relative;
  331. transition: color 0.3s;
  332. &.active {
  333. font-size: 27rpx;
  334. color: #151515;
  335. font-weight: bold;
  336. &::after {
  337. content: '';
  338. position: absolute;
  339. bottom: 0;
  340. left: 50%;
  341. transform: translateX(-50%);
  342. width: 40rpx;
  343. height: 8rpx;
  344. background-color: #FF6B35;
  345. border-radius: 5rpx;
  346. }
  347. }
  348. }
  349. }
  350. // 订单列表
  351. .order-list {
  352. flex: 1;
  353. padding: 20rpx 30rpx;
  354. box-sizing: border-box;
  355. }
  356. // 订单卡片
  357. .order-card {
  358. width: 100%;
  359. background-color: #fff;
  360. border-radius: 20rpx;
  361. padding: 30rpx;
  362. margin-bottom: 20rpx;
  363. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  364. box-sizing: border-box;
  365. // 订单头部
  366. .order-header {
  367. display: flex;
  368. justify-content: space-between;
  369. align-items: center;
  370. margin-bottom: 20rpx;
  371. .order-number {
  372. display: flex;
  373. align-items: center;
  374. font-size: 28rpx;
  375. color: #151515;
  376. font-weight: 500;
  377. .order-number-img {
  378. width: 42rpx;
  379. height: 42rpx;
  380. margin-right: 8rpx;
  381. }
  382. }
  383. }
  384. // 店铺信息
  385. .store-info {
  386. display: flex;
  387. align-items: center;
  388. justify-content: space-between;
  389. margin-bottom: 12rpx;
  390. .order-status {
  391. font-size: 28rpx;
  392. font-weight: 500;
  393. &.status-active {
  394. font-size: 23rpx;
  395. color: #008844;
  396. }
  397. &.status-unpaid {
  398. color: #008844;
  399. }
  400. &.status-paid {
  401. color: #008844;
  402. }
  403. &.status-completed {
  404. color: #E61F19;
  405. }
  406. &.status-cancelled {
  407. color: #FF3B30;
  408. }
  409. }
  410. .store-name {
  411. font-size: 27rpx;
  412. color: #151515;
  413. font-weight: bold;
  414. }
  415. .store-arrow {
  416. font-size: 36rpx;
  417. color: #999;
  418. margin-left: 10rpx;
  419. }
  420. }
  421. // 订单时间
  422. .order-time {
  423. font-size: 23rpx;
  424. color: #AAAAAA;
  425. margin-bottom: 20rpx;
  426. }
  427. // 商品信息
  428. .order-goods {
  429. display: flex;
  430. align-items: center;
  431. margin-bottom: 30rpx;
  432. .goods-images {
  433. display: flex;
  434. gap: 12rpx;
  435. margin-right: 20rpx;
  436. .goods-image {
  437. width: 100rpx;
  438. height: 100rpx;
  439. border-radius: 8rpx;
  440. background-color: #f5f5f5;
  441. }
  442. }
  443. .goods-info {
  444. flex: 1;
  445. display: flex;
  446. flex-direction: column;
  447. align-items: flex-end;
  448. .goods-price {
  449. font-weight: bold;
  450. font-size: 27rpx;
  451. color: #151515;
  452. margin-bottom: 8rpx;
  453. }
  454. .goods-count {
  455. font-size: 23rpx;
  456. color: #666666;
  457. }
  458. }
  459. }
  460. // 操作按钮
  461. .order-actions {
  462. display: flex;
  463. gap: 20rpx;
  464. padding-top: 20rpx;
  465. border-top: 1rpx solid #f0f0f0;
  466. justify-content: flex-end;
  467. .action-btn {
  468. height: 68rpx;
  469. display: flex;
  470. align-items: center;
  471. justify-content: center;
  472. border-radius: 34rpx;
  473. font-size: 28rpx;
  474. font-weight: 500;
  475. transition: all 0.3s;
  476. &.outline {
  477. border: 2rpx solid #FF6B35;
  478. color: #FF6B35;
  479. background-color: transparent;
  480. text-align: center;
  481. width: 172rpx;
  482. height: 73rpx;
  483. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  484. border-radius: 19rpx 19rpx 19rpx 19rpx;
  485. border: 2rpx solid #F47D1F;
  486. &:active {
  487. background-color: #fff4e6;
  488. }
  489. }
  490. &.primary {
  491. background: linear-gradient(135deg, #FF6B35 0%, #FF8C42 100%);
  492. color: #fff;
  493. width: 172rpx;
  494. height: 73rpx;
  495. background: linear-gradient(0deg, #FCB73F 0%, #FC733D 100%);
  496. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  497. border-radius: 19rpx 19rpx 19rpx 19rpx;
  498. &:active {
  499. opacity: 0.8;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. // 空状态
  506. .empty-state {
  507. width: 100%;
  508. padding: 100rpx 0;
  509. display: flex;
  510. justify-content: center;
  511. align-items: center;
  512. .empty-text {
  513. font-size: 28rpx;
  514. color: #999;
  515. }
  516. }
  517. </style>