This commit is contained in:
2024-04-14 17:02:49 +08:00
parent f2f7fcde48
commit 13fd77f13b

View File

@@ -4,7 +4,7 @@ import { getCompanyId } from '@/utils';
const useShoppingCartStore = defineStore('shoppingCart', { const useShoppingCartStore = defineStore('shoppingCart', {
state: (): { shoppingCartList: GoodsBean[] } => ({ state: (): { shoppingCartList: GoodsBean[] } => ({
shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[] shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) == '' ? [] : uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[]
}), }),
persist: { persist: {
@@ -16,7 +16,7 @@ const useShoppingCartStore = defineStore('shoppingCart', {
uni.setStorageSync(`shoppingCart_${getCompanyId()}`, JSON.parse(value).shoppingCartList); uni.setStorageSync(`shoppingCart_${getCompanyId()}`, JSON.parse(value).shoppingCartList);
}, },
getItem(key) { getItem(key) {
return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)); // [!code warning] return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)) || []; // [!code warning]
} }
}, },
// 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined持久化整个状态) // 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined持久化整个状态)
@@ -33,7 +33,7 @@ const useShoppingCartStore = defineStore('shoppingCart', {
}, },
getSameGoodsIndex: (state) => (goodsId: string, colorId: string, sizeId: string) => { getSameGoodsIndex: (state) => (goodsId: string, colorId: string, sizeId: string) => {
return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId); return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId) || -1;
} }
}, },