index.vue 16 KB

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