class CartStore { constructor() { this.storageKey = 'shopping_cart'; this.currentShopId = ''; // 当前店铺ID } // 设置当前店铺ID setShopId() { this.currentShopId = uni.getStorageSync('shop_id'); } // 获取购物车数据 getCart() { try { const cartStr = uni.getStorageSync(this.storageKey); if (!cartStr) { return {}; } return JSON.parse(cartStr); } catch (e) { console.error('获取购物车失败:', e); return {}; } } // 保存购物车数据 saveCart(cartData) { try { uni.setStorageSync(this.storageKey, JSON.stringify(cartData)); } catch (e) { console.error('保存购物车失败:', e); } } // 获取指定店铺的购物车 getShopCart(shopId = null) { const cart = this.getCart(); const targetShopId = shopId || this.currentShopId; if (!targetShopId) return null; if (!cart[targetShopId]) { cart[targetShopId] = { total_price: 0, //所有价格总和 total_num: 0, goods_list: [], pack_price: 0, //配送费总额 total_goods_money: 0, //商品价格总额 shop_info: null }; this.saveCart(cart); } return cart[targetShopId]; } // 添加商品到购物车 addToCart(goods, goodsSpec = [], quantity = 1) { if (!this.currentShopId) { console.error('未设置店铺ID'); return false; } const cart = this.getCart(); if (!cart[this.currentShopId]) { cart[this.currentShopId] = { total_price: 0, total_num: 0, goods_list: [], pack_price: 0, //配送费总额 total_goods_money: 0, //商品价格总额 shop_info: null }; } const shopCart = cart[this.currentShopId]; // 生成商品唯一标识(结合商品ID和规格ID) const specIdStr = goodsSpec.length > 0 ? goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') : 'no_spec'; const goodsKey = `${goods.goods_id}_${specIdStr}`; // 计算商品单价 const unitPrice = this.calculatePrice(goods, goodsSpec); // 获取商品配送费 const packPrice = parseFloat(goods.pack_price || 0); // 查找是否已存在相同商品 const existingIndex = shopCart.goods_list.findIndex( item => this.getGoodsKey(item) === goodsKey ); if (existingIndex > -1) { // 已存在,更新数量 shopCart.goods_list[existingIndex].goods_number += quantity; } else { // 新商品,添加到列表 shopCart.goods_list.push({ goods_id: goods.goods_id, goods_name: goods.goods_name || '', goods_image: goods.goods_image || '', // price: this.calculatePrice(goods, goodsSpec), price: unitPrice, pack_price: packPrice, // 单个商品配送费 goods_number: quantity, goods_attr: this.getSpecAttr(goodsSpec), goods_spec: goodsSpec, category_id: goods.category_id, goods_desc: goods.goods_desc || '', spec_type: goods.spec_type || 1 }); } // 重新计算总计 this.calculateTotals(shopCart); // 保存到本地存储 this.saveCart(cart); return true; } // 更新商品数量 updateQuantity(goodsId, goodsSpec = [], quantity) { if (!this.currentShopId) return false; const cart = this.getCart(); const shopCart = cart[this.currentShopId]; if (!shopCart) return false; const specIdStr = goodsSpec.length > 0 ? goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') : 'no_spec'; const goodsKey = `${goodsId}_${specIdStr}`; const goodsIndex = shopCart.goods_list.findIndex( item => this.getGoodsKey(item) === goodsKey ); if (goodsIndex > -1) { if (quantity <= 0) { // 数量为0,移除商品 shopCart.goods_list.splice(goodsIndex, 1); } else { // 更新数量 shopCart.goods_list[goodsIndex].goods_number = quantity; } // 重新计算总计 this.calculateTotals(shopCart); this.saveCart(cart); return true; } return false; } // 从购物车移除商品 removeFromCart(goodsId, goodsSpec = []) { return this.updateQuantity(goodsId, goodsSpec, 0); } // 清空当前店铺购物车 clearCurrentShopCart() { if (!this.currentShopId) return false; const cart = this.getCart(); cart[this.currentShopId] = { total_price: 0, total_num: 0, goods_list: [], pack_price: 0, total_goods_money: 0, shop_info: null }; this.saveCart(cart); return true; } // 计算商品价格 calculatePrice(goods, goodsSpec = []) { let price = goods.seckill_money > 0 ? Number(goods.seckill_money) : Number(goods.price); // 如果有规格,累加规格价格 if (goodsSpec.length > 0) { goodsSpec.forEach(spec => { // 这里需要根据实际情况获取规格价格 // 假设规格信息中包含价格 if (spec.price) { price += Number(spec.price) * (spec.number || 1); } }); } return Number(price).toFixed(2); } // 获取规格描述 getSpecAttr(goodsSpec = []) { if (goodsSpec.length === 0) return ''; return goodsSpec.map(spec => spec.name || '').join('、'); } // 生成商品唯一键 getGoodsKey(goodsItem) { const specIdStr = goodsItem.goods_spec && goodsItem.goods_spec.length > 0 ? goodsItem.goods_spec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') : 'no_spec'; return `${goodsItem.goods_id}_${specIdStr}`; } // 计算购物车总计 calculateTotals(shopCart) { // let total_price = 0; let total_goods_money = 0; // 商品总额(不含配送费) let total_pack_price = 0; // 配送费总额 let total_num = 0; // 计算商品总额和总数量 shopCart.goods_list.forEach(item => { const itemGoodsMoney = item.price * item.goods_number; // 商品金额 const itemPackPrice = parseFloat(item.pack_price || 0) * item.goods_number; // 配送费金额 const itemTotal = item.price * item.goods_number; // total_goods_money += itemTotal; // total_num += item.goods_number; total_goods_money += itemGoodsMoney; total_pack_price += itemPackPrice; total_num += item.goods_number; }); // 更新商品总额 shopCart.total_goods_money = parseFloat(total_goods_money.toFixed(2)); // 更新配送费总额 shopCart.pack_price = parseFloat(total_pack_price.toFixed(2)); // 更新商品总数量 shopCart.total_num = total_num; // 计算总价:商品总额 + 配送费 const total_price = shopCart.total_goods_money + (shopCart.pack_price || 0); shopCart.total_price = parseFloat(total_price.toFixed(2)); // shopCart.goods_list.forEach(item => { // total_price += item.price * item.goods_number; // total_num += item.goods_number; // }); // shopCart.total_price = parseFloat(total_price.toFixed(2)); // shopCart.total_num = total_num; } // 获取商品在购物车中的数量 getGoodsQuantity(goodsId, goodsSpec = []) { if (!this.currentShopId) return 0; const cart = this.getCart(); const shopCart = cart[this.currentShopId]; if (!shopCart) return 0; const specIdStr = goodsSpec.length > 0 ? goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') : 'no_spec'; const goodsKey = `${goodsId}_${specIdStr}`; const goodsItem = shopCart.goods_list.find( item => this.getGoodsKey(item) === goodsKey ); return goodsItem ? goodsItem.goods_number : 0; } // 获取指定店铺购物车中某商品的总数量(所有规格合并) getGoodsTotalQuantity(goodsId) { if (!this.currentShopId) return 0; const cart = this.getCart(); const shopCart = cart[this.currentShopId]; if (!shopCart) return 0; let total = 0; shopCart.goods_list.forEach(item => { if (item.goods_id === goodsId) { total += item.goods_number; } }); return total; } // 获取购物车统计数据 getCartStats(shopId = null) { const shopCart = this.getShopCart(shopId); if (!shopCart) { return { total_price: 0, total_num: 0, goods_list: [], pack_price: 0, total_goods_money: 0 }; } return { total_price: shopCart.total_price || 0, total_num: shopCart.total_num || 0, goods_list: shopCart.goods_list || [], pack_price: shopCart.pack_price || 0, total_goods_money: shopCart.total_goods_money || 0 }; } // 获取商品总额(不含配送费) getGoodsTotalMoney(shopId = null) { const shopCart = this.getShopCart(shopId); return shopCart ? (shopCart.total_goods_money || 0) : 0; } // 获取配送费 getPackPrice(shopId = null) { const shopCart = this.getShopCart(shopId); return shopCart ? (shopCart.pack_price || 0) : 0; } // 获取总价(商品总额+配送费) getTotalPrice(shopId = null) { const shopCart = this.getShopCart(shopId); return shopCart ? (shopCart.total_price || 0) : 0; } // 获取单个商品在购物车中的信息 getGoodsInfo(goodsId, goodsSpec = []) { if (!this.currentShopId) return null; const cart = this.getCart(); const shopCart = cart[this.currentShopId]; if (!shopCart) return null; const specIdStr = goodsSpec.length > 0 ? goodsSpec.map(spec => `${spec.id}_${spec.number}`).sort().join('_') : 'no_spec'; const goodsKey = `${goodsId}_${specIdStr}`; return shopCart.goods_list.find( item => this.getGoodsKey(item) === goodsKey ); } } // 创建单例 const cartStore = new CartStore(); export default cartStore;