You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 引入获取购物车和删除购物车商品的API方法
const { getCart, delGoodsCart, submitOrder } = require("../../api/index.js");
// 定义小程序页面对象
Page({
// 页面数据对象初始时cartData为空数组totalPrice为0
data: {
cartData: [],
totalPrice: 0 // 初始化 totalPrice 为 0
},
// 页面显示时触发的方法调用http方法获取购物车数据
onShow() {
this.http();
},
// 跳转到商品详情页的方法根据传入的商品ID进行跳转
goDetail(e) {
const id = e.currentTarget.dataset.id; // 获取当前点击元素的数据集中的id值
wx.navigateTo({
url: `/pages/goodsDetails/goodsDetails?id=${id}` // 跳转到商品详情页并传递商品ID参数
});
},
// 删除购物车商品的方法根据传入的商品ID进行删除操作
delCartHandle(e) {
console.log("删除按钮被点击"); // 打印日志,表示删除按钮被点击
console.log(e.currentTarget.dataset.id); // 打印当前点击元素的数据集中的id值
// 调用删除购物车商品的API方法传入当前商品ID
delGoodsCart({ currentID: e.currentTarget.dataset.id }).then(res => {
if (res.data.status === 200) { // 如果API返回的状态码为200表示删除成功
wx.showToast({ // 显示成功提示框
title: '删除成功',
});
this.http(); // 刷新购物车数据
} else { // 如果API返回的状态码不是200表示删除失败
wx.showToast({ // 显示失败提示框
title: '删除失败',
});
}
});
},
// 获取购物车数据的方法
http() {
getCart().then(res => { // 调用获取购物车数据的API方法
console.log(res.data.data); // 打印API返回的购物车数据
const cartData = res.data.data;
let totalPrice = 0;
cartData.forEach(item => {
const price = parseFloat(item.price); // 将 price 字段从字符串转换为数字
if (!isNaN(price)) { // 确保 price 是有效的数字
totalPrice += price; // 计算总价格,每个商品默认数量为 1
} else {
console.warn(`Invalid price for item:`, item); // 调试信息:警告无效的 price
}
});
console.log("Calculated Total Price:", totalPrice); // 调试信息:打印计算出的总价格
this.setData({ // 更新页面数据对象中的cartData属性和totalPrice
cartData: cartData,
totalPrice: totalPrice * 100 // van-submit-bar 的 price 单位是分
});
}).catch(err => {
console.error("Error during getCart operation:", err); // 调试信息:捕获并打印错误
wx.showToast({ // 显示失败提示框
title: '获取购物车数据失败',
icon: 'none'
});
});
},
onSubmit(){
wx.showToast({
// 显示一个提示框,告知用户购买完成
title: '购买完成',// 提示框的标题
icon:"success"// 提示框的图标,这里使用成功图标
})
},
//无法实现
// 提交订单的方法
/*
onSubmit() {
// 调用提交订单的API方法
submitOrder().then(res => {
if (res.data.status === 200) { // 如果API返回的状态码为200表示订单提交成功
wx.showToast({ // 显示成功提示框
title: '购买成功',
});
// 清空购物车数据
this.setData({
cartData: [],
totalPrice: 0
});
// 调用删除购物车商品的API方法传入所有商品ID
const cartIDs = this.data.cartData.map(item => item.id);
delGoodsCart({ currentIDs: cartIDs }).then(delRes => {
if (delRes.data.status === 200) { // 如果API返回的状态码为200表示删除成功
console.log('购物车商品删除成功');
} else { // 如果API返回的状态码不是200表示删除失败
console.error('购物车商品删除失败', delRes);
}
}).catch(delErr => {
console.error('删除购物车商品时发生错误', delErr);
});
} else { // 如果API返回的状态码不是200表示订单提交失败
wx.showToast({ // 显示失败提示框
title: '购买失败',
icon: 'none'
});
}
}).catch(err => {
console.error("Error during submitOrder operation:", err); // 调试信息:捕获并打印错误
wx.showToast({ // 显示失败提示框
title: '提交订单失败',
icon: 'none'
});
});
}
*/
});