98 lines
2.8 KiB
JavaScript
98 lines
2.8 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
const api_cart = require("../api/cart.js");
|
|
const useCartStore = common_vendor.defineStore("cart", () => {
|
|
const items = common_vendor.ref([]);
|
|
const checkedItems = common_vendor.computed(() => items.value.filter((item) => item.checked));
|
|
const totalAmount = common_vendor.computed(
|
|
() => checkedItems.value.reduce((sum, item) => {
|
|
var _a;
|
|
return sum + (((_a = item.specData) == null ? void 0 : _a.totalPrice) || 0) * item.quantity;
|
|
}, 0)
|
|
);
|
|
async function fetchCart() {
|
|
const token = common_vendor.index.getStorageSync("token");
|
|
if (!token)
|
|
return;
|
|
try {
|
|
const list = await api_cart.getCartList();
|
|
items.value = list.map((row) => ({
|
|
id: row.id,
|
|
userId: 0,
|
|
productId: row.product_id,
|
|
specDataId: row.spec_data_id,
|
|
quantity: row.quantity,
|
|
checked: true,
|
|
product: {
|
|
id: row.product_id,
|
|
name: row.product_name,
|
|
basePrice: row.base_price,
|
|
styleNo: row.style_no,
|
|
bannerImages: typeof row.banner_images === "string" ? JSON.parse(row.banner_images || "[]") : row.banner_images || [],
|
|
thumb: row.thumb || ""
|
|
},
|
|
specData: {
|
|
id: row.spec_data_id,
|
|
modelName: row.model_name,
|
|
fineness: row.fineness,
|
|
mainStone: row.main_stone,
|
|
ringSize: row.ring_size,
|
|
goldTotalWeight: row.gold_total_weight || 0,
|
|
totalPrice: row.unit_price || 0
|
|
}
|
|
}));
|
|
} catch {
|
|
}
|
|
}
|
|
function addToCart(item) {
|
|
items.value.push(item);
|
|
api_cart.addToCart({
|
|
productId: item.productId,
|
|
specDataId: item.specDataId,
|
|
quantity: item.quantity
|
|
}).catch(() => {
|
|
});
|
|
}
|
|
function removeFromCart(id) {
|
|
const index = items.value.findIndex((item) => item.id === id);
|
|
if (index !== -1) {
|
|
items.value.splice(index, 1);
|
|
api_cart.deleteCartItem(id).catch(() => {
|
|
});
|
|
}
|
|
}
|
|
function updateQuantity(id, quantity) {
|
|
const item = items.value.find((item2) => item2.id === id);
|
|
if (item) {
|
|
item.quantity = quantity;
|
|
api_cart.updateCartItem(id, { quantity }).catch(() => {
|
|
});
|
|
}
|
|
}
|
|
function toggleCheck(id) {
|
|
const item = items.value.find((item2) => item2.id === id);
|
|
if (item) {
|
|
item.checked = !item.checked;
|
|
}
|
|
}
|
|
function toggleCheckAll() {
|
|
const allChecked = items.value.every((item) => item.checked);
|
|
items.value.forEach((item) => {
|
|
item.checked = !allChecked;
|
|
});
|
|
}
|
|
return {
|
|
items,
|
|
checkedItems,
|
|
totalAmount,
|
|
fetchCart,
|
|
addToCart,
|
|
removeFromCart,
|
|
updateQuantity,
|
|
toggleCheck,
|
|
toggleCheckAll
|
|
};
|
|
});
|
|
exports.useCartStore = useCartStore;
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/store/cart.js.map
|