Files
ZhiQiXiaoYuan/pages/index/index.js
ChuXun eaab9a762a 1
2025-10-19 20:28:31 +08:00

284 lines
6.5 KiB
JavaScript

// pages/index/index.js
const app = getApp()
const userManager = require('../../utils/userManager.js')
const learningTracker = require('../../utils/learningTracker.js')
Page({
data: {
userInfo: null,
features: [
{
id: 1,
name: '启思AI',
icon: '🤖',
desc: '启迪思维,智慧学习助手',
path: '/pages/ai-assistant/ai-assistant',
color: '#6C5CE7',
badge: 'AI',
badgeColor: '#A29BFE'
},
{
id: 2,
name: '学习数据',
icon: '📊',
desc: '可视化数据分析看板',
path: '/pages/dashboard/dashboard',
color: '#667eea',
badge: 'NEW',
badgeColor: '#4CAF50'
},
{
id: 3,
name: '课程中心',
icon: '📚',
desc: '海量课程资源,精准筛选',
path: '/pages/courses/courses',
color: '#4A90E2',
badge: 'HOT',
badgeColor: '#FF5252'
},
{
id: 4,
name: '学科论坛',
icon: '💬',
desc: '学术交流,思维碰撞',
path: '/pages/forum/forum',
color: '#50C878'
},
{
id: 5,
name: '学习工具',
icon: '🛠️',
desc: 'GPA计算、课表、倒计时',
path: '/pages/tools/tools',
color: '#9B59B6'
},
{
id: 6,
name: '个人中心',
icon: '👤',
desc: '个性设置,数据管理',
path: '/pages/my/my',
color: '#F39C12'
}
],
notices: [
'欢迎使用知芽小筑!',
'新增课程筛选功能,快来体验吧',
'学科论坛上线,与同学们一起交流学习'
],
currentNotice: 0
},
onLoad() {
// 获取用户信息
this.getUserInfo()
// 启动公告轮播
this.startNoticeCarousel()
},
onShow() {
// 开始跟踪学习时间
learningTracker.onPageShow('index')
// 每次显示时刷新用户信息
this.getUserInfo()
// 更新自定义TabBar选中状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
},
onHide() {
// 停止跟踪学习时间
learningTracker.onPageHide()
},
onUnload() {
// 记录学习时长
learningTracker.onPageUnload()
// 清除定时器
if (this.noticeTimer) {
clearInterval(this.noticeTimer)
}
},
// 获取用户信息
getUserInfo() {
const userInfo = userManager.getUserInfo()
if (userInfo && userInfo.isLogin) {
this.setData({ userInfo })
} else {
// 未登录状态
this.setData({ userInfo: null })
}
},
// 退出登录
onLogout() {
wx.showModal({
title: '退出登录',
content: '确定要退出登录吗?',
confirmText: '退出',
cancelText: '取消',
confirmColor: '#FF5252',
success: (res) => {
if (res.confirm) {
// 清除用户信息
userManager.clearUserInfo()
this.setData({ userInfo: null })
wx.showToast({
title: '已退出登录',
icon: 'success'
})
// 震动反馈
wx.vibrateShort({
type: 'light'
})
}
}
})
},
// 微信授权登录
onLogin() {
const that = this
// 显示加载提示
wx.showLoading({
title: '正在登录...',
mask: true
})
// 获取用户信息
wx.getUserProfile({
desc: '完善用户资料',
lang: 'zh_CN',
success: (res) => {
console.log('获取用户信息成功', res)
// 使用 userManager 保存用户信息
const userInfo = {
nickname: res.userInfo.nickName,
avatar: res.userInfo.avatarUrl,
gender: res.userInfo.gender,
country: res.userInfo.country,
province: res.userInfo.province,
city: res.userInfo.city,
isLogin: true,
loginTime: new Date().getTime()
}
// 保存用户信息(会自动处理字段兼容性)
userManager.saveUserInfo(userInfo)
that.setData({ userInfo })
console.log('登录成功,保存的用户信息:', userInfo)
// 隐藏加载提示
wx.hideLoading()
// 显示成功提示
wx.showToast({
title: `欢迎,${userInfo.nickname}`,
icon: 'success',
duration: 2000
})
// 震动反馈
wx.vibrateShort({
type: 'light'
})
},
fail: (err) => {
console.error('获取用户信息失败', err)
// 隐藏加载提示
wx.hideLoading()
// 显示错误提示
wx.showModal({
title: '登录提示',
content: '登录已取消,部分功能可能受限',
showCancel: false,
confirmText: '我知道了',
confirmColor: '#667eea'
})
}
})
},
// 公告轮播
startNoticeCarousel() {
this.noticeTimer = setInterval(() => {
const { currentNotice, notices } = this.data
this.setData({
currentNotice: (currentNotice + 1) % notices.length
})
}, 3000)
},
// 功能卡片点击
onFeatureClick(e) {
console.log('功能卡片被点击', e)
const { path } = e.currentTarget.dataset
console.log('跳转路径:', path)
if (!path) {
wx.showToast({
title: '路径配置错误',
icon: 'none'
})
return
}
// TabBar 页面列表
const tabBarPages = [
'/pages/index/index',
'/pages/courses/courses',
'/pages/tools/tools',
'/pages/forum/forum',
'/pages/my/my'
]
// 判断是否为 TabBar 页面
if (tabBarPages.includes(path)) {
wx.switchTab({
url: path,
success: () => {
console.log('切换TabBar成功:', path)
},
fail: (err) => {
console.error('TabBar切换失败:', err)
wx.showToast({
title: '页面切换失败',
icon: 'none'
})
}
})
} else {
wx.navigateTo({
url: path,
success: () => {
console.log('跳转成功:', path)
},
fail: (err) => {
console.error('页面跳转失败:', err)
wx.showToast({
title: '页面跳转失败',
icon: 'none'
})
}
})
}
}
})