index.vue 17 KB

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