购物车逻辑调整

This commit is contained in:
2024-04-14 16:43:43 +08:00
parent 16dac8d97f
commit f2f7fcde48
5 changed files with 31 additions and 18 deletions

View File

@@ -31,7 +31,7 @@
<view class='shopping-cart' @click.stop='goPath("/pages/mall/subs/shoppingcart/index")'>
<image :src='assetsUrl("ic_shopping_cart.png")' />
<text v-if='shoppingCartList.length>0'>{{ shoppingCartList?.length }}</text>
<text v-if='totalCount>0'>{{ totalCount }}</text>
</view>
</view>
<view class='content' v-else style='align-items: center;justify-content:center;'>
@@ -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<CategoryBean[]>([]);
@@ -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('添加购物车成功');
});
};

View File

@@ -42,7 +42,7 @@
</view>
</view>
<view class='recommend-view c-flex-column' v-if='recommendList?.length>0'>
<view class='recommend-view c-flex-column' v-if='(recommendList?.length||0)>0'>
<text>浏览此商品的客户还浏览了</text>
<scroll-view scroll-x>
<view style='display: inline-block' v-for='(item, index) in recommendList'
@@ -78,7 +78,7 @@
<view class='small-button-item'>
<view class='shoppingcart-count' @click.stop='goPath("/pages/mall/subs/shoppingcart/index")'>
<image :src='assetsUrl("ic_goods_shoppingcart.png")' />
<text v-if='shoppingCartList.length>0'>{{ shoppingCartList.length }}</text>
<text v-if='totalCount>0'>{{ totalCount }}</text>
</view>
<text>购物车</text>
</view>
@@ -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<GoodsBean>();
const recommendList = ref<GoodsBean[]>();

View File

@@ -6,7 +6,7 @@
</text>
</view>
<view class='card-view' v-if='shoppingCartList.length>0'>
<view class='card-view' v-if='totalCount>0'>
<scroll-view>
<view class='c-flex-column' v-for='(item, index) in shoppingCartList' :key='index'>
<view class='c-flex-row'>
@@ -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<GoodsBean>();

View File

@@ -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 = [];
}

View File

@@ -43,8 +43,8 @@ const useUserStore = defineStore('user', {
},
getters: {
// getUserInfo(state: UserState): UserState {
// return state.address;
// getUserInfo(state: UserBean): UserBean {
// return { state };
// }
},