|
|
|
@ -83,19 +83,27 @@ Page({ |
|
|
|
loading: true |
|
|
|
}); |
|
|
|
|
|
|
|
// 获取用户电话号码
|
|
|
|
// 获取用户信息
|
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber'); |
|
|
|
const userId = wx.getStorageSync('userId') || userInfo?.userId; |
|
|
|
|
|
|
|
// 只有当电话号码存在时才传递,避免传递空值
|
|
|
|
// 构建请求参数
|
|
|
|
const params = { |
|
|
|
page: this.data.page, |
|
|
|
pageSize: this.data.pageSize |
|
|
|
}; |
|
|
|
|
|
|
|
// 只有当电话号码存在时才传递,避免传递空值
|
|
|
|
if (phoneNumber) { |
|
|
|
params.phone = phoneNumber; |
|
|
|
} |
|
|
|
|
|
|
|
// 传递userId参数,用于后端过滤动态
|
|
|
|
if (userId) { |
|
|
|
params.userId = userId; |
|
|
|
} |
|
|
|
|
|
|
|
API.getPosts(params).then(res => { |
|
|
|
console.log('后端返回的完整响应:', res); |
|
|
|
// 正确处理后端返回的响应格式
|
|
|
|
@ -103,7 +111,7 @@ Page({ |
|
|
|
console.log('后端返回的动态数量:', newPosts.length); |
|
|
|
console.log('后端返回的动态数据:', newPosts); |
|
|
|
|
|
|
|
// 处理images字段,确保它是一个数组
|
|
|
|
// 处理images字段,确保它是一个数组,并添加username字段
|
|
|
|
newPosts = newPosts.map(post => { |
|
|
|
if (post.images) { |
|
|
|
// 如果images是字符串,尝试解析为JSON数组
|
|
|
|
@ -126,9 +134,73 @@ Page({ |
|
|
|
// 如果images不存在,设置为空数组
|
|
|
|
post.images = []; |
|
|
|
} |
|
|
|
|
|
|
|
// 添加username字段(使用phone的前几位作为用户名)
|
|
|
|
if (!post.username) { |
|
|
|
if (post.phone) { |
|
|
|
// 隐藏手机号中间部分
|
|
|
|
post.username = post.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); |
|
|
|
} else { |
|
|
|
post.username = '用户' + post.user_id?.substring(0, 6) || '未知用户'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 添加time字段用于显示时间
|
|
|
|
if (!post.time && post.created_at) { |
|
|
|
const date = new Date(post.created_at); |
|
|
|
post.time = date.toLocaleString('zh-CN', { |
|
|
|
year: 'numeric', |
|
|
|
month: '2-digit', |
|
|
|
day: '2-digit', |
|
|
|
hour: '2-digit', |
|
|
|
minute: '2-digit' |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 确保topic字段存在
|
|
|
|
if (post.topic) { |
|
|
|
console.log('动态话题:', post.topic); |
|
|
|
} |
|
|
|
|
|
|
|
// 为了兼容前端模板,添加user对象
|
|
|
|
if (!post.user) { |
|
|
|
post.user = { |
|
|
|
avatar: post.avatar_url || '', |
|
|
|
nickname: post.username |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// 为了兼容前端模板,添加image字段(使用images数组的第一个元素)
|
|
|
|
if (!post.image && post.images && post.images.length > 0) { |
|
|
|
post.image = post.images[0]; |
|
|
|
} |
|
|
|
|
|
|
|
// 为了兼容前端模板,添加like_count和comment_count字段
|
|
|
|
if (post.likes !== undefined && post.like_count === undefined) { |
|
|
|
post.like_count = post.likes; |
|
|
|
} |
|
|
|
if (post.comments !== undefined && post.comment_count === undefined) { |
|
|
|
post.comment_count = post.comments; |
|
|
|
} |
|
|
|
|
|
|
|
return post; |
|
|
|
}); |
|
|
|
|
|
|
|
// 根据status字段过滤动态:0仅自己可见,1所有人可见,2仅自己可见
|
|
|
|
newPosts = newPosts.filter(post => { |
|
|
|
// 如果status为1,所有人可见
|
|
|
|
if (post.status === 1) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
// 如果status为0或2,仅自己可见
|
|
|
|
if (post.status === 0 || post.status === 2) { |
|
|
|
// 检查是否是当前用户的动态
|
|
|
|
return post.user_id === userId; |
|
|
|
} |
|
|
|
// 默认显示
|
|
|
|
return true; |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果是第一页且没有数据,使用默认动态数据
|
|
|
|
if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) { |
|
|
|
newPosts = [ |
|
|
|
@ -331,6 +403,29 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 预览图片
|
|
|
|
previewImage(e) { |
|
|
|
const images = e.currentTarget.dataset.images; |
|
|
|
const currentIndex = parseInt(e.currentTarget.dataset.current); |
|
|
|
|
|
|
|
console.log('预览图片:', { |
|
|
|
images, |
|
|
|
currentIndex, |
|
|
|
current: images[currentIndex] |
|
|
|
}); |
|
|
|
|
|
|
|
wx.previewImage({ |
|
|
|
current: images[currentIndex], |
|
|
|
urls: images, |
|
|
|
success: function(res) { |
|
|
|
console.log('预览图片成功:', res); |
|
|
|
}, |
|
|
|
fail: function(err) { |
|
|
|
console.error('预览图片失败:', err); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
createPost() { |
|
|
|
wx.navigateTo({ |
|
|
|
url: '/pages/eggbar/create-post' |
|
|
|
|