初始
This commit is contained in:
126
pages/category/category.js
Normal file
126
pages/category/category.js
Normal file
@@ -0,0 +1,126 @@
|
||||
// pages/category/category.js
|
||||
const { get } = require('../../utils/request');
|
||||
|
||||
Page({
|
||||
data: {
|
||||
activeTab: 0,
|
||||
categoryList: [],
|
||||
infoList: [],
|
||||
loading: false,
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
hasMore: true
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.initData();
|
||||
|
||||
// 如果从首页跳转过来带有分类ID
|
||||
if (options.id) {
|
||||
const tabIndex = parseInt(options.id) - 1;
|
||||
this.setData({ activeTab: tabIndex });
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 初始化 TabBar 选中状态
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().init();
|
||||
}
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
this.refreshList().then(() => {
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
onReachBottom() {
|
||||
if (this.data.hasMore && !this.data.loading) {
|
||||
this.loadMore();
|
||||
}
|
||||
},
|
||||
|
||||
// 初始化数据
|
||||
async initData() {
|
||||
// 模拟分类数据
|
||||
this.setData({
|
||||
categoryList: [
|
||||
{ id: 1, name: '科技' },
|
||||
{ id: 2, name: '财经' },
|
||||
{ id: 3, name: '教育' },
|
||||
{ id: 4, name: '健康' },
|
||||
{ id: 5, name: '生活' },
|
||||
{ id: 6, name: '娱乐' }
|
||||
]
|
||||
});
|
||||
|
||||
this.refreshList();
|
||||
},
|
||||
|
||||
// 刷新列表
|
||||
async refreshList() {
|
||||
this.setData({ page: 1, hasMore: true, infoList: [] });
|
||||
await this.loadList();
|
||||
},
|
||||
|
||||
// 加载列表
|
||||
async loadList() {
|
||||
this.setData({ loading: true });
|
||||
|
||||
// 模拟数据
|
||||
setTimeout(() => {
|
||||
const mockList = [
|
||||
{
|
||||
id: 1,
|
||||
title: '分类信息标题示例',
|
||||
summary: '这是分类信息的摘要内容...',
|
||||
image: '/assets/images/info1.png',
|
||||
time: '2小时前',
|
||||
views: 1234
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: '第二条分类信息',
|
||||
summary: '这是第二条信息的摘要...',
|
||||
image: '/assets/images/info2.png',
|
||||
time: '3小时前',
|
||||
views: 856
|
||||
}
|
||||
];
|
||||
|
||||
this.setData({
|
||||
infoList: [...this.data.infoList, ...mockList],
|
||||
loading: false
|
||||
});
|
||||
}, 500);
|
||||
},
|
||||
|
||||
// 加载更多
|
||||
async loadMore() {
|
||||
if (this.data.loading) return;
|
||||
|
||||
const newPage = this.data.page + 1;
|
||||
if (newPage > 3) {
|
||||
this.setData({ hasMore: false });
|
||||
return;
|
||||
}
|
||||
|
||||
this.setData({ page: newPage });
|
||||
await this.loadList();
|
||||
},
|
||||
|
||||
// 切换分类
|
||||
onTabChange(e) {
|
||||
this.setData({ activeTab: e.detail.index });
|
||||
this.refreshList();
|
||||
},
|
||||
|
||||
// 点击信息
|
||||
onInfoTap(e) {
|
||||
const { id } = e.currentTarget.dataset;
|
||||
wx.navigateTo({
|
||||
url: `/pages/detail/detail?id=${id}`
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user