WxApp/page/component/details/details.js

125 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Page({
data: {
goods: {},
specList: [],
selectedSpecs: [], // 用户选择的规格
buyNum: 1, // 购买数量
showPopup: false, // 控制弹出层显示
userInfo: {}, // 用户信息
viewGood: "",
},
onLoad(options) {
const goods = {
id: options.goodsId,
goodName: options.goodName,
goodPrice: options.goodPrice,
goodDetail: options.goodDetail,
goodImage: decodeURIComponent(options.goodImage).split(','),
};
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] });
},
openPopup() {
this.setData({ showPopup: true });
},
handlePopupHide() {
this.setData({ showPopup: false });
},
selectSpec(e) {
const { specid, id, val } = e.currentTarget.dataset;
// 动态更改价格
let updatedPrice = this.data.goods.goodPrice;
if (val === "普通蓬松枕") {
updatedPrice = 220; // 设置为220
}
if (val === "压缩高级枕") {
updatedPrice = 240; // 设置为240
}
let specList = this.data.specList.map(spec => {
if (spec.specId === specid) {
spec.specValueList = spec.specValueList.map(item => {
item.isSelected = item.specValueId == id;
return item;
});
}
return spec;
});
this.setData({
specList,
viewGood: e.currentTarget.dataset.goodimage,
'goods.goodPrice': updatedPrice // 更新价格
});
this.updateSelectedSpecs();
},
goToHome() {
wx.switchTab({
url: '/page/component/index' // 使用switchTab跳转到首页tabbar页面
});
},
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) {
this.setData({
buyNum: e.detail.value
});
},
confirmSpecs() {
console.log(this.data.selectedSpecs)
if (this.data.selectedSpecs.length < this.data.specList.length) {
wx.showToast({
title: '请选择所有规格',
icon: 'none'
});
return;
}
this.setData({ showPopup: false });
// wx.getUserProfile({
// desc: '用于完善会员资料',
// success: (res) => {
// this.setData({ userInfo: res.userInfo });
const { id, goodName, goodImage, goodPrice } = this.data.goods;
const num = this.data.buyNum;
const url = `../orders/orders?goodsId=${id}&title=${goodName}&image=${goodImage[0]}&price=${goodPrice}&num=${num}&specs=${this.data.selectedSpecs.join(',')}`;
wx.navigateTo({ url });
// },
// fail: (err) => {
// wx.showToast({
// title: '获取用户信息失败',
// icon: 'none'
// });
// }
// });
}
});