|
|
@@ -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' });
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
// 下单点击
|