Jelajahi Sumber

全部删除接口

sunshibo 2 bulan lalu
induk
melakukan
0cd5090ee0
3 mengubah file dengan 39 tambahan dan 14 penghapusan
  1. 7 0
      api/dining.js
  2. 25 14
      pages/orderFood/index.vue
  3. 7 0
      utils/request.js

+ 7 - 0
api/dining.js

@@ -26,6 +26,13 @@ export const PostOrderCartAdd = (dto) =>
 export const PostOrderCartUpdate = (params) =>
   api.put({ url: '/store/order/cart/update', params, formUrlEncoded: true });
 
+// 清空购物车(DELETE /store/order/cart/clear,入参 tableId:query + body 双传,兼容不同后端)
+export const PostOrderCartClear = (tableId) => {
+  const id = tableId != null ? String(tableId) : '';
+  const url = id ? `/store/order/cart/clear?tableId=${encodeURIComponent(id)}` : '/store/order/cart/clear';
+  return api.delete({ url, params: id ? { tableId: id } : {} });
+};
+
 /**
  * 订单 SSE 接口配置(GET /store/order/sse/{tableId})
  * 仅提供 URL 与 header,实际连接请使用 utils/sse.js 的 createSSEConnection 封装

+ 25 - 14
pages/orderFood/index.vue

@@ -51,7 +51,7 @@ import CouponModal from "./components/CouponModal.vue";
 import CartModal from "./components/CartModal.vue";
 import SelectCouponModal from "./components/SelectCouponModal.vue";
 import { go } from "@/utils/utils.js";
-import { DiningOrderFood, GetStoreCategories, GetStoreCuisines, getOrderSseConfig, GetOrderCart, PostOrderCartAdd, PostOrderCartUpdate } from "@/api/dining.js";
+import { DiningOrderFood, GetStoreCategories, GetStoreCuisines, getOrderSseConfig, GetOrderCart, PostOrderCartAdd, PostOrderCartUpdate, PostOrderCartClear } from "@/api/dining.js";
 import { createSSEConnection } from "@/utils/sse.js";
 
 const tableId = ref(''); // 桌号,来自上一页 url 参数 tableid
@@ -173,12 +173,12 @@ const availableCoupons = computed(() => {
 // 分类 id 统一转字符串,避免 number/string 比较导致误判
 const sameCategory = (a, b) => String(a ?? '') === String(b ?? '');
 
-// 将购物车数量同步到菜品列表:用购物车接口的 items 按 cuisineId 匹配 foodList 中的菜品并更新 quantity
+// 将购物车数量同步到菜品列表:用购物车接口的 items 按 cuisineId 匹配 foodList 中的菜品并更新 quantity;items 为空时清空所有菜品数量
 const mergeCartIntoFoodList = () => {
   const cartItems = pendingCartData ?? cartData.value?.items ?? [];
-  if (!Array.isArray(cartItems) || cartItems.length === 0) return;
+  if (!Array.isArray(cartItems)) return;
   if (!foodList.value.length) {
-    console.log('购物车已缓存,等 foodList 加载后再同步到菜品列表');
+    if (cartItems.length > 0) console.log('购物车已缓存,等 foodList 加载后再同步到菜品列表');
     return;
   }
   foodList.value = foodList.value.map((item) => {
@@ -189,11 +189,11 @@ const mergeCartIntoFoodList = () => {
     });
     const qty = cartItem
       ? Number(cartItem.quantity ?? cartItem.num ?? cartItem.count ?? 0) || 0
-      : (item.quantity ?? 0);
+      : 0;
     return { ...item, quantity: qty };
   });
   pendingCartData = null;
-  console.log('购物车数量已同步到菜品列表');
+  console.log('购物车数量已同步到菜品列表', cartItems.length ? '' : '(已清空)');
 };
 
 // 从接口返回的 data 层中解析出购物车数组(/store/order/cart/{id} 返回在 items 下)
@@ -462,15 +462,26 @@ const handleCartClose = () => {
   cartModalOpen.value = false;
 };
 
-// 清空购物车
+// 清空购物车:调用 /store/order/cart/clear,入参 tableId,成功后清空本地并关闭弹窗
 const handleCartClear = () => {
-  foodList.value = foodList.value.map((f) => ({ ...f, quantity: 0 }));
-  cartData.value = { items: [], totalAmount: 0, totalQuantity: 0 };
-  cartModalOpen.value = false;
-  uni.showToast({
-    title: '已清空购物车',
-    icon: 'success'
-  });
+  if (!tableId.value) {
+    foodList.value = foodList.value.map((f) => ({ ...f, quantity: 0 }));
+    cartData.value = { items: [], totalAmount: 0, totalQuantity: 0 };
+    cartModalOpen.value = false;
+    uni.showToast({ title: '已清空购物车', icon: 'success' });
+    return;
+  }
+  PostOrderCartClear(tableId.value)
+    .then(() => {
+      foodList.value = foodList.value.map((f) => ({ ...f, quantity: 0 }));
+      cartData.value = { items: [], totalAmount: 0, totalQuantity: 0 };
+      cartModalOpen.value = false;
+      uni.showToast({ title: '已清空购物车', icon: 'success' });
+    })
+    .catch((err) => {
+      console.error('清空购物车失败:', err);
+      uni.showToast({ title: '清空失败,请重试', icon: 'none' });
+    });
 };
 
 // 下单点击

+ 7 - 0
utils/request.js

@@ -166,6 +166,13 @@ export class Request {
 			method: 'PUT'
 		});
 	}
+
+	delete(options) {
+		return this.#request({
+			...options,
+			method: 'DELETE'
+		});
+	}
 }
 
 export const api = new Request();