cart.js
2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { defineStore } from 'pinia';
import CartApi from '@/sheep/api/trade/cart';
const cart = defineStore({
id: 'cart',
state: () => ({
list: [], // 购物车列表(invalidList + validList)
selectedIds: [], // 已选列表
isAllSelected: false, // 是否全选
totalPriceSelected: 0, // 选中项总金额
newList: [], // 除去已下架的购物车列表(validList)
editMode: false, // 是否是编辑模式
}),
actions: {
// 获取购物车列表
async getList() {
const { data, code } = await CartApi.getCartList();
if (code === 0) {
this.list = [...data.validList, ...data.invalidList];
this.newList = data.validList;
// 计算各种关联属性
this.selectedIds = [];
this.isAllSelected = true;
this.totalPriceSelected = 0;
(this.editMode ? this.list : this.newList).forEach((item) => {
if (item.selected) {
this.selectedIds.push(item.id);
this.totalPriceSelected += item.count * item.sku?.price;
} else {
this.isAllSelected = false;
}
});
}
},
onChangeEditMode(flag) {
this.editMode = flag;
this.selectedIds = [];
this.getList();
},
// 添加购物车
async add(goodsInfo) {
// 添加购物项
const { code } = await CartApi.addCart({
skuId: goodsInfo.id,
count: goodsInfo.goods_num,
});
// 刷新购物车列表
if (code === 0) {
await this.getList();
}
},
// 更新购物车
async update(goodsInfo) {
const { code } = await CartApi.updateCartCount({
id: goodsInfo.goods_id,
count: goodsInfo.goods_num,
});
if (code === 0) {
await this.getList();
}
},
// 移除购物车
async delete(ids) {
let idsTemp = '';
if (Array.isArray(ids)) {
idsTemp = ids.join(',');
} else {
idsTemp = ids;
}
const { code } = await CartApi.deleteCart(idsTemp);
if (code === 0) {
await this.getList();
}
},
// 单选购物车商品
async selectSingle(goodsId) {
const { code } = await CartApi.updateCartSelected({
ids: [goodsId],
selected: !this.selectedIds.includes(goodsId), // 取反
});
if (code === 0) {
await this.getList();
}
},
// 全选购物车商品
async selectAll(flag) {
const { code } = await CartApi.updateCartSelected({
ids: this.list.map((item) => item.id),
selected: flag,
});
if (code === 0) {
await this.getList();
}
},
// 清空购物车。注意,仅用于用户退出时,重置数据
emptyList() {
this.list = [];
this.selectedIds = [];
this.isAllSelected = true;
this.totalPriceSelected = 0;
},
},
persist: {
enabled: true,
strategies: [
{
key: 'cart-store',
},
],
},
});
export default cart;