| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- 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: [],
- 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: [],
- 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 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),
- 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: []
- };
-
- 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_num = 0;
-
- 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;
- }
- }
- // 创建单例
- const cartStore = new CartStore();
- export default cartStore;
|