2024-12-10 10:35:22 +00:00
|
|
|
|
import { url } from '../request';
|
|
|
|
|
|
2024-11-10 07:01:22 +00:00
|
|
|
|
Page({
|
2024-12-01 11:56:54 +00:00
|
|
|
|
data: {
|
|
|
|
|
id: '',
|
|
|
|
|
checked: false, // 全选的状态
|
|
|
|
|
productList: [], // 商品列表
|
2024-12-10 10:35:22 +00:00
|
|
|
|
selectedItems: [], // 每个商品的选中状态
|
|
|
|
|
totalPrice: 0, // 总价
|
2024-12-01 11:56:54 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onShow() {
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
this.setData({
|
|
|
|
|
id: userInfo.id, // 获取 id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/cart/selectByUserId',
|
|
|
|
|
method: 'POST',
|
2024-12-10 10:35:22 +00:00
|
|
|
|
data: { id: this.data.id },
|
2024-12-01 11:56:54 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2024-12-03 11:59:36 +00:00
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({
|
|
|
|
|
content: '您未登录,请先登录。',
|
|
|
|
|
success: () => {
|
|
|
|
|
my.navigateTo({
|
|
|
|
|
url: '/pages/denglu/denglu',
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-12-10 10:35:22 +00:00
|
|
|
|
|
|
|
|
|
yichu(e) {
|
|
|
|
|
const cartId = e.currentTarget.dataset.id; // 获取商品的 cartId
|
|
|
|
|
if (!cartId) {
|
|
|
|
|
console.error('没有找到商品cartId');
|
|
|
|
|
my.alert({ content: '商品ID未找到,请稍后重试' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('需要移除的商品cartId:', cartId);
|
|
|
|
|
|
2024-12-03 11:59:36 +00:00
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
this.setData({
|
|
|
|
|
id: userInfo.id, // 获取 id
|
|
|
|
|
});
|
2024-12-10 10:35:22 +00:00
|
|
|
|
// 发送请求移除商品
|
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/cart/delete',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: { id: cartId }, // 使用 cartId 作为参数
|
|
|
|
|
headers: { 'content-type': 'application/json', 'Cookie': userInfo.cookie },
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log(res);
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
my.alert({ content: '成功移除商品' });
|
|
|
|
|
console.log(res);
|
|
|
|
|
// 更新购物车
|
|
|
|
|
this.updateCartList();
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({ content: '移除商品失败,请稍后重试' });
|
|
|
|
|
console.log(res);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
// 移除后更新
|
|
|
|
|
updateCartList() {
|
|
|
|
|
my.getStorage({
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
const userInfo = res.data;
|
|
|
|
|
|
2024-12-03 11:59:36 +00:00
|
|
|
|
if (userInfo && userInfo.cookie) {
|
|
|
|
|
my.request({
|
2024-12-10 10:35:22 +00:00
|
|
|
|
url: url + '/api/cart/selectByUserId', // 获取最新的购物车数据
|
2024-12-03 11:59:36 +00:00
|
|
|
|
method: 'POST',
|
2024-12-10 10:35:22 +00:00
|
|
|
|
data: {
|
|
|
|
|
id: this.data.id
|
|
|
|
|
}, // 使用当前用户ID
|
|
|
|
|
headers: {
|
2024-12-03 11:59:36 +00:00
|
|
|
|
'content-type': 'application/json',
|
2024-12-10 10:35:22 +00:00
|
|
|
|
'Cookie': userInfo.cookie
|
|
|
|
|
},
|
2024-12-03 11:59:36 +00:00
|
|
|
|
dataType: 'json',
|
|
|
|
|
success: (res) => {
|
2024-12-10 10:35:22 +00:00
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
const cartItems = res.data.data;
|
|
|
|
|
this.fetchProductDetails(cartItems, userInfo.id);
|
|
|
|
|
} else {
|
|
|
|
|
my.alert({ content: '获取购物车数据失败,请稍后重试' });
|
|
|
|
|
}
|
2024-12-01 11:56:54 +00:00
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
|
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2024-12-10 10:35:22 +00:00
|
|
|
|
fetchProductDetails(cartItems, userId) {
|
|
|
|
|
console.log(cartItems, 'zheshiid');
|
2024-12-01 11:56:54 +00:00
|
|
|
|
const promises = cartItems.map((item) => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
my.request({
|
|
|
|
|
url: url + '/api/commodities/getById/commodities',
|
|
|
|
|
method: 'GET',
|
2024-12-10 10:35:22 +00:00
|
|
|
|
data: { id: item.commoditiesId },
|
2024-12-01 11:56:54 +00:00
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.data.code === 0) {
|
2024-12-10 10:35:22 +00:00
|
|
|
|
const productData = res.data.data;
|
|
|
|
|
productData.userId = userId; // 添加 userId
|
|
|
|
|
productData.cartId = item.id; // 将 cartId 添加到商品数据中
|
|
|
|
|
resolve(productData);
|
2024-12-01 11:56:54 +00:00
|
|
|
|
} else {
|
|
|
|
|
reject(`商品信息获取失败: ${res.data.message}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: (error) => {
|
|
|
|
|
reject(error);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-12-10 10:35:22 +00:00
|
|
|
|
|
2024-12-01 11:56:54 +00:00
|
|
|
|
Promise.all(promises)
|
|
|
|
|
.then((productList) => {
|
|
|
|
|
this.setData({
|
|
|
|
|
productList,
|
2024-12-10 10:35:22 +00:00
|
|
|
|
selectedItems: new Array(productList.length).fill(false), // 初始化所有商品的选中状态为 false
|
2024-12-01 11:56:54 +00:00
|
|
|
|
});
|
2024-12-10 10:35:22 +00:00
|
|
|
|
console.log(productList, '这是商品');
|
2024-12-01 11:56:54 +00:00
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error('商品信息获取失败: ', error);
|
|
|
|
|
my.alert({ content: '商品信息获取失败,请稍后重试' });
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2024-12-10 10:35:22 +00:00
|
|
|
|
calculateTotalPrice() {
|
|
|
|
|
const selectedItems = this.data.selectedItems;
|
|
|
|
|
const productList = this.data.productList;
|
|
|
|
|
let totalPrice = 0;
|
|
|
|
|
selectedItems.forEach((selected, index) => {
|
|
|
|
|
if (selected) {
|
|
|
|
|
totalPrice += parseFloat(productList[index].commoditiesPrice);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.setData({
|
|
|
|
|
totalPrice: totalPrice.toFixed(2),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2024-12-01 11:56:54 +00:00
|
|
|
|
toggleChange() {
|
|
|
|
|
const newChecked = !this.data.checked;
|
2024-12-10 10:35:22 +00:00
|
|
|
|
const selectedItems = new Array(this.data.productList.length).fill(newChecked);
|
2024-12-01 11:56:54 +00:00
|
|
|
|
this.setData({
|
|
|
|
|
checked: newChecked,
|
|
|
|
|
selectedItems,
|
|
|
|
|
});
|
2024-12-10 10:35:22 +00:00
|
|
|
|
this.calculateTotalPrice();
|
2024-12-01 11:56:54 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleCheckedChange(e) {
|
2024-12-10 10:35:22 +00:00
|
|
|
|
const index = e.target.dataset.index;
|
2024-12-01 11:56:54 +00:00
|
|
|
|
if (index === undefined) {
|
|
|
|
|
console.error('e.target.dataset.index is undefined');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-10 10:35:22 +00:00
|
|
|
|
const selectedItems = [...this.data.selectedItems];
|
|
|
|
|
selectedItems[index] = e.detail.value;
|
2024-12-01 11:56:54 +00:00
|
|
|
|
this.setData({
|
2024-12-10 10:35:22 +00:00
|
|
|
|
selectedItems,
|
2024-12-01 11:56:54 +00:00
|
|
|
|
});
|
2024-12-10 10:35:22 +00:00
|
|
|
|
this.calculateTotalPrice();
|
2024-12-01 11:56:54 +00:00
|
|
|
|
},
|
2024-11-10 07:01:22 +00:00
|
|
|
|
});
|