2025-01-13 13:07:00 +00:00
|
|
|
|
import { url } from '../request';
|
|
|
|
|
|
2024-12-17 11:46:10 +00:00
|
|
|
|
Page({
|
|
|
|
|
data: {
|
2025-01-13 13:07:00 +00:00
|
|
|
|
id: '',
|
|
|
|
|
productList: [], // 商品列表
|
|
|
|
|
select_all: false,
|
|
|
|
|
checkbox_productListid: '',
|
|
|
|
|
totalPrice: 0,
|
2024-12-17 11:46:10 +00:00
|
|
|
|
},
|
2025-01-13 13:07:00 +00:00
|
|
|
|
// 计算总价
|
|
|
|
|
calculateTotalPrice() {
|
|
|
|
|
const totalPrice = this.data.productList
|
|
|
|
|
.filter(item => item.checked) // 只计算勾选的商品
|
|
|
|
|
.reduce((sum, item) => sum + item.quantity * item.commoditiesPrice, 0);
|
|
|
|
|
|
|
|
|
|
// 格式化总价为两位小数
|
|
|
|
|
const formattedTotalPrice = totalPrice.toFixed(2);
|
|
|
|
|
|
|
|
|
|
// 更新 totalPrice
|
|
|
|
|
this.setData({ totalPrice: formattedTotalPrice });
|
|
|
|
|
},
|
|
|
|
|
// 增加商品数量
|
|
|
|
|
increaseQuantity(e) {
|
|
|
|
|
const { index } = e.currentTarget.dataset; // 获取当前商品的索引
|
|
|
|
|
const updatedProductList = [...this.data.productList];
|
|
|
|
|
const item = updatedProductList[index];
|
|
|
|
|
// 增加数量
|
|
|
|
|
if (item.quantity < 999) {
|
|
|
|
|
item.quantity += 1;
|
|
|
|
|
}
|
|
|
|
|
this.setData({ productList: updatedProductList });
|
|
|
|
|
this.calculateTotalPrice(); // 重新计算总价
|
|
|
|
|
},
|
|
|
|
|
// 减少商品数量
|
|
|
|
|
decreaseQuantity(e) {
|
|
|
|
|
const { index } = e.currentTarget.dataset; // 获取当前商品的索引
|
|
|
|
|
const updatedProductList = [...this.data.productList];
|
|
|
|
|
const item = updatedProductList[index];
|
|
|
|
|
// 减少数量
|
|
|
|
|
if (item.quantity > 1) {
|
|
|
|
|
item.quantity -= 1;
|
|
|
|
|
}
|
|
|
|
|
this.setData({ productList: updatedProductList });
|
|
|
|
|
this.calculateTotalPrice(); // 重新计算总价
|
|
|
|
|
},
|
|
|
|
|
// 全选/取消全选
|
|
|
|
|
selectall(e) {
|
|
|
|
|
const newSelectAll = !this.data.select_all;
|
|
|
|
|
const updatedProductList = this.data.productList.map(item => ({
|
|
|
|
|
...item,
|
|
|
|
|
checked: newSelectAll,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const checkbox_productListid = newSelectAll
|
|
|
|
|
? updatedProductList.map(item => item.cartId).join(',')
|
|
|
|
|
: '';
|
|
|
|
|
|
2024-12-17 11:46:10 +00:00
|
|
|
|
this.setData({
|
2025-01-13 13:07:00 +00:00
|
|
|
|
productList: updatedProductList,
|
|
|
|
|
select_all: newSelectAll,
|
|
|
|
|
checkbox_productListid,
|
2024-12-17 11:46:10 +00:00
|
|
|
|
});
|
2025-01-13 13:07:00 +00:00
|
|
|
|
console.log("arr=", checkbox_productListid);
|
|
|
|
|
this.calculateTotalPrice(); // 重新计算总价
|
|
|
|
|
const selectedProducts = updatedProductList.filter(item => item.checked);
|
|
|
|
|
this.setData({
|
|
|
|
|
selectedProducts, // 存储勾选的商品信息
|
|
|
|
|
});
|
|
|
|
|
console.log(selectedProducts);
|
|
|
|
|
},
|
|
|
|
|
checkboxChange(e) {
|
|
|
|
|
const { value } = e.detail; // 当前选中的值列表
|
|
|
|
|
const updatedProductList = this.data.productList.map(item => ({
|
|
|
|
|
...item,
|
|
|
|
|
checked: value.includes(item.cartId.toString()),
|
|
|
|
|
}));
|
|
|
|
|
const select_all = updatedProductList.every(item => item.checked);
|
|
|
|
|
this.setData({
|
|
|
|
|
productList: updatedProductList,
|
|
|
|
|
select_all,
|
2024-12-17 11:46:10 +00:00
|
|
|
|
});
|
2025-01-13 13:07:00 +00:00
|
|
|
|
this.calculateTotalPrice(); // 更新总价
|
|
|
|
|
// 提取勾选的商品
|
|
|
|
|
const selectedProducts = updatedProductList.filter(item => item.checked);
|
|
|
|
|
this.setData({
|
|
|
|
|
selectedProducts, // 存储勾选的商品信息
|
|
|
|
|
});
|
|
|
|
|
console.log(selectedProducts,'askldjaslkdaslkdjklas');
|
|
|
|
|
},
|
|
|
|
|
// 获取商品数据
|
|
|
|
|
fetchProductDetails(cartItems) {
|
|
|
|
|
const promises = cartItems.map((item) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/commodities/getById/commodities',
|
|
|
|
|
method: 'GET',
|
|
|
|
|
data: { id: item.commoditiesId },
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
const productData = res.data.data;
|
|
|
|
|
productData.cartId = item.id; // 将 cartId 添加到商品数据中
|
|
|
|
|
productData.quantity = item.quantity || 1; // 初始化数量
|
|
|
|
|
resolve(productData);
|
|
|
|
|
} else {
|
|
|
|
|
reject(`商品信息获取失败: ${res.data.message}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
reject(error);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Promise.all(promises)
|
|
|
|
|
.then((productList) => {
|
|
|
|
|
// 确保没有重复商品
|
|
|
|
|
const mergedProductList = [];
|
|
|
|
|
productList.forEach((product) => {
|
|
|
|
|
const existingProduct = mergedProductList.find(
|
|
|
|
|
(item) => item.commoditiesId === product.commoditiesId
|
|
|
|
|
);
|
|
|
|
|
if (existingProduct) {
|
|
|
|
|
// 如果已存在相同商品,则累加数量
|
|
|
|
|
existingProduct.quantity += product.quantity;
|
|
|
|
|
} else {
|
|
|
|
|
// 如果是新商品,则添加
|
|
|
|
|
mergedProductList.push(product);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setData({ productList: mergedProductList });
|
|
|
|
|
this.calculateTotalPrice(); // 初始化总价
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error('商品信息获取失败: ', error);
|
|
|
|
|
my.alert({ content: '商品信息获取失败,请稍后重试' });
|
|
|
|
|
});
|
2024-12-17 11:46:10 +00:00
|
|
|
|
},
|
2025-01-13 13:07:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 页面加载时获取商品数据
|
|
|
|
|
onShow() {
|
|
|
|
|
this.setData({
|
|
|
|
|
select_all: false,
|
|
|
|
|
selectedProducts:[],
|
|
|
|
|
});
|
2024-12-17 11:46:10 +00:00
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
2025-01-13 13:07:00 +00:00
|
|
|
|
url: url + '/api/cart/selectByUserId',
|
2024-12-17 11:46:10 +00:00
|
|
|
|
method: 'POST',
|
2025-01-13 13:07:00 +00:00
|
|
|
|
data: {
|
|
|
|
|
id: userInfo.id
|
|
|
|
|
},
|
2024-12-17 11:46:10 +00:00
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
'Cookie': userInfo.cookie,
|
|
|
|
|
},
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
2025-01-13 13:07:00 +00:00
|
|
|
|
console.log(res,'hhhhhhhhhhhh');
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
console.log(res);
|
|
|
|
|
const cartItems = res.data.data;
|
|
|
|
|
this.fetchProductDetails(cartItems);
|
|
|
|
|
} else {
|
2024-12-17 11:46:10 +00:00
|
|
|
|
my.alert({
|
2025-01-13 13:07:00 +00:00
|
|
|
|
content: '登录信息已过期,请重新登录',
|
2024-12-17 11:46:10 +00:00
|
|
|
|
});
|
|
|
|
|
my.navigateTo({
|
2025-01-13 13:07:00 +00:00
|
|
|
|
url: '/pages/denglu/denglu',
|
2024-12-17 11:46:10 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-01-13 13:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
else{
|
2024-12-17 11:46:10 +00:00
|
|
|
|
my.alert({
|
2025-01-13 13:07:00 +00:00
|
|
|
|
content:'您未登录,请先登录'
|
|
|
|
|
})
|
|
|
|
|
my.navigateTo({
|
|
|
|
|
url:'/pages/denglu/denglu'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
yichu(e) {
|
|
|
|
|
const cartId = e.currentTarget.dataset.id; // 获取商品的 cartId
|
|
|
|
|
if (!cartId) {
|
|
|
|
|
console.error('没有找到商品cartId');
|
|
|
|
|
my.alert({ content: '商品ID未找到,请稍后重试' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('需要移除的商品cartId:', cartId);
|
|
|
|
|
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
// 发送请求移除商品
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/cart/delete',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
id: cartId, // 商品 cartId
|
|
|
|
|
userId: userInfo.id, // 当前用户ID
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
'Cookie': userInfo.cookie,
|
|
|
|
|
},
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
my.alert({ content: '成功移除商品' });
|
|
|
|
|
|
|
|
|
|
// 从 productList 中移除对应的商品
|
|
|
|
|
const updatedProductList = this.data.productList.filter(
|
|
|
|
|
(item) => item.cartId !== cartId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.setData({ productList: updatedProductList });
|
|
|
|
|
this.calculateTotalPrice(); // 更新总价
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({ content: '移除商品失败,请稍后重试' });
|
|
|
|
|
console.log(res);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
2024-12-17 11:46:10 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2025-01-13 13:07:00 +00:00
|
|
|
|
// 移除后更新
|
|
|
|
|
updateCartList() {
|
|
|
|
|
this.setData({ select_all: false });
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/cart/selectByUserId', // 获取最新的购物车数据
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
id: this.data.id
|
|
|
|
|
}, // 使用当前用户ID
|
|
|
|
|
headers: {
|
|
|
|
|
'content-type': 'application/json',
|
|
|
|
|
'Cookie': userInfo.cookie
|
|
|
|
|
},
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
const cartItems = res.data.data;
|
|
|
|
|
this.fetchProductDetails(cartItems, userInfo.id);
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({ content: '获取购物车数据失败,请稍后重试' });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-12-17 11:46:10 +00:00
|
|
|
|
},
|
2025-01-13 13:07:00 +00:00
|
|
|
|
jiesuan() {
|
|
|
|
|
const products = this.data.selectedProducts;
|
|
|
|
|
if(!products || products.length === 0){
|
|
|
|
|
my.alert({
|
|
|
|
|
content:'请选择商品'
|
|
|
|
|
})
|
|
|
|
|
}else{
|
|
|
|
|
const productsStr = JSON.stringify(products)
|
|
|
|
|
const prices =this.data.totalPrice
|
|
|
|
|
my.navigateTo({
|
|
|
|
|
url: '/pages/zhifujiemian/zhifujiemian?products='+encodeURIComponent(productsStr)+ '&prices=' + encodeURIComponent(prices)
|
|
|
|
|
});
|
2024-12-17 11:46:10 +00:00
|
|
|
|
}
|
2025-01-13 13:07:00 +00:00
|
|
|
|
},
|
|
|
|
|
});
|