优化下单流程

This commit is contained in:
2025-07-30 15:34:11 +08:00
parent 2832cd3832
commit a8a84f8b39
10 changed files with 170 additions and 42 deletions

View File

@@ -9,6 +9,8 @@ export interface CartItem {
image: string;
quantity: number;
addTime: number;
skuId?: number;
specInfo?: string;
}
// 购物车Hook
@@ -51,9 +53,15 @@ export const useCart = () => {
name: string;
price: string;
image: string;
skuId?: number;
specInfo?: string;
}, quantity: number = 1) => {
const newItems = [...cartItems];
const existingItemIndex = newItems.findIndex(item => item.goodsId === goods.goodsId);
// 如果有SKU需要根据goodsId和skuId来判断是否为同一商品
const existingItemIndex = newItems.findIndex(item =>
item.goodsId === goods.goodsId &&
(goods.skuId ? item.skuId === goods.skuId : !item.skuId)
);
if (existingItemIndex >= 0) {
// 如果商品已存在,增加数量
@@ -66,7 +74,9 @@ export const useCart = () => {
price: goods.price,
image: goods.image,
quantity,
addTime: Date.now()
addTime: Date.now(),
skuId: goods.skuId,
specInfo: goods.specInfo
};
newItems.push(newItem);
}
@@ -98,7 +108,7 @@ export const useCart = () => {
return;
}
const newItems = cartItems.map(item =>
const newItems = cartItems.map(item =>
item.goodsId === goodsId ? { ...item, quantity } : item
);
setCartItems(newItems);