index.vue 16 KB

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