Browse Source

refactor(购物车): 重构购物车数量计算逻辑并优化添加/减少商品操作

- 将购物车数量计算从简单的数组长度改为商品总数量求和
- 修改添加/减少商品操作以支持变化量传递
- 更新相关组件和store方法命名以更准确反映功能
- 优化购物车页面布局结构
master
wei 6 days ago
parent
commit
3988361d7e
  1. 2
      components/custom-tab-bar/my-tab-bar.vue
  2. 18
      pages/allDish/allDish.vue
  3. 15
      pages/shoppingCart/shoppingCart.vue
  4. 11
      store/index.js

2
components/custom-tab-bar/my-tab-bar.vue

@ -26,7 +26,7 @@ const activeColor = "#06CA64";
/**
* 购物车数量
*/
const cartCount = computed(() => store.cartList.length);
const cartCount = computed(() => store.getCartTotalQuantity());
/**
* 底部导航栏标签信息

18
pages/allDish/allDish.vue

@ -615,7 +615,7 @@ async function initCartInfo() {
// cartCount.value = res.total;
// cartList.value = res.data || [];
store.changeCartList(res.data.map(item => item.id));
store.changeCartList(res.data);
totalPrice.value = res.data.reduce((acc, item) => acc + item.amount, 0).toFixed(2);
if (store.cartList.length > 0) {
preview(store.cartList);
@ -669,20 +669,22 @@ async function onAdd(item, e) {
// 0,
item.sum = Number(item.sum) || 0;
// (item.minNum), +1
item.sum += item.sum === 0 ? (item.minNum || 1) : 1;
const diff = item.sum === 0 ? (item.minNum || 1) : 1;
item.sum += diff;
//
inputValue.value = item.sum;
toCart(item);
toCart(item, diff);
}
/**
* 更新购物车
* @param item 商品项
* @param diff 变化量
*/
async function toCart(item) {
async function toCart(item, diff) {
const data = {
quantity: item.sum,
quantity: diff,
specId: item.id,
Chuxiao: item.chuxiao,
isChuxiao: false,
@ -739,11 +741,13 @@ function onMinus(item) {
// 0,
item.sum = Number(item.sum) || 0;
// (item.minNum), -1
item.sum -= item.sum === (item.minNum || 1) ? (item.minNum || 1) : 1;
const diff = -(item.sum === (item.minNum || 1) ? (item.minNum || 1) : 1);
item.sum += diff;
//
inputValue.value = item.sum;
toCart(item);
toCart(item, diff);
}
function onChooseNorm(item) {

15
pages/shoppingCart/shoppingCart.vue

@ -1,8 +1,9 @@
<script setup>
import { onLoad } from "@dcloudio/uni-app";
import customTabBar from "@/components/custom-tab-bar/my-tab-bar.vue";
import navv from "@/components/nav/nav.vue";
onLoad(() => {
// #ifdef APP
uni.hideTabBar();
@ -11,10 +12,18 @@ onLoad(() => {
</script>
<template>
<view class="w-full h-full">
<navv>
<template #default="{ menu, status }">
<view>
购物车
</view>
</template>
</navv>
<!-- <view class="w-full h-full">
购物车
<customTabBar tab-index="3" />
</view>
</view> -->
</template>
<style lang="scss" scoped></style>

11
store/index.js

@ -12,7 +12,7 @@ import { ref } from "vue";
* @property {string} tabbarBottomHeight - 底部导航高度
* @property {string} tabbarItemWidth - 底部导航每个的宽度
* @property {method} increment - 增加购物车角标数量
* @property {method} getCartListSize - 获取购物车列表数量
* @property {method} getCartTotalQuantity - 获取购物车商品数量
* @property {method} changeCartList - 修改购物车列表
* @property {method} changeShowVoice - 修改语音弹窗是否显示
* @property {method} changeSearchValue - 修改搜索内容
@ -105,8 +105,11 @@ export default defineStore("store",
function changeCartList(info) {
cartList.value = info;
}
function getCartListSize() {
return cartList.value.length;
/**
* 获取购物车商品数量
*/
function getCartTotalQuantity() {
return cartList.value.reduce((acc, cur) => acc + cur.quantity, 0);
}
/**
* 语音弹窗是否显示
@ -129,7 +132,7 @@ export default defineStore("store",
clearSearchValue,
changeHomeType,
changeCartList,
getCartListSize,
getCartTotalQuantity,
changeShowVoice,
};
});
Loading…
Cancel
Save