Browse Source

修复商品详情页面404错误:优先使用完整的productId字符串

pull/12/head
徐飞洋 2 months ago
parent
commit
879dcd4e16
  1. 18
      pages/goods-detail/goods-detail.js
  2. 8
      pages/goods-update/goods-update.js
  3. 6
      pages/index/index.js
  4. 2
      utils/api.js

18
pages/goods-detail/goods-detail.js

@ -412,7 +412,7 @@ Page({
const regionParam = encodeURIComponent(region);
// 如果有联系人或地区信息,则添加到分享路径
let sharePath = `/pages/goods-detail/goods-detail?productId=${goodsDetail.id || goodsDetail.productId}`;
let sharePath = `/pages/goods-detail/goods-detail?productId=${goodsDetail.productId || goodsDetail.id}`;
if (contactName && contactPhone && region) {
sharePath += `&contactName=${contactNameParam}&contactPhone=${contactPhoneParam}&region=${regionParam}`;
} else if (contactName && contactPhone) {
@ -451,7 +451,7 @@ Page({
const contactPhoneParam = encodeURIComponent(contactPhone);
const regionParam = encodeURIComponent(region);
let queryParams = [`productId=${goodsDetail.id || goodsDetail.productId}`];
let queryParams = [`productId=${goodsDetail.productId || goodsDetail.id}`];
// 添加联系人信息到查询参数
if (contactName) {
@ -514,8 +514,9 @@ Page({
return;
}
// 获取商品ID
const productId = item.id || item.productId;
// 获取商品ID - 优先使用完整的productId字符串,而不是数字ID
// 从日志分析:商品有两个ID字段:id: 1898(数字)和productId: "product_1768010244400_186"(完整字符串)
const productId = item.productId || item.id;
if (!productId) {
console.error('商品ID不存在:', item);
wx.showToast({
@ -629,10 +630,11 @@ Page({
console.log('从分享URL中获取地区信息:', contactFromShare);
}
// 从商品数据中提取商品ID
// 从商品数据中提取商品ID - 优先使用完整的productId字符串,而不是数字ID
let productId;
if (goodsData && (goodsData.id || goodsData.productId)) {
productId = goodsData.id || goodsData.productId;
if (goodsData) {
// 优先使用productId字符串
productId = goodsData.productId || goodsData.id;
} else if (options.productId) {
productId = options.productId;
} else if (options.id) {
@ -1129,7 +1131,7 @@ Page({
// 添加收藏
addFavorite: function () {
const productId = String(this.data.goodsDetail.id || this.data.goodsDetail.productId);
const productId = String(this.data.goodsDetail.productId || this.data.goodsDetail.id);
console.log('用户点击了收藏按钮,商品ID:', productId);
// 检查用户登录状态

8
pages/goods-update/goods-update.js

@ -315,7 +315,7 @@ Page({
return {
title: title,
path: `/pages/goods-update/goods-update?productId=${goodsDetail.id || goodsDetail.productId}`,
path: `/pages/goods-update/goods-update?productId=${goodsDetail.productId || goodsDetail.id}`,
imageUrl: goodsDetail.imageUrls && goodsDetail.imageUrls.length > 0 ? goodsDetail.imageUrls[0] : '/images/你有好蛋.png'
}
},
@ -1018,7 +1018,7 @@ Page({
// 创建editSupply对象
const editSupply = {
id: goodsDetail.id || goodsDetail.productId,
id: goodsDetail.productId || goodsDetail.id,
imageUrls: goodsDetail.imageUrls || [],
name: goodsDetail.name || '',
spec: spec,
@ -1207,7 +1207,7 @@ Page({
// 权限验证通过,继续执行上架流程
const goodsDetail = this.data.goodsDetail;
const productId = goodsDetail.id || goodsDetail.productId;
const productId = goodsDetail.productId || goodsDetail.id;
wx.showModal({
title: '确认上架',
@ -1426,7 +1426,7 @@ Page({
// 确认下架(实际执行下架确认的内部方法)
confirmUnpublish: function() {
const goodsDetail = this.data.goodsDetail;
const productId = goodsDetail.id || goodsDetail.productId;
const productId = goodsDetail.productId || goodsDetail.id;
wx.showModal({
title: '确认下架',

6
pages/index/index.js

@ -355,7 +355,7 @@ Page({
// 更新商品列表中对应商品的收藏状态
const updatedGoods = this.data.goods.map(item => {
if (String(item.id || item.productId) === data.productId) {
if (String(item.productId || item.id) === data.productId) {
return {
...item,
isFavorite: data.isFavorite
@ -1448,7 +1448,7 @@ Page({
const uniqueGoodsMap = new Map();
filtered.forEach(item => {
// 使用id作为唯一标识,如果id不存在则使用productId
const uniqueId = item.id || item.productId;
const uniqueId = item.productId || item.id;
if (uniqueId && !uniqueGoodsMap.has(uniqueId)) {
uniqueGoodsMap.set(uniqueId, item);
}
@ -1710,7 +1710,7 @@ Page({
const item = e.currentTarget.dataset.item
// 确保productId存在,优先使用id,其次使用productId
const productId = String(item.id || item.productId || '')
const productId = String(item.productId || item.id || '')
if (!productId) {
console.error('商品ID不存在,无法查看详情');

2
utils/api.js

@ -435,7 +435,7 @@ module.exports = {
console.log('构建product对象,原始goodsItem:', goodsItem);
const product = {
productId: this.sanitizeProductId(goodsItem.productId || goodsItem.id), // 安全处理商品ID,确保为有效的字符串格式
id: this.sanitizeProductId(goodsItem.id || goodsItem.productId), // 确保id字段也存在且格式正确
id: this.sanitizeProductId(goodsItem.productId || goodsItem.id), // 确保id字段也存在且格式正确 - 优先使用productId字符串
productName: goodsItem.productName || goodsItem.name || '未命名商品', // 品种
quantity: goodsItem.quantity || 1, // 使用传入的数量,如果没有则默认为1
price: goodsItem.price || '', // 确保价格有默认值,使用空字符串支持字符串类型

Loading…
Cancel
Save