WxApp/page/component/details/details.js

129 lines
3.6 KiB
JavaScript
Raw Permalink 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 = {
2024-09-04 23:56:21 +08:00
id: options.goodsId,
2024-08-31 17:33:35 +08:00
goodName: options.goodName,
goodPrice: options.goodPrice,
goodDetail: options.goodDetail,
2024-09-05 15:43:51 +08:00
goodImage: decodeURIComponent(options.goodImage).split(","),
}
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" }
]
}
];
2024-09-05 15:43:51 +08:00
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() {
2024-09-05 15:43:51 +08:00
this.setData({ showPopup: true })
2024-08-31 22:40:03 +08:00
},
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
handlePopupHide() {
2024-09-05 15:43:51 +08:00
this.setData({ showPopup: false })
2024-08-31 22:40:03 +08:00
},
2024-08-29 14:36:16 +08:00
2024-08-31 22:40:03 +08:00
selectSpec(e) {
2024-09-05 15:43:51 +08:00
const { specid, id, val } = e.currentTarget.dataset
2024-09-01 11:15:29 +08:00
// 动态更改价格
2024-09-05 15:43:51 +08:00
let updatedPrice = this.data.goods.goodPrice
2024-09-01 11:15:29 +08:00
if (val === "普通蓬松枕") {
2024-09-05 15:43:51 +08:00
updatedPrice = 220 // 设置为220
2024-09-01 11:15:29 +08:00
}
if (val === "压缩高级枕") {
2024-09-05 15:43:51 +08:00
updatedPrice = 240 // 设置为240
2024-09-01 11:15:29 +08:00
}
2024-09-05 15:43:51 +08:00
let specList = this.data.specList.map((spec) => {
2024-08-31 22:40:03 +08:00
if (spec.specId === specid) {
2024-09-05 15:43:51 +08:00
spec.specValueList = spec.specValueList.map((item) => {
item.isSelected = item.specValueId == id
return item
})
2024-08-29 14:36:16 +08:00
}
2024-09-05 15:43:51 +08:00
return spec
})
2024-09-01 11:15:29 +08:00
this.setData({
specList,
viewGood: e.currentTarget.dataset.goodimage,
2024-09-05 15:43:51 +08:00
"goods.goodPrice": updatedPrice, // 更新价格
})
this.updateSelectedSpecs()
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
goToHome() {
wx.switchTab({
2024-09-05 15:43:51 +08:00
url: "/page/component/index", // 使用switchTab跳转到首页tabbar页面
})
2024-08-29 14:36:16 +08:00
},
2024-08-31 22:40:03 +08:00
updateSelectedSpecs() {
2024-09-05 15:43:51 +08:00
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 })
2024-08-31 22:40:03 +08:00
},
handleBuyNumChange(e) {
2024-08-29 14:36:16 +08:00
this.setData({
2024-09-05 15:43:51 +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)
2024-09-05 15:43:51 +08:00
// if (this.data.selectedSpecs.length < this.data.specList.length) {
// wx.showToast({
// title: "请选择所有规格",
// icon: "none",
// })
// return
// }
2024-08-29 14:36:16 +08:00
2024-09-05 15:43:51 +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-09-05 15:43:51 +08:00
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(",")}`
2024-08-29 14:36:16 +08:00
2024-09-05 15:43:51 +08:00
wx.navigateTo({ url })
2024-08-31 22:40:03 +08:00
// },
// fail: (err) => {
// wx.showToast({
// title: '获取用户信息失败',
// icon: 'none'
// });
// }
// });
2024-09-05 15:43:51 +08:00
},
})