| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | import { defineStore } from 'pinia';import cartApi from '@/sheep/api/cart';const cart = defineStore({  id: 'cart',  state: () => ({    list: [], // 购物车列表    selectedIds: [], // 已选列表    isAllSelected: false, //是否全选    cartSelectedTotalPrice: '0.00', // 选中项总金额  }),  getters: {    totalPriceSelected: (state) => {      let price = 0;      if (!state.selectedIds.length) return price.toFixed(2);      state.list.forEach((item) => {        price += state.selectedIds.includes(item.id)          ? Number(item.sku.price/100) * item.count          : 0;      });      return price.toFixed(2);    },  },  actions: {    // 获取购物车列表    async getList() {      const { data, code } = await cartApi.list();      if (code === 0) {        this.list = data.validList;      }    },    // 添加购物车    async add(goodsInfo) {      const { error } = await cartApi.append({        goods_id: goodsInfo.goods_id,        goods_num: goodsInfo.goods_num,        goods_sku_price_id: goodsInfo.id,      });      if (error === 0) {        this.getList();      }    },    // 更新购物车    async update(goodsInfo) {      const { error } = await cartApi.update({        id: goodsInfo.goods_id,        count: goodsInfo.goods_num,        goods_sku_price_id: goodsInfo.goods_sku_price_id,      });      if (error === 0) {        // this.getList();      }    },    // 移除购物车    async delete(ids) {      if (typeof ids === 'array') {        ids = ids.join(',');      }      const { code } = await cartApi.delete(ids);      if (code === 0) {        this.selectAll(false);        this.getList();      }    },    // 选择购物车商品    selectSingle(goodsId) {      if (!this.selectedIds.includes(goodsId)) {        this.selectedIds.push(goodsId);      } else {        this.selectedIds.splice(this.selectedIds.indexOf(goodsId), 1);      }      this.isAllSelected = this.selectedIds.length === this.list.length;    },    // 全选    selectAll(flag) {      this.isAllSelected = flag;      if (!flag) {        this.selectedIds = [];      } else {        this.list.forEach((item) => {          this.selectedIds.push(item.id);        });      }    },    // 清空购物车    emptyList() {      this.list = [];      this.selectedIds = [];      this.isAllSelected = false;      this.cartSelectedTotalPrice = '0.00';    },  },  persist: {    enabled: true,    strategies: [      {        key: 'cart-store',      },    ],  },});export default cart;
 |