diff --git a/src/pages/mall/index.vue b/src/pages/mall/index.vue
index 7869e79..01aab12 100644
--- a/src/pages/mall/index.vue
+++ b/src/pages/mall/index.vue
@@ -31,7 +31,7 @@
- {{ shoppingCartList?.length }}
+ {{ totalCount }}
@@ -52,7 +52,7 @@ import { useUserStore } from '@/store';
const userStore = useUserStore();
const { companyConfigInfo } = storeToRefs(userStore);
const shoppingCartStore = useShoppingCartStore();
-const { shoppingCartList } = storeToRefs(shoppingCartStore);
+const { totalCount } = storeToRefs(shoppingCartStore);
const skuDialogRef = ref();
const categoryList = ref([]);
@@ -66,13 +66,12 @@ onLoad(() => {
return;
}
- console.log('companyConfigInfo.value', companyConfigInfo.value);
-
userStore.$onAction(({ name, after }) => {
after(() => {
//更新用户信息
if(name === 'setUserInfo') {
fetchCategoryList();
+ shoppingCartStore.resetData();
}
});
});
@@ -127,7 +126,16 @@ const getRandomFloatInRange = (a: number, b: number) => {
const addShoppingCart = (goodsBean: GoodsBean) => {
skuDialogRef.value.show(goodsBean.goodsId, (e: GoodsBean) => {
- shoppingCartStore.save(e);
+ //重新选择sku后判断当前购物中是否存在相同sku的商品
+ const existsIndex = shoppingCartStore.getSameGoodsIndex(e?.id || '', e.checkedStock.colorId, e.checkedStock.sizeId);
+ //不存在则更新当前商品sku
+ if(existsIndex < 0) {
+ shoppingCartStore.save(e);
+ }
+ //如果已存在,则更新已存在商品数量,并删除当前商品
+ else {
+ shoppingCartStore.updateCount(existsIndex, e.checkedStock.count);
+ }
showToast('添加购物车成功');
});
};
diff --git a/src/pages/mall/subs/goods/detail.vue b/src/pages/mall/subs/goods/detail.vue
index 987da20..0aa46f1 100644
--- a/src/pages/mall/subs/goods/detail.vue
+++ b/src/pages/mall/subs/goods/detail.vue
@@ -42,7 +42,7 @@
-
+
浏览此商品的客户还浏览了
- {{ shoppingCartList.length }}
+ {{ totalCount }}
购物车
@@ -102,7 +102,7 @@ import { GoodsBean } from '@/api/goods/types';
import useShoppingCartStore from '@/store/modules/shoppingcart';
const shoppingCartStore = useShoppingCartStore();
-const { shoppingCartList } = storeToRefs(shoppingCartStore);
+const { totalCount } = storeToRefs(shoppingCartStore);
const goodsBean = ref();
const recommendList = ref();
diff --git a/src/pages/mall/subs/shoppingcart/index.vue b/src/pages/mall/subs/shoppingcart/index.vue
index c105747..9fbb2aa 100644
--- a/src/pages/mall/subs/shoppingcart/index.vue
+++ b/src/pages/mall/subs/shoppingcart/index.vue
@@ -6,7 +6,7 @@
-
+
@@ -72,7 +72,7 @@ const isEditMode = ref(false);
const checkedAll = ref(false);
const shoppingCartStore = useShoppingCartStore();
-const { shoppingCartList } = storeToRefs(shoppingCartStore);
+const { shoppingCartList, totalCount } = storeToRefs(shoppingCartStore);
const skuDialogRef = ref();
const temporaryGoodsBean = ref();
diff --git a/src/store/modules/shoppingcart/index.ts b/src/store/modules/shoppingcart/index.ts
index 21d6d98..6771758 100644
--- a/src/store/modules/shoppingcart/index.ts
+++ b/src/store/modules/shoppingcart/index.ts
@@ -4,18 +4,19 @@ import { getCompanyId } from '@/utils';
const useShoppingCartStore = defineStore('shoppingCart', {
state: (): { shoppingCartList: GoodsBean[] } => ({
- shoppingCartList: []
+ shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[]
}),
persist: {
// 修改存储中使用的键名称,默认为当前 Store的 id
- key: 'shoppingCartState_' + getCompanyId(),
+ key: 'shoppingCartState',
storage: {
setItem(key, value) {
- uni.setStorageSync(key, value); // [!code warning]
+ uni.setStorageSync(key, value || []); // [!code warning]
+ uni.setStorageSync(`shoppingCart_${getCompanyId()}`, JSON.parse(value).shoppingCartList);
},
getItem(key) {
- return uni.getStorageSync(key); // [!code warning]
+ return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)); // [!code warning]
}
},
// 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined,持久化整个状态)
@@ -23,11 +24,11 @@ const useShoppingCartStore = defineStore('shoppingCart', {
},
getters: {
- getShoppingCartList(): any[] {
+ getShoppingCartList(): GoodsBean[] {
return this.shoppingCartList;
},
- getTotalCount(): number {
+ totalCount(): number {
return this.shoppingCartList.length;
},
@@ -57,6 +58,10 @@ const useShoppingCartStore = defineStore('shoppingCart', {
this.shoppingCartList = this.shoppingCartList.filter(res => res.checked == undefined || res.checked === false);
},
+ resetData() {
+ this.shoppingCartList = uni.getStorageSync(`shoppingCart_${getCompanyId()}`);
+ },
+
clearAll() {
this.shoppingCartList = [];
}
diff --git a/src/store/modules/user/index.ts b/src/store/modules/user/index.ts
index a681cfe..fde507f 100644
--- a/src/store/modules/user/index.ts
+++ b/src/store/modules/user/index.ts
@@ -43,8 +43,8 @@ const useUserStore = defineStore('user', {
},
getters: {
- // getUserInfo(state: UserState): UserState {
- // return state.address;
+ // getUserInfo(state: UserBean): UserBean {
+ // return { state };
// }
},