index.vue 16 KB

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