Page({ data: { productName: '', category: '', specification: '', price: 0, quantity: 1, totalPrice: 0, loading: false, error: '' }, onLoad(options) { console.log('接收到的参数:', options); // 即使参数不完整,也要尝试获取并设置 const productName = options.productName ? decodeURIComponent(options.productName) : ''; const category = options.category ? decodeURIComponent(options.category) : ''; const specification = options.specification ? decodeURIComponent(options.specification) : ''; let price = 0; if (options.price) { const decodedPrice = decodeURIComponent(options.price); console.log('解码后的价格:', decodedPrice); price = parseFloat(decodedPrice) || 0; } console.log('解析后的参数:', { productName, category, specification, price }); this.setData({ productName: productName, category: category, specification: specification, price: price, totalPrice: 0 // 初始时总价为0,不显示 }); }, // 件数输入变化 onQuantityChange(e) { const quantity = parseInt(e.detail.value) || 1; this.setData({ quantity: quantity // 只更新件数,不更新总价,等待点击计算按钮 }); }, // 减少数量 decreaseQuantity() { if (this.data.quantity > 1) { this.setData({ quantity: this.data.quantity - 1 }); } }, // 增加数量 increaseQuantity() { this.setData({ quantity: this.data.quantity + 1 }); }, // 计算价格 calculatePrice() { const totalPrice = Math.round(this.data.price * this.data.quantity * 10) / 10; this.setData({ totalPrice: totalPrice }); wx.showToast({ title: '计算完成', icon: 'success', duration: 1000 }); }, // 返回上一页 goBack() { wx.navigateBack(); }, // 下拉刷新 onPullDownRefresh() { console.log('开始下拉刷新'); // 重新加载页面数据 // 由于spec-detail页面的数据是通过URL参数传递的,这里可以重新获取参数并设置数据 const options = this.options || {}; const productName = options.productName ? decodeURIComponent(options.productName) : ''; const category = options.category ? decodeURIComponent(options.category) : ''; const specification = options.specification ? decodeURIComponent(options.specification) : ''; let price = 0; if (options.price) { const decodedPrice = decodeURIComponent(options.price); price = parseFloat(decodedPrice) || 0; } this.setData({ productName: productName, category: category, specification: specification, price: price, totalPrice: 0 // 初始时总价为0,不显示 }); // 结束下拉刷新 wx.stopPullDownRefresh(); } });