2023-12-13 03:35:02 +00:00
|
|
|
import { defineStore } from 'pinia';
|
2023-12-15 16:09:01 +00:00
|
|
|
import CartApi from '@/sheep/api/trade/cart';
|
2023-12-13 03:35:02 +00:00
|
|
|
|
|
|
|
const cart = defineStore({
|
|
|
|
id: 'cart',
|
|
|
|
state: () => ({
|
|
|
|
list: [], // 购物车列表
|
|
|
|
selectedIds: [], // 已选列表
|
2023-12-15 16:09:01 +00:00
|
|
|
isAllSelected: false, // 是否全选
|
|
|
|
totalPriceSelected: 0, // 选中项总金额
|
2023-12-13 03:35:02 +00:00
|
|
|
}),
|
|
|
|
actions: {
|
|
|
|
// 获取购物车列表
|
|
|
|
async getList() {
|
2023-12-15 16:09:01 +00:00
|
|
|
const { data, code } = await CartApi.getCartList();
|
2023-12-13 03:35:02 +00:00
|
|
|
if (code === 0) {
|
|
|
|
this.list = data.validList;
|
2023-12-15 16:09:01 +00:00
|
|
|
|
|
|
|
// 计算各种关联属性
|
|
|
|
this.selectedIds = [];
|
|
|
|
this.isAllSelected = true;
|
|
|
|
this.totalPriceSelected = 0;
|
|
|
|
this.list.forEach((item) => {
|
|
|
|
if (item.selected) {
|
|
|
|
this.selectedIds.push(item.id);
|
|
|
|
this.totalPriceSelected += item.count * item.sku.price;
|
|
|
|
} else {
|
|
|
|
this.isAllSelected = false;
|
|
|
|
}
|
|
|
|
});
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-15 16:09:01 +00:00
|
|
|
|
2023-12-13 03:35:02 +00:00
|
|
|
// 添加购物车
|
|
|
|
async add(goodsInfo) {
|
2023-12-15 16:09:01 +00:00
|
|
|
// 添加购物项
|
|
|
|
const { code } = await CartApi.addCart({
|
|
|
|
skuId: goodsInfo.id,
|
|
|
|
count: goodsInfo.goods_num,
|
2023-12-13 03:35:02 +00:00
|
|
|
});
|
2023-12-15 16:09:01 +00:00
|
|
|
// 刷新购物车列表
|
|
|
|
if (code === 0) {
|
|
|
|
await this.getList();
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 更新购物车
|
|
|
|
async update(goodsInfo) {
|
2023-12-15 16:09:01 +00:00
|
|
|
const { code } = await CartApi.updateCartCount({
|
2023-12-13 03:35:02 +00:00
|
|
|
id: goodsInfo.goods_id,
|
|
|
|
count: goodsInfo.goods_num,
|
|
|
|
});
|
2023-12-15 16:09:01 +00:00
|
|
|
if (code === 0) {
|
|
|
|
await this.getList();
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 移除购物车
|
|
|
|
async delete(ids) {
|
2024-08-13 13:57:04 +00:00
|
|
|
let idsTemp = '';
|
|
|
|
if (Array.isArray(ids)) {
|
|
|
|
idsTemp = ids.join(',');
|
|
|
|
} else {
|
|
|
|
idsTemp = ids;
|
|
|
|
}
|
|
|
|
const { code } = await CartApi.deleteCart(idsTemp);
|
2023-12-13 03:35:02 +00:00
|
|
|
if (code === 0) {
|
2023-12-15 16:09:01 +00:00
|
|
|
await this.getList();
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-12-15 16:09:01 +00:00
|
|
|
// 单选购物车商品
|
|
|
|
async selectSingle(goodsId) {
|
|
|
|
const { code } = await CartApi.updateCartSelected({
|
|
|
|
ids: [goodsId],
|
|
|
|
selected: !this.selectedIds.includes(goodsId), // 取反
|
|
|
|
});
|
|
|
|
if (code === 0) {
|
|
|
|
await this.getList();
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-12-15 16:09:01 +00:00
|
|
|
// 全选购物车商品
|
|
|
|
async selectAll(flag) {
|
|
|
|
const { code } = await CartApi.updateCartSelected({
|
|
|
|
ids: this.list.map((item) => item.id),
|
|
|
|
selected: flag
|
|
|
|
});
|
|
|
|
if (code === 0) {
|
|
|
|
await this.getList();
|
2023-12-13 03:35:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-12-28 14:59:14 +00:00
|
|
|
// 清空购物车。注意,仅用于用户退出时,重置数据
|
|
|
|
emptyList() {
|
|
|
|
this.list = [];
|
|
|
|
this.selectedIds = [];
|
|
|
|
this.isAllSelected = true;
|
|
|
|
this.totalPriceSelected = 0;
|
2023-12-13 03:35:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
persist: {
|
|
|
|
enabled: true,
|
|
|
|
strategies: [
|
|
|
|
{
|
|
|
|
key: 'cart-store',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default cart;
|