index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 res = await GetOrderPage(params);
  191. // /store/order/my-orders:返回 data,可能为 { records, total } 或 { data: { records, total } }
  192. const raw = res && typeof res === 'object' ? res : {};
  193. const list = Array.isArray(raw.records)
  194. ? raw.records
  195. : Array.isArray(raw.data?.records)
  196. ? raw.data.records
  197. : Array.isArray(raw.list)
  198. ? raw.list
  199. : [];
  200. const normalized = list.map(normalizeOrder);
  201. if (tab === 'current') {
  202. currentOrders.value = append ? [...currentOrders.value, ...normalized] : normalized;
  203. } else {
  204. historyOrders.value = append ? [...historyOrders.value, ...normalized] : normalized;
  205. }
  206. const total =
  207. raw.total != null ? Number(raw.total) : (raw.data?.total != null ? Number(raw.data.total) : 0);
  208. if (normalized.length < pageSize || (page * pageSize >= total)) {
  209. noMore.value[pageKey] = true;
  210. } else {
  211. currentPage.value[pageKey] = page + 1;
  212. }
  213. } catch (e) {
  214. uni.showToast({ title: e?.message || '加载失败', icon: 'none' });
  215. } finally {
  216. loading.value = false;
  217. }
  218. }
  219. const storeIdRef = ref('');
  220. const tableIdRef = ref('');
  221. const STORAGE_STORE_ID = 'currentStoreId';
  222. const STORAGE_TABLE_ID = 'currentTableId';
  223. function doLoad() {
  224. currentPage.value = { current: 1, history: 1 };
  225. noMore.value = { current: false, history: false };
  226. loadOrderList(activeTab.value, false);
  227. }
  228. // 搜索触发(输入框确认时)
  229. function doSearch() {
  230. doLoad();
  231. }
  232. // 滚动到底部加载更多
  233. function handleScrollToLower() {
  234. loadOrderList(activeTab.value, true);
  235. }
  236. watch(activeTab, (tab) => {
  237. // 每次切换标签都重新拉取该 tab 的数据,保证来回切换时列表会更新
  238. loadOrderList(tab, false);
  239. });
  240. // 处理订单详情(须传 orderId 详情页才会请求接口)
  241. const handleOrderDetail = (order) => {
  242. const orderId = order?.id ?? order?.orderId ?? '';
  243. if (!orderId) {
  244. uni.showToast({ title: '订单ID缺失', icon: 'none' });
  245. return;
  246. }
  247. go(`/pages/orderInfo/orderDetail?orderId=${encodeURIComponent(String(orderId))}`);
  248. };
  249. // 切换标签页
  250. const switchTab = (tab) => {
  251. activeTab.value = tab;
  252. };
  253. // 处理加餐:带上该订单的桌号和人数,点餐页依赖 tableid、diners 拉取列表和购物车
  254. const handleAddFood = (order) => {
  255. const tableid = order?.tableId ?? order?.tableNumber ?? uni.getStorageSync(STORAGE_TABLE_ID) ?? '';
  256. const diners = order?.dinerCount ?? order?.diners ?? uni.getStorageSync('currentDiners') ?? '';
  257. const q = [];
  258. if (tableid !== '' && tableid != null) q.push(`tableid=${encodeURIComponent(String(tableid))}`);
  259. if (diners !== '' && diners != null) q.push(`diners=${encodeURIComponent(String(diners))}`);
  260. go(q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index');
  261. };
  262. // 处理结算:带上订单ID、桌号、人数、订单金额、备注,确认订单页用于调起支付
  263. const handleCheckout = (order) => {
  264. const orderId = order?.id ?? order?.orderId ?? '';
  265. const orderNo = order?.orderNo ?? order?.orderNumber ?? orderId ?? '';
  266. const tableId = order?.tableId ?? order?.tableNumber ?? '';
  267. const tableNumber = order?.tableNumber ?? order?.tableNo ?? '';
  268. const diners = order?.dinerCount ?? order?.diners ?? '';
  269. const totalAmount = order?.totalPrice ?? order?.payAmount ?? 0;
  270. const remark = (order?.remark ?? '').trim().slice(0, 30);
  271. const q = [];
  272. if (orderId !== '') q.push(`orderId=${encodeURIComponent(String(orderId))}`);
  273. if (orderNo !== '') q.push(`orderNo=${encodeURIComponent(String(orderNo))}`);
  274. if (tableId !== '' && tableId != null) q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  275. if (tableNumber !== '') q.push(`tableNumber=${encodeURIComponent(String(tableNumber))}`);
  276. if (diners !== '' && diners != null) q.push(`diners=${encodeURIComponent(String(diners))}`);
  277. if (totalAmount != null && totalAmount !== '') q.push(`totalAmount=${encodeURIComponent(String(totalAmount))}`);
  278. if (remark !== '') q.push(`remark=${encodeURIComponent(remark)}`);
  279. go(q.length ? `/pages/checkout/index?${q.join('&')}` : '/pages/checkout/index');
  280. };
  281. onLoad((options) => {
  282. uni.setNavigationBarTitle({ title: '我的订单' });
  283. // 优先用 URL 参数,否则用本地缓存(点餐/下单流程会写入)
  284. storeIdRef.value = options?.storeId ?? uni.getStorageSync(STORAGE_STORE_ID) ?? '';
  285. tableIdRef.value = options?.tableId ?? uni.getStorageSync(STORAGE_TABLE_ID) ?? '';
  286. doLoad();
  287. });
  288. </script>
  289. <style lang="scss" scoped>
  290. .content {
  291. display: flex;
  292. flex-direction: column;
  293. height: 100vh;
  294. background-color: #f7f9fa;
  295. box-sizing: border-box;
  296. }
  297. // 搜索栏
  298. .search-container {
  299. width: 100%;
  300. padding: 20rpx 30rpx;
  301. background-color: #fff;
  302. box-sizing: border-box;
  303. .search-box {
  304. width: 100%;
  305. height: 80rpx;
  306. background-color: #f5f5f5;
  307. border-radius: 40rpx;
  308. display: flex;
  309. align-items: center;
  310. padding: 0 30rpx;
  311. box-sizing: border-box;
  312. .search-icon {
  313. width: 34rpx;
  314. height: 34rpx;
  315. margin-right: 16rpx;
  316. }
  317. .search-input {
  318. flex: 1;
  319. font-size: 28rpx;
  320. color: #333;
  321. background: transparent;
  322. border: none;
  323. }
  324. }
  325. }
  326. // 标签页
  327. .tabs {
  328. display: flex;
  329. background-color: #fff;
  330. border-bottom: 1rpx solid #f0f0f0;
  331. padding: 0 30rpx;
  332. .tab-item {
  333. flex: 1;
  334. height: 88rpx;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. font-size: 27rpx;
  339. color: #AAAAAA;
  340. font-weight: bold;
  341. position: relative;
  342. transition: color 0.3s;
  343. &.active {
  344. font-size: 27rpx;
  345. color: #151515;
  346. font-weight: bold;
  347. &::after {
  348. content: '';
  349. position: absolute;
  350. bottom: 0;
  351. left: 50%;
  352. transform: translateX(-50%);
  353. width: 40rpx;
  354. height: 8rpx;
  355. background-color: #FF6B35;
  356. border-radius: 5rpx;
  357. }
  358. }
  359. }
  360. }
  361. // 订单列表
  362. .order-list {
  363. flex: 1;
  364. padding: 20rpx 30rpx;
  365. box-sizing: border-box;
  366. }
  367. // 订单卡片
  368. .order-card {
  369. width: 100%;
  370. background-color: #fff;
  371. border-radius: 20rpx;
  372. padding: 30rpx;
  373. margin-bottom: 20rpx;
  374. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  375. box-sizing: border-box;
  376. // 订单头部
  377. .order-header {
  378. display: flex;
  379. justify-content: space-between;
  380. align-items: center;
  381. margin-bottom: 20rpx;
  382. .order-number {
  383. display: flex;
  384. align-items: center;
  385. font-size: 28rpx;
  386. color: #151515;
  387. font-weight: 500;
  388. .order-number-img {
  389. width: 42rpx;
  390. height: 42rpx;
  391. margin-right: 8rpx;
  392. }
  393. }
  394. }
  395. // 店铺信息
  396. .store-info {
  397. display: flex;
  398. align-items: center;
  399. justify-content: space-between;
  400. margin-bottom: 12rpx;
  401. .order-status {
  402. font-size: 28rpx;
  403. font-weight: 500;
  404. &.status-active {
  405. font-size: 23rpx;
  406. color: #008844;
  407. }
  408. &.status-unpaid {
  409. color: #008844;
  410. }
  411. &.status-paid {
  412. color: #008844;
  413. }
  414. &.status-completed {
  415. color: #E61F19;
  416. }
  417. &.status-cancelled {
  418. color: #FF3B30;
  419. }
  420. }
  421. .store-name {
  422. font-size: 27rpx;
  423. color: #151515;
  424. font-weight: bold;
  425. }
  426. .store-arrow {
  427. font-size: 36rpx;
  428. color: #999;
  429. margin-left: 10rpx;
  430. }
  431. }
  432. // 订单时间
  433. .order-time {
  434. font-size: 23rpx;
  435. color: #AAAAAA;
  436. margin-bottom: 20rpx;
  437. }
  438. // 商品信息
  439. .order-goods {
  440. display: flex;
  441. align-items: center;
  442. margin-bottom: 30rpx;
  443. .goods-images {
  444. display: flex;
  445. gap: 12rpx;
  446. margin-right: 20rpx;
  447. .goods-image {
  448. width: 100rpx;
  449. height: 100rpx;
  450. border-radius: 8rpx;
  451. background-color: #f5f5f5;
  452. }
  453. }
  454. .goods-info {
  455. flex: 1;
  456. display: flex;
  457. flex-direction: column;
  458. align-items: flex-end;
  459. .goods-price {
  460. font-weight: bold;
  461. font-size: 27rpx;
  462. color: #151515;
  463. margin-bottom: 8rpx;
  464. }
  465. .goods-count {
  466. font-size: 23rpx;
  467. color: #666666;
  468. }
  469. }
  470. }
  471. // 操作按钮
  472. .order-actions {
  473. display: flex;
  474. gap: 20rpx;
  475. padding-top: 20rpx;
  476. border-top: 1rpx solid #f0f0f0;
  477. justify-content: flex-end;
  478. .action-btn {
  479. height: 68rpx;
  480. display: flex;
  481. align-items: center;
  482. justify-content: center;
  483. border-radius: 34rpx;
  484. font-size: 28rpx;
  485. font-weight: 500;
  486. transition: all 0.3s;
  487. &.outline {
  488. border: 2rpx solid #FF6B35;
  489. color: #FF6B35;
  490. background-color: transparent;
  491. text-align: center;
  492. width: 172rpx;
  493. height: 73rpx;
  494. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  495. border-radius: 19rpx 19rpx 19rpx 19rpx;
  496. border: 2rpx solid #F47D1F;
  497. &:active {
  498. background-color: #fff4e6;
  499. }
  500. }
  501. &.primary {
  502. background: linear-gradient(135deg, #FF6B35 0%, #FF8C42 100%);
  503. color: #fff;
  504. width: 172rpx;
  505. height: 73rpx;
  506. background: linear-gradient(0deg, #FCB73F 0%, #FC733D 100%);
  507. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(86, 125, 244, 0.05);
  508. border-radius: 19rpx 19rpx 19rpx 19rpx;
  509. &:active {
  510. opacity: 0.8;
  511. }
  512. }
  513. }
  514. }
  515. }
  516. // 空状态
  517. .empty-state {
  518. width: 100%;
  519. padding: 100rpx 0;
  520. display: flex;
  521. justify-content: center;
  522. align-items: center;
  523. .empty-text {
  524. font-size: 28rpx;
  525. color: #999;
  526. }
  527. }
  528. </style>