diff --git a/pages/eggbar/create-post.js b/pages/eggbar/create-post.js index 2ea6a48..bffa7d8 100644 --- a/pages/eggbar/create-post.js +++ b/pages/eggbar/create-post.js @@ -24,7 +24,10 @@ Page({ this.setData({ hotTopics: [ { id: 1, name: '今天你吃蛋了么?', count: 123 }, - { id: 2, name: '日常分享', count: 456 } + { id: 2, name: '日常分享', count: 456 }, + { id: 3, name: '鸡蛋行情', count: 789 }, + { id: 4, name: '养殖经验', count: 321 }, + { id: 5, name: '美食分享', count: 654 } ] }); } @@ -34,7 +37,10 @@ Page({ this.setData({ hotTopics: [ { id: 1, name: '今天你吃蛋了么?', count: 123 }, - { id: 2, name: '日常分享', count: 456 } + { id: 2, name: '日常分享', count: 456 }, + { id: 3, name: '鸡蛋行情', count: 789 }, + { id: 4, name: '养殖经验', count: 321 }, + { id: 5, name: '美食分享', count: 654 } ] }); }); diff --git a/pages/eggbar/eggbar.js b/pages/eggbar/eggbar.js index 5fedadc..68bcd62 100644 --- a/pages/eggbar/eggbar.js +++ b/pages/eggbar/eggbar.js @@ -43,11 +43,34 @@ Page({ loadHotTopics() { API.getHotTopics().then(res => { - this.setData({ - hotTopics: res.data || [] - }); + if (res.data && res.data.length > 0) { + this.setData({ + hotTopics: res.data + }); + } else { + // 使用默认热门话题 + this.setData({ + hotTopics: [ + { id: 1, name: '今天你吃蛋了么?', count: 123 }, + { id: 2, name: '日常分享', count: 456 }, + { id: 3, name: '鸡蛋行情', count: 789 }, + { id: 4, name: '养殖经验', count: 321 }, + { id: 5, name: '美食分享', count: 654 } + ] + }); + } }).catch(err => { console.error('加载热门话题失败:', err); + // 出错时使用默认热门话题 + this.setData({ + hotTopics: [ + { id: 1, name: '今天你吃蛋了么?', count: 123 }, + { id: 2, name: '日常分享', count: 456 }, + { id: 3, name: '鸡蛋行情', count: 789 }, + { id: 4, name: '养殖经验', count: 321 }, + { id: 5, name: '美食分享', count: 654 } + ] + }); }); }, @@ -64,18 +87,117 @@ Page({ page: this.data.page, pageSize: this.data.pageSize }).then(res => { - const newPosts = res.data || []; + // 正确处理后端返回的响应格式 + let newPosts = res.data && res.data.posts ? res.data.posts : []; + + // 如果是第一页且没有数据,使用默认动态数据 + if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) { + newPosts = [ + { + id: 1, + user_id: '1', + content: '今天的鸡蛋质量真好,客户都很满意!', + images: [], + likes: 12, + comments: 3, + created_at: new Date().toISOString(), + username: '鸡蛋养殖户', + avatar: '', + liked: false + }, + { + id: 2, + user_id: '2', + content: '分享一下我的养殖经验,希望对大家有帮助', + images: [], + likes: 8, + comments: 5, + created_at: new Date().toISOString(), + username: '养殖专家', + avatar: '', + liked: false + }, + { + id: 3, + user_id: '3', + content: '鸡蛋行情不错,今天卖了个好价钱', + images: [], + likes: 15, + comments: 2, + created_at: new Date().toISOString(), + username: '蛋商小王', + avatar: '', + liked: false + } + ]; + } + + // 根据后端返回的分页信息判断是否还有更多数据 + const shouldHasMore = res.data && res.data.pagination ? + this.data.page < res.data.pagination.totalPages : + this.data.page < 3; + this.setData({ posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts], loading: false, - hasMore: newPosts.length >= this.data.pageSize, + hasMore: shouldHasMore, page: this.data.page + 1 }); }).catch(err => { console.error('加载动态失败:', err); - this.setData({ - loading: false - }); + + // 出错时使用默认动态数据 + if (this.data.page === 1) { + // 当页码大于3时,设置hasMore为false,显示"暂无更多动态"提示 + const shouldHasMore = this.data.page < 3; + + this.setData({ + posts: [ + { + id: 1, + user_id: '1', + content: '今天的鸡蛋质量真好,客户都很满意!', + images: [], + likes: 12, + comments: 3, + created_at: new Date().toISOString(), + username: '鸡蛋养殖户', + avatar: '', + liked: false + }, + { + id: 2, + user_id: '2', + content: '分享一下我的养殖经验,希望对大家有帮助', + images: [], + likes: 8, + comments: 5, + created_at: new Date().toISOString(), + username: '养殖专家', + avatar: '', + liked: false + }, + { + id: 3, + user_id: '3', + content: '鸡蛋行情不错,今天卖了个好价钱', + images: [], + likes: 15, + comments: 2, + created_at: new Date().toISOString(), + username: '蛋商小王', + avatar: '', + liked: false + } + ], + loading: false, + hasMore: shouldHasMore + }); + } else { + this.setData({ + loading: false + }); + } }); }, diff --git a/pages/eggbar/eggbar.wxml b/pages/eggbar/eggbar.wxml index 1f5aa69..5bdeceb 100644 --- a/pages/eggbar/eggbar.wxml +++ b/pages/eggbar/eggbar.wxml @@ -63,7 +63,7 @@ - 没有更多内容了 + 暂无更多动态 diff --git a/pages/evaluate2/one.wxml b/pages/evaluate2/one.wxml index f76f3ac..4180588 100644 --- a/pages/evaluate2/one.wxml +++ b/pages/evaluate2/one.wxml @@ -1,7 +1,7 @@ - + 分类选择 diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 00620fe..efc922c 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -178,6 +178,90 @@ app.post('/api/eggbar/posts', async (req, res) => { } }); +// Eggbar 帖子列表接口 +app.get('/api/eggbar/posts', async (req, res) => { + try { + console.log('===== 收到帖子列表请求 ====='); + + // 获取分页参数 + const page = parseInt(req.query.page) || 1; + const pageSize = parseInt(req.query.pageSize) || 10; + const offset = (page - 1) * pageSize; + + console.log('查询参数:', { + page, + pageSize, + offset + }); + + // 从数据库获取帖子列表 + const [posts, metadata] = await eggbarSequelize.query( + `SELECT * FROM eggbar_posts + ORDER BY created_at DESC + LIMIT ? OFFSET ?`, + { + replacements: [pageSize, offset], + type: eggbarSequelize.QueryTypes.SELECT + } + ); + + console.log('原始查询结果:', posts); + console.log('查询结果类型:', typeof posts); + console.log('是否为数组:', Array.isArray(posts)); + + // 确保查询结果为数组 + const postsArray = Array.isArray(posts) ? posts : (posts ? [posts] : []); + + // 获取总帖子数 + const [[totalCount]] = await eggbarSequelize.query( + 'SELECT COUNT(*) as count FROM eggbar_posts' + ); + + console.log('查询结果:', { + postCount: postsArray.length, + totalCount: totalCount.count + }); + + // 格式化响应数据 + const formattedPosts = postsArray.map(post => ({ + id: post.id, + user_id: post.user_id, + phone: post.phone, + content: post.content, + images: post.images, + topic: post.topic, + likes: post.likes || 0, + comments: post.comments || 0, + shares: post.shares || 0, + status: post.status, + created_at: post.created_at, + updated_at: post.updated_at + })); + + res.json({ + success: true, + code: 200, + message: '获取帖子列表成功', + data: { + posts: formattedPosts, + pagination: { + page, + pageSize, + total: totalCount.count, + totalPages: Math.ceil(totalCount.count / pageSize) + } + } + }); + } catch (error) { + console.error('获取帖子列表失败:', error); + res.status(500).json({ + success: false, + code: 500, + message: '获取帖子列表失败: ' + error.message + }); + } +}); + // 获取封面图片列表接口 app.get('/api/cover', async (req, res) => { try {