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.

93 lines
2.4 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.

const { getGoodsDetails, addGoodsCart } = require("../../api/index.js");
Page({
/**
* 页面的初始数据
*/
data: {
goodsDetails: {} // 存储商品详情数据
},
/**
* 生命周期函数--监听页面加载
* @param {Object} options - 页面加载时传递的参数
*/
onLoad(options) {
// 显示加载提示
wx.showLoading({
title: '等待数据加载...',
});
// 获取商品详情数据
getGoodsDetails({ id: options.id }).then(res => {
wx.hideLoading(); // 隐藏加载提示
if (res.data.status === 200) {
// 设置商品详情数据
this.setData({
goodsDetails: res.data.data[0]
});
} else {
// 显示数据获取失败的提示
wx.showToast({
title: '数据获取失败',
icon: "none" // 修改为 "none",因为 "success" 不适合表示错误
});
}
});
},
/**
* 客服点击事件
*/
onClickKF() {
// 客服功能实现
},
/**
* 购物车点击事件
*/
onClickCart() {
// 跳转到购物车页面
wx.switchTab({
url: '/pages/cart/cart',
});
},
/**
* 加入购物车点击事件
*/
onClickAddCart() {
// 添加商品到购物车
addGoodsCart({
title: this.data.goodsDetails.title, // 商品标题
price: this.data.goodsDetails.price, // 商品价格
image: this.data.goodsDetails.topimage, // 商品图片
currentID: this.data.goodsDetails.id // 商品ID
}).then(res => {
if (res.data.status === 200) {
// 显示添加成功提示
wx.showToast({
title: res.data.msg,
});
} else {
// 显示添加失败提示
wx.showToast({
title: res.data.msg,
});
}
});
},
/**
* 立即购买点击事件
* @param {Object} e - 事件对象
*/
onClickBuy(e) {
// 跳转到购买页面并传递商品ID
wx.navigateTo({
url: '/pages/buy/buy?id=' + e.currentTarget.dataset.id,
});
}
});