Browse Source

修复收藏页面点击货源详情关注人数显示不正确的问题

pull/1/head
Default User 2 months ago
parent
commit
d731489a4c
  1. 2
      custom-tab-bar/index.wxml
  2. 17
      pages/customer-service/index.wxss
  3. 16
      pages/goods-detail/goods-detail.js
  4. 28
      server-example/server-mysql.js

2
custom-tab-bar/index.wxml

@ -18,7 +18,7 @@
<view class="tab-bar-icon">
<view class="tab-bar-badge" wx:if="{{badges['buyer']}}">{{badges['buyer']}}</view>
</view>
<view class="tab-bar-text">买蛋</view>
<view class="tab-bar-text">商城</view>
</view>
</view>

17
pages/customer-service/index.wxss

@ -1,8 +1,11 @@
/* pages/customer-service/index.wxss */
.container {
padding-bottom: 100rpx;
padding: 0;
margin: 0;
background-color: #f8f8f8;
min-height: 100vh;
width: 100%;
box-sizing: border-box;
}
/* 顶部导航栏 */
@ -124,17 +127,21 @@
/* 经纪人列表 */
.broker-list {
background-color: #f8f8f8;
padding: 0 30rpx;
margin-top: 280rpx; /* 为固定导航和搜索区域留出空间 */
padding: 0;
margin: 280rpx 0 0 0;
width: 100%;
box-sizing: border-box;
}
.broker-item {
background-color: #fff;
border-radius: 24rpx;
margin: 20rpx 0;
padding: 28rpx;
padding: 28rpx 30rpx;
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
transition: all 0.2s ease;
width: 100%;
box-sizing: border-box;
}
.broker-item:active {
@ -370,6 +377,8 @@
background-color: #fff;
margin: 20rpx 0;
border-radius: 24rpx;
width: 100%;
box-sizing: border-box;
}
.empty-state text:first-child {

16
pages/goods-detail/goods-detail.js

@ -183,14 +183,9 @@ Page({
// 确保商品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));
// 关键修改:直接使用API返回的reservedCount值,这个值已经是从favorites表中统计的收藏数量
// 不再使用selected或reservationCount字段计算,确保收藏人数显示正确
const finalReservationCount = product.reservedCount || 0;
// 处理grossWeight为null或无效的情况,返回空字符串以支持文字输入
const grossWeightValue = product.grossWeight !== null && product.grossWeight !== undefined ? product.grossWeight : '';
@ -254,7 +249,6 @@ Page({
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,
@ -264,7 +258,9 @@ Page({
// 复制原始产品对象中的所有字段,确保不丢失任何数据
...product,
// 合并预加载数据中的字段,优先使用预加载数据中的联系人信息
...(preloadedData || {})
...(preloadedData || {}),
// 确保reservedCount字段使用我们计算得到的值,放在最后以覆盖其他来源的值
reservedCount: finalReservationCount
};
console.log('最终格式化后的数据:', {

28
server-example/server-mysql.js

@ -1853,7 +1853,7 @@ app.post('/api/product/list', async (req, res) => {
}
// 处理商品列表中的grossWeight字段,确保是数字类型,同时反序列化imageUrls
const processedProducts = products.map(product => {
const processedProducts = await Promise.all(products.map(async product => {
const productJSON = product.toJSON();
// 确保created_at字段存在并转换为ISO字符串格式
@ -1880,8 +1880,15 @@ app.post('/api/product/list', async (req, res) => {
// 确保grossWeight值是字符串类型
productJSON.grossWeight = String(grossWeightDetails.value);
// 确保reservedCount是数字类型,如果不存在则默认为0
productJSON.reservedCount = typeof productJSON.reservedCount === 'number' ? productJSON.reservedCount : 0;
// 查询该商品的收藏人数 - 从favorites表中统计
const favoriteCount = await Favorite.count({
where: {
productId: productJSON.productId
}
});
// 使用查询到的收藏人数更新reservedCount字段
productJSON.reservedCount = favoriteCount;
// 重要修复:反序列化imageUrls字段,确保前端收到的是数组
if (productJSON.imageUrls && typeof productJSON.imageUrls === 'string') {
@ -1934,7 +1941,7 @@ app.post('/api/product/list', async (req, res) => {
}
return productJSON;
});
}));
// 准备响应数据 - 修改格式以匹配前端期望
const responseData = {
@ -3687,6 +3694,13 @@ app.post('/api/products/detail', async (req, res) => {
});
}
// 查询收藏人数 - 从favorites表中统计该商品的收藏数量
const favoriteCount = await Favorite.count({
where: {
productId: productId
}
});
// 对返回的商品数据进行处理
let updatedProduct = { ...product.toJSON() };
@ -3730,11 +3744,11 @@ app.post('/api/products/detail', async (req, res) => {
updatedProduct.grossWeight = String(grossWeightDetails.value);
}
// 确保reservedCount是数字类型,如果不存在则默认为0
updatedProduct.reservedCount = typeof updatedProduct.reservedCount === 'number' ? updatedProduct.reservedCount : 0;
// 设置收藏人数 - 从favorites表统计得到
updatedProduct.reservedCount = favoriteCount;
console.log('商品详情 - 最终返回的毛重值:', updatedProduct.grossWeight, '类型:', typeof updatedProduct.grossWeight);
console.log('商品详情 - 返回的预约人数:', updatedProduct.reservedCount, '类型:', typeof updatedProduct.reservedCount);
console.log('商品详情 - 返回的收藏人数:', updatedProduct.reservedCount, '类型:', typeof updatedProduct.reservedCount);
res.json({
success: true,

Loading…
Cancel
Save