|
|
|
@ -1,6 +1,36 @@ |
|
|
|
// pages/favorites/index.js
|
|
|
|
const API = require('../../utils/api.js'); |
|
|
|
|
|
|
|
// 提取地区中的省份信息
|
|
|
|
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({ |
|
|
|
|
|
|
|
/** |
|
|
|
@ -156,42 +186,105 @@ Page({ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
API.getFavorites(phoneNumber).then(res => { |
|
|
|
console.log('获取收藏列表成功:', res); |
|
|
|
// 检查API返回是否成功
|
|
|
|
if (res && res.code === 200 && res.data) { |
|
|
|
let favorites = res.data.favorites || []; |
|
|
|
|
|
|
|
// 转换supplyStatus字段值
|
|
|
|
favorites = favorites.map(item => { |
|
|
|
if (item.Product && item.Product.supplyStatus) { |
|
|
|
// 将supplyStatus由"平台货源"、"三方认证"、"三方未认证"修改为"预售"、"现货"
|
|
|
|
// 平台货源和三方认证转为现货,三方未认证转为预售
|
|
|
|
if (['平台货源', '三方认证'].includes(item.Product.supplyStatus)) { |
|
|
|
item.Product.supplyStatus = "现货"; |
|
|
|
} else if (item.Product.supplyStatus === '三方未认证') { |
|
|
|
item.Product.supplyStatus = "预售"; |
|
|
|
// 首先获取所有商品列表,确保包含联系人信息
|
|
|
|
API.getProductList('published', { |
|
|
|
page: 1, |
|
|
|
pageSize: 100 // 获取足够多的商品,确保包含所有收藏商品
|
|
|
|
}).then(productListRes => { |
|
|
|
console.log('获取商品列表成功:', productListRes); |
|
|
|
|
|
|
|
// 然后获取收藏列表
|
|
|
|
return API.getFavorites(phoneNumber).then(res => { |
|
|
|
console.log('获取收藏列表成功:', res); |
|
|
|
// 检查API返回是否成功
|
|
|
|
if (res && res.code === 200 && res.data) { |
|
|
|
let favorites = res.data.favorites || []; |
|
|
|
|
|
|
|
// 获取商品列表数据
|
|
|
|
const allProducts = productListRes && productListRes.products ? productListRes.products : []; |
|
|
|
|
|
|
|
// 转换supplyStatus字段值,并从商品列表中获取联系人信息
|
|
|
|
favorites = favorites.map(item => { |
|
|
|
if (item.Product && item.Product.supplyStatus) { |
|
|
|
// 将supplyStatus由"平台货源"、"三方认证"、"三方未认证"修改为"预售"、"现货"
|
|
|
|
// 平台货源和三方认证转为现货,三方未认证转为预售
|
|
|
|
if (['平台货源', '三方认证'].includes(item.Product.supplyStatus)) { |
|
|
|
item.Product.supplyStatus = "现货"; |
|
|
|
} else if (item.Product.supplyStatus === '三方未认证') { |
|
|
|
item.Product.supplyStatus = "预售"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return item; |
|
|
|
}); |
|
|
|
|
|
|
|
// 从商品列表中查找对应的商品,获取联系人信息
|
|
|
|
const productId = item.Product?.productId || item.productId; |
|
|
|
const matchingProduct = allProducts.find(product => |
|
|
|
String(product.id) === String(productId) || |
|
|
|
String(product.productId) === String(productId) |
|
|
|
); |
|
|
|
|
|
|
|
// 提取联系人信息
|
|
|
|
let product_contact = ''; |
|
|
|
let contact_phone = ''; |
|
|
|
let fullRegion = ''; |
|
|
|
let province = ''; |
|
|
|
|
|
|
|
// 优先从商品列表中获取联系人信息和地区信息
|
|
|
|
if (matchingProduct) { |
|
|
|
product_contact = matchingProduct.product_contact || matchingProduct.contact || ''; |
|
|
|
contact_phone = matchingProduct.contact_phone || matchingProduct.phone || ''; |
|
|
|
// 保存完整地区数据
|
|
|
|
fullRegion = matchingProduct.fullRegion || matchingProduct.region || ''; |
|
|
|
// 提取省份
|
|
|
|
province = extractProvince(fullRegion); |
|
|
|
} |
|
|
|
// 然后尝试从收藏数据中获取
|
|
|
|
else if (item.Product) { |
|
|
|
product_contact = item.Product.product_contact || item.Product.contact || ''; |
|
|
|
contact_phone = item.Product.contact_phone || item.Product.phone || ''; |
|
|
|
// 保存完整地区数据
|
|
|
|
fullRegion = item.Product.region || ''; |
|
|
|
// 提取省份
|
|
|
|
province = extractProvince(fullRegion); |
|
|
|
} |
|
|
|
|
|
|
|
// 更新item对象,确保联系人信息和地区信息同时存在于顶层和Product对象中
|
|
|
|
item.product_contact = product_contact; |
|
|
|
item.contact_phone = contact_phone; |
|
|
|
item.fullRegion = fullRegion; |
|
|
|
item.region = province; |
|
|
|
if (item.Product) { |
|
|
|
item.Product.product_contact = product_contact; |
|
|
|
item.Product.contact_phone = contact_phone; |
|
|
|
item.Product.fullRegion = fullRegion; |
|
|
|
item.Product.region = province; |
|
|
|
} |
|
|
|
|
|
|
|
return item; |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('更新后的收藏列表:', favorites); |
|
|
|
this.setData({ |
|
|
|
favoritesList: favorites, |
|
|
|
hasFavorites: favorites.length > 0, |
|
|
|
loading: false |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// API返回格式不正确
|
|
|
|
this.setData({ |
|
|
|
loading: false, |
|
|
|
hasFavorites: false |
|
|
|
}); |
|
|
|
wx.showToast({ |
|
|
|
title: '数据获取失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
favoritesList: favorites, |
|
|
|
hasFavorites: favorites.length > 0, |
|
|
|
loading: false |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// API返回格式不正确
|
|
|
|
this.setData({ |
|
|
|
loading: false, |
|
|
|
hasFavorites: false |
|
|
|
}); |
|
|
|
wx.showToast({ |
|
|
|
title: '数据获取失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
// 停止下拉刷新
|
|
|
|
if (isPullDown) { |
|
|
|
wx.stopPullDownRefresh(); |
|
|
|
} |
|
|
|
}); |
|
|
|
}).catch(err => { |
|
|
|
console.error('获取收藏列表失败:', err); |
|
|
|
wx.showToast({ |
|
|
|
@ -202,7 +295,6 @@ Page({ |
|
|
|
loading: false, |
|
|
|
hasFavorites: false |
|
|
|
}); |
|
|
|
}).finally(() => { |
|
|
|
// 停止下拉刷新
|
|
|
|
if (isPullDown) { |
|
|
|
wx.stopPullDownRefresh(); |
|
|
|
@ -253,14 +345,18 @@ Page({ |
|
|
|
/** |
|
|
|
* 跳转到商品详情页 |
|
|
|
*/ |
|
|
|
goToGoodsDetail: function (e) { |
|
|
|
// 检查用户是否登录
|
|
|
|
goToGoodsDetail: function(e) { |
|
|
|
console.log('goToGoodsDetail - e.currentTarget.dataset:', e.currentTarget.dataset); |
|
|
|
const goodsItem = e.currentTarget.dataset.item; |
|
|
|
console.log('goToGoodsDetail - goodsItem:', goodsItem); |
|
|
|
console.log('goToGoodsDetail - goodsItem.Product:', goodsItem.Product); |
|
|
|
|
|
|
|
// 检查用户登录状态
|
|
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
if (!openid || !userId) { |
|
|
|
console.log('用户未登录,需要授权登录'); |
|
|
|
// 显示授权登录弹窗
|
|
|
|
console.log('用户未登录,提示登录'); |
|
|
|
wx.showToast({ |
|
|
|
title: '请先登录', |
|
|
|
icon: 'none' |
|
|
|
@ -268,12 +364,116 @@ Page({ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
const goodsItem = e.currentTarget.dataset.item; |
|
|
|
// 获取嵌套在Product对象中的商品数据
|
|
|
|
const productData = goodsItem.Product; |
|
|
|
// 跳转到商品详情页面,并传递商品数据,使用encodeURIComponent编码JSON字符串
|
|
|
|
wx.navigateTo({ |
|
|
|
url: '/pages/goods-detail/goods-detail?goodsData=' + encodeURIComponent(JSON.stringify(productData)) |
|
|
|
// 获取商品ID
|
|
|
|
const productId = goodsItem.Product?.productId || goodsItem.productId; |
|
|
|
console.log('获取完整商品详情 - productId:', productId); |
|
|
|
|
|
|
|
// 调用API.getProductList获取完整的商品列表,确保包含联系人信息
|
|
|
|
wx.showLoading({ title: '加载中' }); |
|
|
|
API.getProductList('published', { |
|
|
|
page: 1, |
|
|
|
pageSize: 100 // 获取足够多的商品,确保包含当前商品
|
|
|
|
}).then(productListRes => { |
|
|
|
console.log('获取商品列表成功:', productListRes); |
|
|
|
|
|
|
|
// 获取商品列表数据
|
|
|
|
const allProducts = productListRes && productListRes.products ? productListRes.products : []; |
|
|
|
|
|
|
|
// 从商品列表中查找对应的商品,获取联系人信息
|
|
|
|
const matchingProduct = allProducts.find(product => |
|
|
|
String(product.id) === String(productId) || |
|
|
|
String(product.productId) === String(productId) |
|
|
|
); |
|
|
|
|
|
|
|
// 提取联系人信息
|
|
|
|
let product_contact = ''; |
|
|
|
let contact_phone = ''; |
|
|
|
let region = ''; |
|
|
|
|
|
|
|
// 优先从商品列表中获取联系人信息
|
|
|
|
if (matchingProduct) { |
|
|
|
product_contact = matchingProduct.product_contact || matchingProduct.contact || ''; |
|
|
|
contact_phone = matchingProduct.contact_phone || matchingProduct.phone || ''; |
|
|
|
region = matchingProduct.region || ''; |
|
|
|
console.log('从商品列表获取到联系人信息:', { product_contact, contact_phone, region }); |
|
|
|
} |
|
|
|
// 然后尝试从收藏数据中获取
|
|
|
|
else { |
|
|
|
product_contact = goodsItem.product_contact || goodsItem.Product?.product_contact || goodsItem.Product?.contact || goodsItem.contact || ''; |
|
|
|
contact_phone = goodsItem.contact_phone || goodsItem.Product?.contact_phone || goodsItem.Product?.phone || goodsItem.phone || ''; |
|
|
|
region = goodsItem.region || goodsItem.Product?.region || ''; |
|
|
|
console.log('从收藏数据获取到联系人信息:', { product_contact, contact_phone, region }); |
|
|
|
} |
|
|
|
|
|
|
|
// 确保商品ID正确设置
|
|
|
|
const productData = goodsItem.Product || {}; |
|
|
|
const completeGoodsData = { |
|
|
|
// 首先包含原始商品对象的所有字段
|
|
|
|
...goodsItem, |
|
|
|
// 然后用Product对象中的字段覆盖
|
|
|
|
...productData, |
|
|
|
// 确保商品ID正确设置
|
|
|
|
id: productData.productId || goodsItem.productId, |
|
|
|
productId: productData.productId || goodsItem.productId, |
|
|
|
// 强制设置从商品列表中获取到的联系人信息和地区信息
|
|
|
|
product_contact: product_contact, |
|
|
|
contact_phone: contact_phone, |
|
|
|
// 保存完整地区数据
|
|
|
|
fullRegion: region, |
|
|
|
// 只显示省份
|
|
|
|
region: extractProvince(region) |
|
|
|
}; |
|
|
|
|
|
|
|
// 专门检查并记录联系人信息
|
|
|
|
console.log('联系人信息检查:'); |
|
|
|
console.log('- 联系人姓名:', completeGoodsData.product_contact || '暂无'); |
|
|
|
console.log('- 联系电话:', completeGoodsData.contact_phone || '暂无'); |
|
|
|
|
|
|
|
// 添加更多日志,确保我们没有丢失重要信息
|
|
|
|
console.log('准备传递的完整商品数据:', completeGoodsData); |
|
|
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
// 跳转到商品详情页
|
|
|
|
wx.navigateTo({ |
|
|
|
url: '/pages/goods-detail/goods-detail?goodsData=' + encodeURIComponent(JSON.stringify(completeGoodsData)) |
|
|
|
}); |
|
|
|
|
|
|
|
// 后台调用API.updateProductContacts(),与buyer页面保持一致
|
|
|
|
console.log('开始调用API.updateProductContacts()'); |
|
|
|
API.updateProductContacts().then(function(res) { |
|
|
|
console.log('商品联系人更新成功:', res); |
|
|
|
}).catch(function(err) { |
|
|
|
console.error('商品联系人更新失败:', err); |
|
|
|
}); |
|
|
|
}).catch(err => { |
|
|
|
console.error('获取商品列表失败:', err); |
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
// 即使获取商品列表失败,也尝试跳转到详情页
|
|
|
|
// 保存当前已有的联系人信息
|
|
|
|
const existingContactInfo = { |
|
|
|
product_contact: goodsItem.product_contact || goodsItem.Product?.product_contact || goodsItem.Product?.contact || goodsItem.contact || '', |
|
|
|
contact_phone: goodsItem.contact_phone || goodsItem.Product?.contact_phone || goodsItem.Product?.phone || goodsItem.phone || '', |
|
|
|
region: goodsItem.region || goodsItem.Product?.region || '' |
|
|
|
}; |
|
|
|
|
|
|
|
// 确保商品ID正确设置
|
|
|
|
const productData = goodsItem.Product || {}; |
|
|
|
const completeGoodsData = { |
|
|
|
...goodsItem, |
|
|
|
...productData, |
|
|
|
id: productData.productId || goodsItem.productId, |
|
|
|
productId: productData.productId || goodsItem.productId, |
|
|
|
product_contact: existingContactInfo.product_contact, |
|
|
|
contact_phone: existingContactInfo.contact_phone, |
|
|
|
region: existingContactInfo.region |
|
|
|
}; |
|
|
|
|
|
|
|
// 跳转到商品详情页
|
|
|
|
wx.navigateTo({ |
|
|
|
url: '/pages/goods-detail/goods-detail?goodsData=' + encodeURIComponent(JSON.stringify(completeGoodsData)) |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
|