🐛 修复代码冲突,被错误覆盖掉的 cart.vue、pay.vue、result.vue、confirm.vue

This commit is contained in:
YunaiV
2023-12-16 00:09:01 +08:00
parent b235cb0d8d
commit 77e2719837
9 changed files with 796 additions and 643 deletions
+48 -53
View File
@@ -1,98 +1,93 @@
import { defineStore } from 'pinia';
import cartApi from '@/sheep/api/cart';
import CartApi from '@/sheep/api/trade/cart';
const cart = defineStore({
id: 'cart',
state: () => ({
list: [], // 购物车列表
selectedIds: [], // 已选列表
isAllSelected: false, //是否全选
cartSelectedTotalPrice: '0.00', // 选中项总金额
isAllSelected: false, // 是否全选
totalPriceSelected: 0, // 选中项总金额
}),
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();
const { data, code } = await CartApi.getCartList();
if (code === 0) {
this.list = data.validList;
// 计算各种关联属性
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;
}
});
}
},
// 添加购物车
async add(goodsInfo) {
const { error } = await cartApi.append({
goods_id: goodsInfo.goods_id,
goods_num: goodsInfo.goods_num,
goods_sku_price_id: goodsInfo.id,
// 添加购物项
const { code } = await CartApi.addCart({
skuId: goodsInfo.id,
count: goodsInfo.goods_num,
});
if (error === 0) {
this.getList();
// 刷新购物车列表
if (code === 0) {
await this.getList();
}
},
// 更新购物车
async update(goodsInfo) {
const { error } = await cartApi.update({
const { code } = await CartApi.updateCartCount({
id: goodsInfo.goods_id,
count: goodsInfo.goods_num,
goods_sku_price_id: goodsInfo.goods_sku_price_id,
});
if (error === 0) {
// this.getList();
if (code === 0) {
await this.getList();
}
},
// 移除购物车
async delete(ids) {
if (typeof ids === 'array') {
ids = ids.join(',');
}
const { code } = await cartApi.delete(ids);
const { code } = await CartApi.deleteCart(ids.join(','));
if (code === 0) {
this.selectAll(false);
this.getList();
await this.getList();
}
},
// 选购物车商品
selectSingle(goodsId) {
if (!this.selectedIds.includes(goodsId)) {
this.selectedIds.push(goodsId);
} else {
this.selectedIds.splice(this.selectedIds.indexOf(goodsId), 1);
// 选购物车商品
async selectSingle(goodsId) {
const { code } = await CartApi.updateCartSelected({
ids: [goodsId],
selected: !this.selectedIds.includes(goodsId), // 取反
});
if (code === 0) {
await this.getList();
}
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);
});
// 全选购物车商品
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 = false;
this.cartSelectedTotalPrice = '0.00';
async emptyList() {
await this.delete(this.list.map((item) => item.id));
},
},
persist: {