diff --git a/pages/eggbar/create-post.js b/pages/eggbar/create-post.js index 3547114..e56594c 100644 --- a/pages/eggbar/create-post.js +++ b/pages/eggbar/create-post.js @@ -132,20 +132,47 @@ Page({ .then(uploadedImages => { console.log('图片上传完成,上传的图片数量:', uploadedImages.length); - // 获取用户ID - const userId = wx.getStorageSync('userId'); + // 获取用户ID - 从多个来源获取,确保可靠性 + let userId = wx.getStorageSync('userId'); + + // 如果没有userId,尝试从userInfo中获取 if (!userId) { - throw new Error('用户未登录'); + const userInfo = wx.getStorageSync('userInfo'); + userId = userInfo?.userId || userInfo?.id; } - // 获取用户电话号码 + // 再次检查userId + if (!userId) { + console.error('获取userId失败,本地存储中没有userId'); + throw new Error('用户未登录,请重新登录'); + } + + console.log('获取到的userId:', userId); + + // 获取用户信息 + let phoneNumber = null; + let avatarUrl = null; const userInfo = wx.getStorageSync('userInfo'); - const phoneNumber = userInfo?.phoneNumber || wx.getStorageSync('phoneNumber'); + + if (userInfo) { + if (userInfo?.phoneNumber) { + phoneNumber = userInfo.phoneNumber; + } + if (userInfo?.avatarUrl) { + avatarUrl = userInfo.avatarUrl; + } + } else { + phoneNumber = wx.getStorageSync('phoneNumber'); + avatarUrl = wx.getStorageSync('avatarUrl'); + } + console.log('获取到的用户电话号码:', phoneNumber); + console.log('获取到的用户头像URL:', avatarUrl); const postData = { user_id: userId, phone: phoneNumber, + avatar_url: avatarUrl, content: this.data.content, images: uploadedImages, topic: this.data.selectedTopic?.name || this.data.selectedTopic @@ -169,7 +196,7 @@ Page({ console.error('发布动态失败:', err); wx.hideLoading(); wx.showToast({ - title: '发布失败,请重试', + title: err.message || '发布失败,请重试', icon: 'none' }); }); diff --git a/pages/eggbar/eggbar.js b/pages/eggbar/eggbar.js index ee665ad..68f4f8a 100644 --- a/pages/eggbar/eggbar.js +++ b/pages/eggbar/eggbar.js @@ -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,6 +134,29 @@ 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' + }); + } + return post; }); diff --git a/pages/eggbar/eggbar.wxml b/pages/eggbar/eggbar.wxml index a75559c..595444b 100644 --- a/pages/eggbar/eggbar.wxml +++ b/pages/eggbar/eggbar.wxml @@ -29,7 +29,7 @@ - + {{item.username}} {{item.time}} diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index f30dc2b..b210614 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -132,7 +132,7 @@ app.post('/api/eggbar/posts', async (req, res) => { console.log('===== 收到帖子创建请求 ====='); console.log('1. 收到请求体:', JSON.stringify(req.body, null, 2)); - const { user_id, content, images, topic, phone } = req.body; + const { user_id, content, images, topic, phone, avatar_url } = req.body; // 数据验证 if (!user_id) { @@ -153,6 +153,7 @@ app.post('/api/eggbar/posts', async (req, res) => { const newPost = await EggbarPost.create({ user_id: user_id, phone: phone || null, + avatar_url: avatar_url || null, content: content || null, images: imagesData, topic: topic || null @@ -260,6 +261,7 @@ app.get('/api/eggbar/posts', async (req, res) => { id: post.id, user_id: post.user_id, phone: post.phone, + avatar_url: post.avatar_url, content: post.content, images: images, topic: post.topic, @@ -272,8 +274,29 @@ app.get('/api/eggbar/posts', async (req, res) => { }; }); - // 检查用户是否已点赞 + // 获取当前用户信息 const phone = req.query.phone || req.headers['x-phone']; + const userId = req.query.userId || req.headers['x-user-id']; + + // 根据status字段和用户信息过滤动态 + formattedPosts = formattedPosts.filter(post => { + // 获取帖子状态,默认为0 + const postStatus = post.status || 0; + + // status=1:审核通过,所有用户都可见 + if (postStatus === 1) { + return true; + } + + // status=0(待审核)或2(拒绝):只有发布者可见 + // 检查是否是发布者(通过phone或userId) + const isPublisherByPhone = phone && post.phone === phone; + const isPublisherByUserId = userId && post.user_id === userId; + + return isPublisherByPhone || isPublisherByUserId; + }); + + // 检查用户是否已点赞 if (phone) { try { // 批量检查点赞状态 @@ -1743,6 +1766,11 @@ EggbarPost.init({ allowNull: true, comment: '用户电话号码' }, + avatar_url: { + type: DataTypes.TEXT, + allowNull: true, + comment: '用户头像URL' + }, content: { type: DataTypes.TEXT, allowNull: true, diff --git a/utils/api.js b/utils/api.js index d1052ff..8b72cc2 100644 --- a/utils/api.js +++ b/utils/api.js @@ -2266,6 +2266,10 @@ module.exports = { getApp().globalData.userType = userType; } + // 将用户信息存储到本地存储 + wx.setStorageSync('userInfo', userInfo); + console.log('用户信息已存储到本地存储:', userInfo); + // 将用户类型更新到服务器数据库 if (isCustomerService) { console.log('将客服身份更新到服务器数据库');