WxApp/page/component/details/details.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-08-29 14:36:16 +08:00
Page({
2024-08-31 22:40:03 +08:00
data: {
goods: {},
specList: [],
selectedSpecs: [], // 用户选择的规格
buyNum: 1, // 购买数量
showPopup: false, // 控制弹出层显示
userInfo: {}, // 用户信息
viewGood: "",
},
onLoad(options) {
2024-08-29 14:36:16 +08:00
const goods = {
id: options.id,
2024-08-31 17:33:35 +08:00
goodName: options.goodName,
goodPrice: options.goodPrice,
goodDetail: options.goodDetail,
goodImage: decodeURIComponent(options.goodImage).split(','),
2024-08-29 14:36:16 +08:00
};
2024-08-31 22:40:03 +08:00
const specList = [
{
specId: 2,
title: "枕被套 、床单颜色",
specValueList: [
{ specValueId: 3, specValue: "贝诺灰", isSelected: false, goodImage: "/image/beizi2.jpg" },
{ specValueId: 4, specValue: "开心日记", isSelected: false, goodImage: "/image/beizi3.jpg" },
{ specValueId: 5, specValue: "草莓熊", isSelected: false, goodImage: "/image/beizi4.jpg" },
{ specValueId: 6, specValue: "乖乖狗", isSelected: false, goodImage: "/image/beizi5.jpg" },
{ specValueId: 7, specValue: "蓝心小熊", isSelected: false, goodImage: "/image/beizi6.jpg" },
{ specValueId: 8, specValue: "初夏-紫", isSelected: false, goodImage: "/image/beizi7.jpg" }
]
}
];
this.setData({ goods, specList, viewGood: goods.goodImage[0] });
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
openPopup() {
this.setData({ showPopup: true });
},
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
handlePopupHide() {
this.setData({ showPopup: false });
},
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
selectSpec(e) {
2024-09-01 11:15:29 +08:00
const { specid, id, val } = e.currentTarget.dataset;
// 动态更改价格
let updatedPrice = this.data.goods.goodPrice;
if (val === "普通蓬松枕") {
updatedPrice = 220; // 设置为220
}
if (val === "压缩高级枕") {
updatedPrice = 240; // 设置为240
}
2024-08-31 22:40:03 +08:00
let specList = this.data.specList.map(spec => {
if (spec.specId === specid) {
spec.specValueList = spec.specValueList.map(item => {
item.isSelected = item.specValueId == id;
return item;
2024-08-29 14:36:16 +08:00
});
}
2024-08-31 22:40:03 +08:00
return spec;
2024-08-29 14:36:16 +08:00
});
2024-09-01 11:15:29 +08:00
this.setData({
specList,
viewGood: e.currentTarget.dataset.goodimage,
'goods.goodPrice': updatedPrice // 更新价格
});
2024-08-31 22:40:03 +08:00
this.updateSelectedSpecs();
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
goToHome() {
wx.switchTab({
url: '/page/component/index' // 使用switchTab跳转到首页tabbar页面
});
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
updateSelectedSpecs() {
const selectedSpecs = this.data.specList.map(spec => {
const selectedItem = spec.specValueList.find(item => item.isSelected);
return selectedItem ? selectedItem.specValue : null;
}).filter(item => item);
this.setData({ selectedSpecs });
},
handleBuyNumChange(e) {
2024-08-29 14:36:16 +08:00
this.setData({
2024-08-31 22:40:03 +08:00
buyNum: e.detail.value
});
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
confirmSpecs() {
console.log(this.data.selectedSpecs)
if (this.data.selectedSpecs.length < this.data.specList.length) {
wx.showToast({
title: '请选择所有规格',
icon: 'none'
});
return;
}
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
this.setData({ showPopup: false });
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
// wx.getUserProfile({
// desc: '用于完善会员资料',
// success: (res) => {
// this.setData({ userInfo: res.userInfo });
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
const { id, goodName, goodImage, goodPrice } = this.data.goods;
const num = this.data.buyNum;
const url = `../orders/orders?id=${id}&title=${goodName}&image=${goodImage[0]}&price=${goodPrice}&num=${num}&specs=${this.data.selectedSpecs.join(',')}`;
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
wx.navigateTo({ url });
// },
// fail: (err) => {
// wx.showToast({
// title: '获取用户信息失败',
// icon: 'none'
// });
// }
// });
}
});