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_attr: goods.goods_attr, 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); } // 再来一单 - 从订单数据重建购物车 reorderFromOrderData(shopId, orderData) { if (!shopId) { console.error('店铺ID为空'); return false; } // 处理订单数据 const processedData = this.processOrderData(orderData); if (!processedData) { return false; } // 设置当前店铺 this.currentShopId = shopId; const cart = this.getCart(); // 直接替换购物车数据 cart[shopId] = processedData; console.log('处理后的购物车数据:', cart[shopId]); // 保存到本地存储 this.saveCart(cart); // 触发购物车更新事件 if (typeof uni.$emit === 'function') { uni.$emit('cartUpdated', { shop_id: shopId }); } return true; } // 处理订单数据格式转换 processOrderData(orderData) { if (!orderData) return null; // 构建标准购物车数据结构 const processedData = { total_price: parseFloat(orderData.total_price) || 0, total_num: parseInt(orderData.total_num) || 0, goods_list: [], pack_price: parseFloat(orderData.pack_price) || 0, total_goods_money: parseFloat(orderData.total_goods_money) || 0, shop_info: orderData.shop_info || null }; // 处理商品列表 if (orderData.goods_list && Array.isArray(orderData.goods_list)) { processedData.goods_list = orderData.goods_list.map(item => { // 处理规格属性 let goods_attr = ''; let goods_spec = []; if (item.goods_attr) { goods_attr = item.goods_attr; } if (item.goods_spec && Array.isArray(item.goods_spec)) { goods_spec = item.goods_spec.map(spec => ({ id: spec.id || spec.spec_id || 0, number: spec.number || 1, name: spec.name || spec.spec_name || '', price: spec.price || 0 })); // 如果没有goods_attr,从规格生成 if (!goods_attr && goods_spec.length > 0) { goods_attr = goods_spec.map(spec => spec.number > 1 ? `${spec.name}×${spec.number}` : spec.name ).join('、'); } } return { goods_id: item.goods_id, goods_name: item.goods_name || '', goods_image: item.goods_image || '', price: parseFloat(item.price) || 0, pack_price: parseFloat(item.pack_price) || 0, goods_number: parseInt(item.goods_number) || 1, goods_attr: goods_attr, goods_spec: goods_spec, category_id: item.category_id || 0, goods_desc: item.goods_desc || '', spec_type: item.spec_type || 1 }; }); } return processedData; } // 清空当前店铺购物车 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.number > 1 ? `${spec.name}x${spec.number}` : 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;