185 lines
5.1 KiB
JavaScript
185 lines
5.1 KiB
JavaScript
import {url} from '../request'
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
checked: false, // 全选的状态
|
|
productList: [], // 商品列表
|
|
selectedItems: [] // 每个商品的选中状态
|
|
},
|
|
|
|
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',
|
|
data: {
|
|
id: this.data.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);
|
|
}
|
|
console.log(res);
|
|
console.log(this.data.id);
|
|
},
|
|
fail: (error) => {
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
},
|
|
});
|
|
} else {
|
|
my.alert({
|
|
content: '您未登录,请先登录。',
|
|
success: () => {
|
|
my.navigateTo({
|
|
url: '/pages/denglu/denglu',
|
|
});
|
|
},
|
|
});
|
|
}
|
|
},
|
|
});
|
|
},
|
|
yichu(){
|
|
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/delete',
|
|
method: 'POST',
|
|
data: {
|
|
id: item.commoditiesId,
|
|
},
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
'Cookie': userInfo.cookie,
|
|
},
|
|
dataType: 'json',
|
|
success: (res) => {
|
|
alert('成功移除商品')
|
|
console.log(res);
|
|
},
|
|
fail: (error) => {
|
|
console.error('请求失败: ', JSON.stringify(error));
|
|
my.alert({ content: '请求失败,请稍后重试' });
|
|
},
|
|
});
|
|
} else {
|
|
my.alert({
|
|
content: '您未登录,请先登录。',
|
|
success: () => {
|
|
my.navigateTo({
|
|
url: '/pages/denglu/denglu',
|
|
});
|
|
},
|
|
});
|
|
}
|
|
},
|
|
});
|
|
},
|
|
|
|
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) {
|
|
resolve(res.data.data);
|
|
console.log(res);
|
|
console.log('ajsdhjasoijdkas');
|
|
console.log(this.data.id)
|
|
} else {
|
|
reject(`商品信息获取失败: ${res.data.message}`);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
reject(error);
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
Promise.all(promises)
|
|
.then((productList) => {
|
|
this.setData({
|
|
productList,
|
|
selectedItems: new Array(productList.length).fill(false), // 初始化所有商品的选中状态
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.error('商品信息获取失败: ', error);
|
|
my.alert({ content: '商品信息获取失败,请稍后重试' });
|
|
});
|
|
},
|
|
onChange(value, e) {
|
|
console.log(value, e);
|
|
},
|
|
|
|
// 全选控制
|
|
toggleChange() {
|
|
const newChecked = !this.data.checked;
|
|
const selectedItems = new Array(this.data.productList.length).fill(newChecked); // 全选时,更新所有商品的选中状态
|
|
this.setData({
|
|
checked: newChecked,
|
|
selectedItems,
|
|
});
|
|
},
|
|
|
|
// 单独控制每个商品的选中状态
|
|
handleCheckedChange(e) {
|
|
console.log(e); // 打印出 e 对象,检查其结构
|
|
const index = e.target.dataset.index; // 获取当前商品的索引
|
|
if (index === undefined) {
|
|
console.error('e.target.dataset.index is undefined');
|
|
return;
|
|
}
|
|
const selectedItems = [...this.data.selectedItems]; // 获取当前的选中状态
|
|
selectedItems[index] = e.detail.value; // 更新该商品的选中状态
|
|
this.setData({
|
|
selectedItems, // 更新选中状态
|
|
});
|
|
},
|
|
|
|
// 计算总价
|
|
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),
|
|
});
|
|
},
|
|
});
|
|
|