提交
This commit is contained in:
281
甲情_甲意/miniprogram/pages/gouwuche/gouwuche.js
Normal file
281
甲情_甲意/miniprogram/pages/gouwuche/gouwuche.js
Normal file
@ -0,0 +1,281 @@
|
||||
import { url } from '../request';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
id: '',
|
||||
productList: [], // 商品列表
|
||||
select_all: false,
|
||||
checkbox_productListid: '',
|
||||
totalPrice: 0,
|
||||
},
|
||||
// 计算总价
|
||||
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(',')
|
||||
: '';
|
||||
|
||||
this.setData({
|
||||
productList: updatedProductList,
|
||||
select_all: newSelectAll,
|
||||
checkbox_productListid,
|
||||
});
|
||||
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,
|
||||
});
|
||||
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.cartVO.commoditiesId },
|
||||
headers: { 'content-type': 'application/json' },
|
||||
success: (res) => {
|
||||
console.log(res,'在这家');
|
||||
if (res.data.code === 0) {
|
||||
const productData = res.data.data;
|
||||
productData.cartId = item.cartVO.id; // 将 cartId 添加到商品数据中
|
||||
productData.quantity = 1; // 默认数量为 1
|
||||
productData.name = item.business.businessName;
|
||||
resolve(productData);
|
||||
} else {
|
||||
reject(`商品信息获取失败: ${res.data.message}`);
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
reject(error);
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Promise.all(promises)
|
||||
.then((productList) => {
|
||||
this.setData({ productList });
|
||||
this.calculateTotalPrice(); // 初始化总价
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('商品信息获取失败: ', error);
|
||||
my.alert({ content: '商品信息获取失败,请稍后重试' });
|
||||
});
|
||||
},
|
||||
|
||||
// 页面加载时获取商品数据
|
||||
onShow() {
|
||||
this.setData({
|
||||
select_all: false,
|
||||
selectedProducts:[],
|
||||
});
|
||||
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: userInfo.id
|
||||
},
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
dataType: 'json',
|
||||
success: (res) => {
|
||||
console.log(res,'hhhhhhhhhhhh');
|
||||
if (res.data.code === 0) {
|
||||
console.log(res);
|
||||
const cartItems = res.data.data;
|
||||
this.fetchProductDetails(cartItems);
|
||||
} else {
|
||||
my.alert({
|
||||
content: '登录信息已过期,请重新登录',
|
||||
});
|
||||
my.navigateTo({
|
||||
url: '/pages/denglu/denglu',
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('请求失败: ', JSON.stringify(error));
|
||||
my.alert({ content: '请求失败,请稍后重试' });
|
||||
},
|
||||
});
|
||||
}
|
||||
else{
|
||||
my.alert({
|
||||
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;
|
||||
this.setData({
|
||||
id: userInfo.id, // 获取 id
|
||||
|
||||
});
|
||||
// 发送请求移除商品
|
||||
if (userInfo && userInfo.cookie) {
|
||||
my.request({
|
||||
url: url + '/api/cart/delete',
|
||||
method: 'POST',
|
||||
data: {
|
||||
id: cartId,
|
||||
userId: userInfo.id
|
||||
}, // 使用 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);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
// 移除后更新
|
||||
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: '请求失败,请稍后重试' });
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
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)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
});
|
Reference in New Issue
Block a user