Files
edu_mp/pages/index/index.js
2025-12-05 15:41:32 +08:00

139 lines
3.3 KiB
JavaScript

// pages/index/index.js
const { get } = require('../../utils/request');
const { formatRelativeTime } = require('../../utils/util');
Page({
data: {
searchValue: '',
bannerList: [],
categoryList: [],
infoList: [],
loading: false,
page: 1,
pageSize: 10,
hasMore: true
},
onLoad() {
this.initData();
},
onShow() {
// 初始化 TabBar 选中状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().init();
}
},
onPullDownRefresh() {
this.initData().then(() => {
wx.stopPullDownRefresh();
});
},
onReachBottom() {
if (this.data.hasMore && !this.data.loading) {
this.loadMoreInfo();
}
},
// 初始化数据
async initData() {
this.setData({ page: 1, hasMore: true });
// 模拟数据 - 实际项目中替换为API调用
this.setData({
bannerList: [
{ id: 1, image: '/assets/images/banner1.png', title: '热门资讯' },
{ id: 2, image: '/assets/images/banner2.png', title: '最新动态' }
],
categoryList: [
{ id: 1, name: '科技', icon: 'technology' },
{ id: 2, name: '财经', icon: 'finance' },
{ id: 3, name: '教育', icon: 'education' },
{ id: 4, name: '健康', icon: 'health' },
{ id: 5, name: '生活', icon: 'life' },
{ id: 6, name: '娱乐', icon: 'entertainment' },
{ id: 7, name: '体育', icon: 'sports' },
{ id: 8, name: '更多', icon: 'more' }
],
infoList: [
{
id: 1,
title: '这是一条测试信息标题,展示信息服务的基本样式',
summary: '这是信息摘要内容,用于展示信息的简要描述...',
image: '/assets/images/info1.png',
category: '科技',
time: new Date().getTime() - 3600000,
views: 1234
},
{
id: 2,
title: '第二条信息标题,这里是标题内容展示',
summary: '这是第二条信息的摘要内容...',
image: '/assets/images/info2.png',
category: '财经',
time: new Date().getTime() - 7200000,
views: 856
}
]
});
},
// 加载更多信息
async loadMoreInfo() {
if (this.data.loading) return;
this.setData({ loading: true });
// 模拟加载更多
setTimeout(() => {
const newPage = this.data.page + 1;
// 模拟无更多数据
if (newPage > 3) {
this.setData({ hasMore: false, loading: false });
return;
}
this.setData({
page: newPage,
loading: false
});
}, 1000);
},
// 搜索
onSearch(e) {
const value = e.detail;
wx.navigateTo({
url: `/pages/search/search?keyword=${value}`
});
},
// 搜索值变化
onSearchChange(e) {
this.setData({ searchValue: e.detail });
},
// 点击分类
onCategoryTap(e) {
const { id, name } = e.currentTarget.dataset;
wx.navigateTo({
url: `/pages/category/category?id=${id}&name=${name}`
});
},
// 点击信息
onInfoTap(e) {
const { id } = e.currentTarget.dataset;
wx.navigateTo({
url: `/pages/detail/detail?id=${id}`
});
},
// 格式化时间
formatTime(time) {
return formatRelativeTime(time);
}
});