You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

547 lines
18 KiB

// pages/goods-detail/goods-detail.js
const API = require('../../utils/api.js')
// 根据sourceType获取对应的颜色
function getSourceTypeColor(sourceType) {
const colorMap = {
'三方认证': '#4d9dff',
'三方未认证': '#ff4d4f',
'平台货源': '#2ad21f'
};
return colorMap[sourceType] || '#4d9dff';
}
// 格式化毛重显示的辅助函数
function formatGrossWeight(grossWeight, weight) {
console.log('===== formatGrossWeight 函数调用 =====');
console.log('输入参数:');
console.log('- grossWeight:', grossWeight, '(类型:', typeof grossWeight, ')');
console.log('- weight:', weight, '(类型:', typeof weight, ')');
// 1. 优先使用grossWeight,只要它不是null、不是undefined、不是空字符串
if (grossWeight !== null && grossWeight !== undefined && grossWeight !== '') {
console.log('使用grossWeight参数');
return grossWeight;
}
// 如果grossWeight无效,尝试使用weight字段
if (weight !== null && weight !== undefined && weight !== '') {
console.log('使用weight参数');
return weight;
}
// 3. 新增逻辑:如果grossWeight和weight都无效,返回空字符串以支持文字输入
console.log('两个参数都无效,返回空字符串');
return "";
}
// 提取地区中的省份信息
function extractProvince(region) {
if (!region || typeof region !== 'string') {
return region;
}
// 查找各种省份格式的位置
const provinceEndIndex = region.indexOf('省');
const autonomousRegionEndIndex = region.indexOf('自治区');
const municipalityEndIndex = region.indexOf('市'); // 用于直辖市,如北京市、上海市
const specialRegionEndIndex = region.indexOf('特别行政区'); // 用于香港、澳门
if (provinceEndIndex !== -1) {
// 包含"省"字,提取到"省"字结束
return region.substring(0, provinceEndIndex + 1);
} else if (autonomousRegionEndIndex !== -1) {
// 包含"自治区",提取到"自治区"结束
return region.substring(0, autonomousRegionEndIndex + 3);
} else if (specialRegionEndIndex !== -1) {
// 包含"特别行政区",提取到"特别行政区"结束
return region.substring(0, specialRegionEndIndex + 5);
} else if (municipalityEndIndex === 2) {
// 直辖市(如北京市、上海市),市字在第2个字符位置
return region.substring(0, municipalityEndIndex + 1);
}
// 如果没有找到匹配的格式,返回原字符串
return region;
}
Page({
data: {
goodsDetail: {}, // 当前商品详情
showImagePreview: false, // 控制图片预览弹窗显示
previewImageUrls: [], // 预览的图片URL列表
previewImageIndex: 0, // 当前预览图片的索引
fromSeller: false, // 是否来自seller页面
// 图片缩放相关状态
scale: 1, // 当前缩放比例
lastScale: 1, // 上一次缩放比例
startDistance: 0, // 双指起始距离
doubleTapTimer: null, // 双击计时器
lastTapTime: 0, // 上一次单击时间
isScaling: false, // 是否正在缩放中
offsetX: 0, // X轴偏移量
offsetY: 0, // Y轴偏移量
initialTouch: null, // 初始触摸点
},
onLoad: function (options) {
console.log('商品详情页面加载,参数:', options);
// 解析传入的商品数据
let goodsData = null;
if (options.goodsData) {
try {
goodsData = JSON.parse(decodeURIComponent(options.goodsData));
console.log('解析后的商品数据:', goodsData);
// 优先使用传入的商品数据中的联系人信息
this.setData({
goodsDetail: goodsData,
fromSeller: options.fromSeller === 'true'
});
} catch (error) {
console.error('解析商品数据失败:', error);
}
}
// 从商品数据中提取商品ID
let productId;
if (goodsData && (goodsData.id || goodsData.productId)) {
productId = goodsData.id || goodsData.productId;
} else if (options.productId) {
productId = options.productId;
} else {
console.error('未找到商品ID');
return;
}
// 加载商品详情(即使已有goodsData,也调用API获取最新数据)
this.loadGoodsDetail(productId, goodsData);
},
loadGoodsDetail: function (productId, preloadedData = null) {
// 首先显示预加载的数据,确保UI快速响应
if (preloadedData) {
console.log('使用预加载数据显示UI');
}
API.getProductDetail({ productId: productId })
.then(res => {
console.log('获取商品详情成功:', res);
if (res && res.code === 200 && res.data) {
// 从本地存储获取已预约商品ID列表
const reservedGoodsIds = wx.getStorageSync('reservedGoodsIds') || [];
const product = res.data;
// 详细检查联系人相关字段 - 特别关注数据库字段名
console.log('===== 数据库字段名详细检查 =====');
console.log('- 数据库字段 product_contact:', product.product_contact, '(类型:', typeof product.product_contact, ')');
console.log('- 数据库字段 contact_phone:', product.contact_phone, '(类型:', typeof product.contact_phone, ')');
console.log('- 其他可能的字段:');
console.log(' - contactPhone:', product.contactPhone);
console.log(' - phone:', product.phone);
console.log(' - contact:', product.contact);
// 检查完整的API响应字段,确保不错过任何重要信息
console.log('API响应完整字段列表:', Object.keys(product).sort());
// 只过滤hidden状态的商品
if (product.status === 'hidden') {
wx.showToast({
title: '商品已下架',
icon: 'none',
duration: 2000
});
// 2秒后返回上一页
setTimeout(() => {
wx.navigateBack();
}, 2000);
return;
}
// 确保商品ID的一致性
const productIdStr = String(product.productId || product.id);
// 增强的预约人数计算逻辑
const selectedValue = product.selected;
const reservedCountValue = product.reservedCount;
const reservationCountValue = product.reservationCount;
const finalReservationCount = selectedValue !== undefined && selectedValue !== null ? selectedValue :
(reservedCountValue !== undefined && reservedCountValue !== null ? reservedCountValue :
(reservationCountValue || 0));
// 处理grossWeight为null或无效的情况,返回空字符串以支持文字输入
const grossWeightValue = product.grossWeight !== null && product.grossWeight !== undefined ? product.grossWeight : '';
// 转换supplyStatus字段值
let supplyStatusValue = product.supplyStatus || '';
// 将"平台货源"、"三方认证"、"三方未认证"修改为"预售"、"现货"
if (supplyStatusValue === '平台货源' || supplyStatusValue === '三方认证') {
supplyStatusValue = '现货';
} else if (supplyStatusValue === '三方未认证') {
supplyStatusValue = '预售';
}
// 关键修改:优先使用预加载数据中的联系人信息,如果有的话
let contactPhone = '';
let contactName = '';
let region = '';
// 首先检查预加载数据
if (preloadedData) {
contactPhone = preloadedData.contact_phone || preloadedData.contactPhone || preloadedData.phone || '';
contactName = preloadedData.product_contact || preloadedData.contact || preloadedData.contactName || '';
region = preloadedData.region || '';
console.log('从预加载数据获取联系人信息:', { contactName, contactPhone, region });
}
// 如果预加载数据中没有,则使用API返回的数据
if (!contactPhone) {
contactPhone = product.contact_phone !== null && product.contact_phone !== undefined ? product.contact_phone :
(product.contactPhone || product.phone || '暂无联系电话');
}
if (!contactName) {
contactName = product.product_contact !== null && product.product_contact !== undefined ? product.product_contact :
(product.contact || product.contactName || '联系人信息暂不可用');
}
if (!region && product.region) {
region = extractProvince(product.region);
}
// 如果region仍为空,尝试从预加载数据中提取
if (!region && preloadedData && preloadedData.region) {
region = extractProvince(preloadedData.region);
}
if (!region) region = '地区未知';
// 转换商品数据格式
const formattedGoods = {
id: productIdStr,
productId: productIdStr,
// 直接使用数据库字段名
name: product.productName || product.name || '商品名称',
price: product.price,
minOrder: product.minOrder || product.quantity,
yolk: product.yolk,
spec: product.spec || product.specification || '暂无规格',
region: region,
// 直接使用数据库字段名,确保与表结构完全一致
product_contact: contactName,
contact_phone: contactPhone,
// 保留原始字段引用,确保数据完整性
imageUrls: product.imageUrls || product.images || [],
displayGrossWeight: formatGrossWeight(grossWeightValue, product.weight),
isReserved: reservedGoodsIds.some(itemId => String(itemId) === productIdStr),
reservedCount: finalReservationCount,
created_at: product.created_at || product.createdAt,
updated_at: product.updated_at || product.updatedAt,
status: product.status,
supplyStatus: supplyStatusValue,
sourceType: product.sourceType || '',
sourceTypeColor: getSourceTypeColor(product.sourceType),
// 复制原始产品对象中的所有字段,确保不丢失任何数据
...product,
// 合并预加载数据中的字段,优先使用预加载数据中的联系人信息
...(preloadedData || {})
};
console.log('最终格式化后的数据:', {
product_contact: formattedGoods.product_contact,
contact_phone: formattedGoods.contact_phone,
region: formattedGoods.region
});
this.setData({
goodsDetail: formattedGoods
});
} else {
wx.showToast({
title: '获取商品详情失败',
icon: 'none',
duration: 2000
});
}
})
.catch(err => {
console.error('获取商品详情失败:', err);
wx.showToast({
title: '获取商品详情失败',
icon: 'none',
duration: 2000
});
})
.finally(() => {
wx.hideLoading();
});
},
// 预览图片
previewImage(e) {
const { urls, index } = e.currentTarget.dataset;
if (!urls || urls.length === 0) {
wx.showToast({
title: '没有图片可预览',
icon: 'none'
});
return;
}
this.setData({
showImagePreview: true,
previewImageUrls: urls,
previewImageIndex: parseInt(index || 0)
});
this.resetZoom();
},
// 关闭图片预览
closeImagePreview() {
this.setData({
showImagePreview: false
});
this.resetZoom();
},
// 重置缩放状态
resetZoom() {
this.setData({
scale: 1,
lastScale: 1,
offsetX: 0,
offsetY: 0,
initialTouch: null
});
},
// 图片预览切换
onPreviewImageChange(e) {
this.setData({
previewImageIndex: e.detail.current
});
// 切换图片时重置缩放状态
this.resetZoom();
},
// 处理图片点击事件(单击/双击判断)
handleImageTap(e) {
const currentTime = Date.now();
const lastTapTime = this.data.lastTapTime || 0;
// 判断是否为双击(300ms内连续点击)
if (currentTime - lastTapTime < 300) {
// 双击事件
if (this.data.doubleTapTimer) {
clearTimeout(this.data.doubleTapTimer);
}
// 切换放大/缩小状态
const newScale = this.data.scale === 1 ? 2 : 1;
this.setData({
scale: newScale,
lastScale: newScale,
offsetX: 0,
offsetY: 0,
lastTapTime: 0 // 重置双击状态
});
} else {
// 单击事件,设置延迟来检测是否会成为双击
if (this.data.doubleTapTimer) {
clearTimeout(this.data.doubleTapTimer);
}
this.setData({
lastTapTime: currentTime,
doubleTapTimer: setTimeout(() => {
// 确认是单击,关闭图片预览
this.closeImagePreview();
}, 300)
});
}
},
// 计算两点之间的距离
calculateDistance(touch1, touch2) {
const dx = touch2.clientX - touch1.clientX;
const dy = touch2.clientY - touch1.clientY;
return Math.sqrt(dx * dx + dy * dy);
},
// 处理触摸开始事件
handleTouchStart(e) {
console.log('触摸开始事件:', e);
const touches = e.touches;
if (touches.length === 1) {
// 单指:准备拖动
this.setData({
initialTouch: {
x: touches[0].clientX,
y: touches[0].clientY
}
});
} else if (touches.length === 2) {
// 双指:记录起始距离,准备缩放
const distance = this.calculateDistance(touches[0], touches[1]);
this.setData({
startDistance: distance,
isScaling: true,
lastScale: this.data.scale
});
}
},
// 处理触摸移动事件
handleTouchMove(e) {
const touches = e.touches;
if (touches.length === 1 && this.data.initialTouch && this.data.scale !== 1) {
// 单指拖动(只有在缩放状态下才允许拖动)
const deltaX = touches[0].clientX - this.data.initialTouch.x;
const deltaY = touches[0].clientY - this.data.initialTouch.y;
// 计算新的偏移量
let newOffsetX = this.data.offsetX + deltaX;
let newOffsetY = this.data.offsetY + deltaY;
// 边界限制
const windowWidth = wx.getSystemInfoSync().windowWidth;
const windowHeight = wx.getSystemInfoSync().windowHeight;
const maxOffsetX = (windowWidth * (this.data.scale - 1)) / 2;
const maxOffsetY = (windowHeight * (this.data.scale - 1)) / 2;
newOffsetX = Math.max(-maxOffsetX, Math.min(maxOffsetX, newOffsetX));
newOffsetY = Math.max(-maxOffsetY, Math.min(maxOffsetY, newOffsetY));
this.setData({
offsetX: newOffsetX,
offsetY: newOffsetY,
initialTouch: {
x: touches[0].clientX,
y: touches[0].clientY
}
});
} else if (touches.length === 2) {
// 双指缩放
const currentDistance = this.calculateDistance(touches[0], touches[1]);
const scale = (currentDistance / this.data.startDistance) * this.data.lastScale;
// 限制缩放范围在0.5倍到3倍之间
const newScale = Math.max(0.5, Math.min(3, scale));
this.setData({
scale: newScale,
isScaling: true
});
}
},
// 处理触摸结束事件
handleTouchEnd(e) {
this.setData({
isScaling: false,
lastScale: this.data.scale,
initialTouch: null
});
},
// 拨打电话
makePhoneCall(e) {
console.log('拨打电话事件:', e);
const phoneNumber = e.currentTarget.dataset.phone;
if (phoneNumber) {
wx.showModal({
title: '联系人电话',
content: phoneNumber,
showCancel: true,
cancelText: '取消',
confirmText: '拨打',
success: (res) => {
if (res.confirm) {
wx.makePhoneCall({
phoneNumber: phoneNumber,
success: () => {
console.log('拨打电话成功');
},
fail: (err) => {
console.error('拨打电话失败', err);
wx.showToast({
title: '拨打电话失败',
icon: 'none'
});
}
});
}
}
});
}
},
// 我想要(预约)
onClickWantInDetail(e) {
console.log('我想要事件:', e);
const { id } = e.currentTarget.dataset;
if (!id) return;
// 从本地存储获取openid
const openid = wx.getStorageSync('openid');
console.log('openid:', openid);
// 检查是否已登录
if (!openid) {
// 如果未登录,显示授权登录弹窗
this.setData({ showAuthModal: true });
return;
}
// 获取已预约商品ID列表
let reservedGoodsIds = wx.getStorageSync('reservedGoodsIds') || [];
// 检查是否已经预约过
if (reservedGoodsIds.some(itemId => String(itemId) === String(id))) {
wx.showToast({
title: '您已经预约过该商品',
icon: 'none',
duration: 1500
});
return;
}
// 添加到已预约列表
reservedGoodsIds.push(id);
wx.setStorageSync('reservedGoodsIds', reservedGoodsIds);
// 更新页面状态
this.setData({
'goodsDetail.isReserved': true
});
// 调用API记录预约
API.reserveProduct({ id: id })
.then(res => {
console.log('预约成功:', res);
wx.showToast({
title: '预约成功',
icon: 'success',
duration: 1500
});
})
.catch(err => {
console.error('预约失败:', err);
// 如果API调用失败,从本地列表中移除
reservedGoodsIds = reservedGoodsIds.filter(itemId => String(itemId) !== String(id));
wx.setStorageSync('reservedGoodsIds', reservedGoodsIds);
// 更新页面状态
this.setData({
'goodsDetail.isReserved': false
});
wx.showToast({
title: '预约失败,请重试',
icon: 'none',
duration: 1500
});
});
},
// 返回上一页
goBack() {
wx.navigateBack();
}
});