217 lines
6.5 KiB
JavaScript
217 lines
6.5 KiB
JavaScript
![]() |
import {url} from '../request'
|
|||
|
Page({
|
|||
|
data: {
|
|||
|
id: '',
|
|||
|
productList: [], // 商品列表
|
|||
|
selectedItems: [], // 每个商品的选中状态
|
|||
|
totalPrice: 0, // 总价
|
|||
|
checkAll: false, // 全选标志
|
|||
|
hhh:[]
|
|||
|
},
|
|||
|
|
|||
|
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) => {
|
|||
|
console.log(res);
|
|||
|
if (res.data.code === 0) {
|
|||
|
const cartItems = res.data.data;
|
|||
|
this.fetchProductDetails(cartItems);
|
|||
|
}
|
|||
|
},
|
|||
|
fail: (error) => {
|
|||
|
console.error('请求失败: ', JSON.stringify(error));
|
|||
|
my.alert({ content: '请求失败,请稍后重试' });
|
|||
|
},
|
|||
|
});
|
|||
|
} else {
|
|||
|
my.alert({
|
|||
|
content: '您未登录,请先登录。',
|
|||
|
success: () => {
|
|||
|
my.navigateTo({
|
|||
|
url: '/pages/denglu/denglu',
|
|||
|
});
|
|||
|
},
|
|||
|
});
|
|||
|
}
|
|||
|
},
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
// 获取商品详细信息
|
|||
|
fetchProductDetails(cartItems, userId) {
|
|||
|
console.log(cartItems, 'zheshiid');
|
|||
|
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.userId = userId; // 添加 userId
|
|||
|
productData.cartId = item.id; // 将 cartId 添加到商品数据中
|
|||
|
resolve(productData);
|
|||
|
} else {
|
|||
|
reject(`商品信息获取失败: ${res.data.message}`);
|
|||
|
}
|
|||
|
},
|
|||
|
fail: (error) => {
|
|||
|
reject(error);
|
|||
|
},
|
|||
|
});
|
|||
|
});
|
|||
|
});
|
|||
|
|
|||
|
Promise.all(promises)
|
|||
|
.then((productList) => {
|
|||
|
this.setData({
|
|||
|
productList,
|
|||
|
selectedItems: new Array(productList.length).fill(false), // 初始化所有商品的选中状态为 false
|
|||
|
});
|
|||
|
console.log(productList, '这是商品');
|
|||
|
})
|
|||
|
.catch((error) => {
|
|||
|
console.error('商品信息获取失败: ', error);
|
|||
|
my.alert({ content: '商品信息获取失败,请稍后重试' });
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
|
|||
|
// 计算 checkAll 是否选中
|
|||
|
checkAll() {
|
|||
|
return this.data.selectedItems.every(item => item); // 如果所有项都选中,返回 true
|
|||
|
},
|
|||
|
|
|||
|
// 处理单个复选框的选中状态变化
|
|||
|
onChange(event) {
|
|||
|
const selectedItems = event.detail.value; // 获取选中的商品ID列表
|
|||
|
const updatedSelectedItems = this.data.productList.map((item, index) => {
|
|||
|
return selectedItems.includes(item.id); // 更新每个商品的选中状态
|
|||
|
});
|
|||
|
this.setData({
|
|||
|
selectedItems: updatedSelectedItems
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
// 处理全选框的选中状态变化
|
|||
|
onCheckAllChange(event) {
|
|||
|
const allSelected = event.detail.value; // 获取全选框的状态
|
|||
|
const updatedSelectedItems = this.data.productList.map(() => allSelected); // 全选或全不选
|
|||
|
this.setData({
|
|||
|
selectedItems: updatedSelectedItems
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
// 计算合计
|
|||
|
calculateTotalPrice() {
|
|||
|
const total = this.data.productList.reduce((sum, item, index) => {
|
|||
|
if (this.data.selectedItems[index]) {
|
|||
|
sum += item.commoditiesPrice; // 累加选中商品的价格
|
|||
|
}
|
|||
|
return sum;
|
|||
|
}, 0);
|
|||
|
this.setData({
|
|||
|
totalPrice: total.toFixed(2) // 保留两位小数
|
|||
|
});
|
|||
|
},
|
|||
|
|
|||
|
// 处理移除商品
|
|||
|
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;
|
|||
|
this.setData({
|
|||
|
id: userInfo.id, // 获取 id
|
|||
|
});
|
|||
|
// 发送请求移除商品
|
|||
|
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;
|
|||
|
|
|||
|
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: '请求失败,请稍后重试' });
|
|||
|
},
|
|||
|
});
|
|||
|
}
|
|||
|
},
|
|||
|
});
|
|||
|
},
|
|||
|
});
|