|
|
@ -43,11 +43,34 @@ Page({ |
|
|
|
|
|
|
|
|
loadHotTopics() { |
|
|
loadHotTopics() { |
|
|
API.getHotTopics().then(res => { |
|
|
API.getHotTopics().then(res => { |
|
|
this.setData({ |
|
|
if (res.data && res.data.length > 0) { |
|
|
hotTopics: res.data || [] |
|
|
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 => { |
|
|
}).catch(err => { |
|
|
console.error('加载热门话题失败:', 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, |
|
|
page: this.data.page, |
|
|
pageSize: this.data.pageSize |
|
|
pageSize: this.data.pageSize |
|
|
}).then(res => { |
|
|
}).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({ |
|
|
this.setData({ |
|
|
posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts], |
|
|
posts: this.data.page === 1 ? newPosts : [...this.data.posts, ...newPosts], |
|
|
loading: false, |
|
|
loading: false, |
|
|
hasMore: newPosts.length >= this.data.pageSize, |
|
|
hasMore: shouldHasMore, |
|
|
page: this.data.page + 1 |
|
|
page: this.data.page + 1 |
|
|
}); |
|
|
}); |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('加载动态失败:', 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 |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|