优化下单流程
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user