727 lines
18 KiB
JavaScript
727 lines
18 KiB
JavaScript
// pages/my/my.js
|
||
const userManager = require('../../utils/userManager.js')
|
||
const learningTracker = require('../../utils/learningTracker.js')
|
||
|
||
Page({
|
||
data: {
|
||
userInfo: null,
|
||
stats: {
|
||
favoriteCourses: 0,
|
||
myPosts: 0,
|
||
myComments: 0
|
||
},
|
||
menuList: [
|
||
{
|
||
id: 1,
|
||
icon: '✏️',
|
||
title: '编辑资料',
|
||
desc: '修改昵称和头像',
|
||
arrow: true,
|
||
type: 'edit'
|
||
},
|
||
{
|
||
id: 2,
|
||
icon: '❤️',
|
||
title: '我的收藏',
|
||
desc: '收藏的课程和帖子',
|
||
arrow: true,
|
||
type: 'favorite'
|
||
},
|
||
{
|
||
id: 3,
|
||
icon: '📝',
|
||
title: '我的帖子',
|
||
desc: '查看发布的帖子',
|
||
arrow: true,
|
||
type: 'posts'
|
||
},
|
||
{
|
||
id: 4,
|
||
icon: '🔔',
|
||
title: '消息通知',
|
||
desc: '系统消息和互动提醒',
|
||
arrow: true,
|
||
type: 'notification'
|
||
},
|
||
{
|
||
id: 5,
|
||
icon: '⚙️',
|
||
title: '通用设置',
|
||
desc: '隐私、通知等设置',
|
||
arrow: true,
|
||
type: 'settings'
|
||
},
|
||
{
|
||
id: 6,
|
||
icon: '❓',
|
||
title: '帮助与反馈',
|
||
desc: '使用指南和问题反馈',
|
||
arrow: true,
|
||
type: 'help'
|
||
},
|
||
{
|
||
id: 7,
|
||
icon: '📊',
|
||
title: '数据统计',
|
||
desc: '学习数据和使用记录',
|
||
arrow: true,
|
||
type: 'statistics'
|
||
},
|
||
{
|
||
id: 8,
|
||
icon: '🚪',
|
||
title: '退出登录',
|
||
desc: '退出当前账号',
|
||
arrow: false,
|
||
type: 'logout',
|
||
danger: true
|
||
}
|
||
],
|
||
showEditDialog: false,
|
||
editNickname: ''
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadUserInfo()
|
||
this.loadStats()
|
||
},
|
||
|
||
onShow() {
|
||
// 开始跟踪学习时间
|
||
learningTracker.onPageShow('tools')
|
||
|
||
this.loadUserInfo()
|
||
this.loadStats()
|
||
|
||
// 更新自定义TabBar选中状态
|
||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||
this.getTabBar().setData({
|
||
selected: 4
|
||
})
|
||
}
|
||
},
|
||
|
||
onHide() {
|
||
// 停止跟踪学习时间
|
||
learningTracker.onPageHide()
|
||
},
|
||
|
||
onUnload() {
|
||
// 记录学习时长
|
||
learningTracker.onPageUnload()
|
||
},
|
||
|
||
// 获取用户信息
|
||
loadUserInfo() {
|
||
const userInfo = userManager.getUserInfo()
|
||
this.setData({
|
||
userInfo: userInfo
|
||
})
|
||
console.log('加载用户信息:', userInfo)
|
||
},
|
||
|
||
// 点击头像上传
|
||
onChooseAvatar() {
|
||
const that = this
|
||
wx.chooseImage({
|
||
count: 1,
|
||
sizeType: ['compressed'],
|
||
sourceType: ['album', 'camera'],
|
||
success(res) {
|
||
const tempFilePath = res.tempFilePaths[0]
|
||
|
||
// 更新头像
|
||
userManager.updateAvatar(tempFilePath)
|
||
|
||
// 更新页面显示
|
||
const updatedUserInfo = userManager.getUserInfo()
|
||
that.setData({
|
||
userInfo: updatedUserInfo
|
||
})
|
||
|
||
console.log('头像更新成功,保存的数据:', updatedUserInfo)
|
||
|
||
wx.showToast({
|
||
title: '头像更新成功',
|
||
icon: 'success'
|
||
})
|
||
},
|
||
fail() {
|
||
wx.showToast({
|
||
title: '取消选择',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 显示编辑昵称对话框
|
||
showEditNicknameDialog() {
|
||
const { userInfo } = this.data
|
||
this.setData({
|
||
showEditDialog: true,
|
||
editNickname: userInfo.nickname || ''
|
||
})
|
||
},
|
||
|
||
// 关闭编辑对话框
|
||
closeEditDialog() {
|
||
this.setData({
|
||
showEditDialog: false,
|
||
editNickname: ''
|
||
})
|
||
},
|
||
|
||
// 昵称输入
|
||
onNicknameInput(e) {
|
||
this.setData({
|
||
editNickname: e.detail.value
|
||
})
|
||
},
|
||
|
||
// 保存昵称
|
||
saveNickname() {
|
||
const { editNickname } = this.data
|
||
|
||
if (!editNickname.trim()) {
|
||
wx.showToast({
|
||
title: '请输入昵称',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
|
||
if (editNickname.length > 10) {
|
||
wx.showToast({
|
||
title: '昵称最多10个字',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
|
||
// 更新昵称
|
||
userManager.updateNickname(editNickname.trim())
|
||
|
||
// 更新页面显示
|
||
const updatedUserInfo = userManager.getUserInfo()
|
||
this.setData({
|
||
userInfo: updatedUserInfo,
|
||
showEditDialog: false,
|
||
editNickname: ''
|
||
})
|
||
|
||
console.log('昵称更新成功,保存的数据:', updatedUserInfo)
|
||
|
||
wx.showToast({
|
||
title: '昵称修改成功',
|
||
icon: 'success'
|
||
})
|
||
},
|
||
|
||
// 加载统计数据
|
||
loadStats() {
|
||
const userInfo = userManager.getUserInfo()
|
||
const favoriteCourses = wx.getStorageSync('favoriteCourses') || []
|
||
const forumPosts = wx.getStorageSync('forumPosts') || []
|
||
|
||
// 获取当前用户的昵称
|
||
const currentUserName = userInfo.nickname || '同学'
|
||
|
||
// 统计我的帖子
|
||
const myPosts = forumPosts.filter(post => {
|
||
return post.author === currentUserName || post.author === '我'
|
||
})
|
||
|
||
// 统计我的评论(从帖子详情中统计)
|
||
let totalComments = 0
|
||
forumPosts.forEach(post => {
|
||
if (post.commentList && Array.isArray(post.commentList)) {
|
||
const myComments = post.commentList.filter(comment => {
|
||
return comment.author === currentUserName || comment.author === '我'
|
||
})
|
||
totalComments += myComments.length
|
||
}
|
||
})
|
||
|
||
this.setData({
|
||
stats: {
|
||
favoriteCourses: favoriteCourses.length,
|
||
myPosts: myPosts.length,
|
||
myComments: totalComments
|
||
}
|
||
})
|
||
|
||
console.log('统计数据更新:', {
|
||
用户名: currentUserName,
|
||
收藏课程: favoriteCourses.length,
|
||
发布帖子: myPosts.length,
|
||
评论数: totalComments
|
||
})
|
||
},
|
||
|
||
// 菜单点击
|
||
onMenuClick(e) {
|
||
const { id } = e.currentTarget.dataset
|
||
const menuItem = this.data.menuList.find(item => item.id === id)
|
||
|
||
if (!menuItem) return
|
||
|
||
switch(menuItem.type) {
|
||
case 'edit':
|
||
// 编辑资料
|
||
this.showEditNicknameDialog()
|
||
break
|
||
|
||
case 'favorite':
|
||
// 我的收藏
|
||
this.showMyFavorites()
|
||
break
|
||
|
||
case 'posts':
|
||
// 我的帖子
|
||
this.showMyPosts()
|
||
break
|
||
|
||
case 'notification':
|
||
// 消息通知
|
||
this.showNotifications()
|
||
break
|
||
|
||
case 'settings':
|
||
// 通用设置
|
||
this.showSettings()
|
||
break
|
||
|
||
case 'help':
|
||
// 帮助与反馈
|
||
this.showHelp()
|
||
break
|
||
|
||
case 'statistics':
|
||
// 数据统计
|
||
this.showStatistics()
|
||
break
|
||
|
||
case 'logout':
|
||
// 退出登录
|
||
this.handleLogout()
|
||
break
|
||
|
||
default:
|
||
wx.showToast({
|
||
title: '功能开发中',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
},
|
||
|
||
// 我的收藏
|
||
showMyFavorites() {
|
||
const favoriteCourseIds = wx.getStorageSync('favoriteCourses') || []
|
||
const favoritePosts = wx.getStorageSync('favoritePosts') || []
|
||
|
||
// 从课程数据中获取收藏的课程详细信息
|
||
const { coursesData } = require('../../utils/data.js')
|
||
const favoriteCourses = coursesData.filter(course =>
|
||
favoriteCourseIds.includes(course.id)
|
||
)
|
||
|
||
const totalFavorites = favoriteCourses.length + favoritePosts.length
|
||
|
||
if (totalFavorites === 0) {
|
||
wx.showModal({
|
||
title: '我的收藏',
|
||
content: '暂无收藏内容\n\n可以收藏:\n• 喜欢的课程\n• 有用的论坛帖子',
|
||
showCancel: false,
|
||
confirmText: '去看看',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.switchTab({ url: '/pages/courses/courses' })
|
||
}
|
||
}
|
||
})
|
||
} else {
|
||
wx.showActionSheet({
|
||
itemList: ['查看收藏的课程', '查看收藏的帖子', '查看所有收藏', '取消'],
|
||
success: (res) => {
|
||
if (res.tapIndex === 0) {
|
||
// 查看课程收藏
|
||
this.showFavoriteCoursesList(favoriteCourses)
|
||
} else if (res.tapIndex === 1) {
|
||
// 查看帖子收藏
|
||
this.showFavoritePostsList(favoritePosts)
|
||
} else if (res.tapIndex === 2) {
|
||
// 查看所有收藏
|
||
this.showAllFavorites(favoriteCourses, favoritePosts)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
// 显示所有收藏概览
|
||
showAllFavorites(favoriteCourses, favoritePosts) {
|
||
let contentLines = []
|
||
|
||
// 课程收藏
|
||
if (favoriteCourses.length > 0) {
|
||
contentLines.push(`📚 收藏课程 (${favoriteCourses.length}门)`)
|
||
favoriteCourses.slice(0, 5).forEach((course, index) => {
|
||
contentLines.push(`${index + 1}. ${course.name} - ${course.teacher}`)
|
||
})
|
||
if (favoriteCourses.length > 5) {
|
||
contentLines.push(` ...还有${favoriteCourses.length - 5}门课程`)
|
||
}
|
||
contentLines.push('')
|
||
}
|
||
|
||
// 帖子收藏 - 过滤有效数据
|
||
const validPosts = favoritePosts.filter(post => post && post.title)
|
||
if (validPosts.length > 0) {
|
||
contentLines.push(`📝 收藏帖子 (${validPosts.length}条)`)
|
||
validPosts.slice(0, 5).forEach((post, index) => {
|
||
contentLines.push(`${index + 1}. ${post.title}`)
|
||
})
|
||
if (validPosts.length > 5) {
|
||
contentLines.push(` ...还有${validPosts.length - 5}条帖子`)
|
||
}
|
||
}
|
||
|
||
wx.showModal({
|
||
title: `⭐ 我的收藏`,
|
||
content: contentLines.join('\n'),
|
||
showCancel: false,
|
||
confirmText: '知道了',
|
||
confirmColor: '#667eea'
|
||
})
|
||
},
|
||
|
||
// 显示收藏课程列表
|
||
showFavoriteCoursesList(favoriteCourses) {
|
||
if (favoriteCourses.length === 0) {
|
||
wx.showModal({
|
||
title: '收藏的课程',
|
||
content: '还没有收藏任何课程\n去课程中心看看吧!',
|
||
showCancel: false,
|
||
confirmText: '去看看',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.switchTab({ url: '/pages/courses/courses' })
|
||
}
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
const courseTitles = favoriteCourses.map(course =>
|
||
`${course.name} - ${course.teacher}`
|
||
)
|
||
|
||
wx.showActionSheet({
|
||
itemList: courseTitles,
|
||
success: (res) => {
|
||
const selectedCourse = favoriteCourses[res.tapIndex]
|
||
// 跳转到课程详情
|
||
wx.navigateTo({
|
||
url: `/pages/course-detail/course-detail?id=${selectedCourse.id}`
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 显示收藏帖子列表
|
||
showFavoritePostsList(favoritePosts) {
|
||
console.log('收藏的帖子数据:', favoritePosts)
|
||
console.log('收藏的帖子数量:', favoritePosts.length)
|
||
|
||
if (favoritePosts.length === 0) {
|
||
wx.showModal({
|
||
title: '收藏的帖子',
|
||
content: '还没有收藏任何帖子\n去论坛看看吧!',
|
||
showCancel: false,
|
||
confirmText: '去看看',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.switchTab({ url: '/pages/forum/forum' })
|
||
}
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
// 确保每个帖子都有title和id
|
||
const validPosts = favoritePosts.filter(post => post && post.title && post.id)
|
||
|
||
if (validPosts.length === 0) {
|
||
wx.showModal({
|
||
title: '收藏的帖子',
|
||
content: '收藏数据异常\n请重新收藏帖子',
|
||
showCancel: false,
|
||
confirmText: '知道了',
|
||
confirmColor: '#667eea'
|
||
})
|
||
return
|
||
}
|
||
|
||
const postTitles = validPosts.map(post => post.title)
|
||
|
||
wx.showActionSheet({
|
||
itemList: postTitles,
|
||
success: (res) => {
|
||
const selectedPost = validPosts[res.tapIndex]
|
||
console.log('选中的帖子:', selectedPost)
|
||
// 跳转到帖子详情
|
||
wx.navigateTo({
|
||
url: `/pages/forum-detail/forum-detail?id=${selectedPost.id}`
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 我的帖子
|
||
showMyPosts() {
|
||
const userInfo = userManager.getUserInfo()
|
||
const currentUserName = userInfo.nickname || '同学'
|
||
const forumPosts = wx.getStorageSync('forumPosts') || []
|
||
|
||
const myPosts = forumPosts.filter(post => {
|
||
return post.author === currentUserName || post.author === '我'
|
||
})
|
||
|
||
console.log('我的帖子查询', {
|
||
当前用户名: currentUserName,
|
||
总帖子数: forumPosts.length,
|
||
我的帖子数: myPosts.length,
|
||
我的帖子: myPosts.map(p => ({ 标题: p.title, 作者: p.author }))
|
||
})
|
||
|
||
if (myPosts.length === 0) {
|
||
wx.showModal({
|
||
title: '我的帖子',
|
||
content: '还没有发布过帖子\n去论坛分享你的想法吧!',
|
||
showCancel: false,
|
||
confirmText: '去发帖',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.switchTab({
|
||
url: '/pages/forum/forum'
|
||
})
|
||
}
|
||
}
|
||
})
|
||
} else {
|
||
// 显示帖子列表供用户选择
|
||
const postTitles = myPosts.map(post => {
|
||
const commentCount = post.comments || 0
|
||
const likeCount = post.likes || 0
|
||
return `${post.title} (💬${commentCount} ❤️${likeCount})`
|
||
})
|
||
|
||
wx.showActionSheet({
|
||
itemList: postTitles,
|
||
success: (res) => {
|
||
const selectedPost = myPosts[res.tapIndex]
|
||
// 跳转到帖子详情
|
||
wx.navigateTo({
|
||
url: `/pages/forum-detail/forum-detail?id=${selectedPost.id}`
|
||
})
|
||
}
|
||
})
|
||
}
|
||
},
|
||
|
||
// 消息通知
|
||
showNotifications() {
|
||
wx.showModal({
|
||
title: '💌 消息通知',
|
||
content: '暂无新消息\n\n系统会在这里提醒您:\n• 帖子的点赞和评论\n• 课程更新通知\n• 系统公告',
|
||
showCancel: false,
|
||
confirmText: '我知道了',
|
||
confirmColor: '#667eea'
|
||
})
|
||
},
|
||
|
||
// 通用设置
|
||
showSettings() {
|
||
const settingsContent = [
|
||
'⚙️ 通用设置',
|
||
'',
|
||
'✓ 消息推送:已开启',
|
||
'✓ 隐私保护:已开启',
|
||
'✓ 缓存管理:自动清理',
|
||
'✓ 深色模式:跟随系统',
|
||
'',
|
||
'点击确定返回'
|
||
].join('\n')
|
||
|
||
wx.showModal({
|
||
title: '设置中心',
|
||
content: settingsContent,
|
||
showCancel: false,
|
||
confirmText: '确定',
|
||
confirmColor: '#667eea'
|
||
})
|
||
},
|
||
|
||
// 帮助与反馈
|
||
showHelp() {
|
||
wx.showModal({
|
||
title: '📖 帮助中心',
|
||
content: '使用指南:\n\n1. 课程中心:浏览和收藏课程\n2. 学科论坛:发帖交流学习\n3. 学习工具:GPA计算等工具\n4. 个人中心:管理个人信息\n\n遇到问题?请联系管理员',
|
||
showCancel: true,
|
||
cancelText: '返回',
|
||
confirmText: '联系我们',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
this.showContactInfo()
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 显示联系方式
|
||
showContactInfo() {
|
||
wx.showModal({
|
||
title: '📞 联系我们',
|
||
content: '客服邮箱:\n19354618812@163.com\n\n客服电话:\n19354618812\n\n工作时间:\n周一至周五 9:00-18:00\n\n欢迎您的咨询和建议!',
|
||
showCancel: true,
|
||
cancelText: '关闭',
|
||
confirmText: '复制邮箱',
|
||
confirmColor: '#667eea',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.setClipboardData({
|
||
data: '19354618812@163.com',
|
||
success: () => {
|
||
wx.showToast({
|
||
title: '邮箱已复制',
|
||
icon: 'success'
|
||
})
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 数据统计
|
||
showStatistics() {
|
||
const { stats } = this.data
|
||
const userInfo = userManager.getUserInfo()
|
||
const currentUserName = userInfo.nickname || '同学'
|
||
const forumPosts = wx.getStorageSync('forumPosts') || []
|
||
|
||
// 获取我的评论详情
|
||
let myCommentsList = []
|
||
forumPosts.forEach(post => {
|
||
if (post.commentList && Array.isArray(post.commentList)) {
|
||
post.commentList.forEach(comment => {
|
||
if (comment.author === currentUserName || comment.author === '我') {
|
||
myCommentsList.push({
|
||
postTitle: post.title,
|
||
content: comment.content,
|
||
time: comment.time
|
||
})
|
||
}
|
||
})
|
||
}
|
||
})
|
||
|
||
// 构建详细内容
|
||
let contentLines = [
|
||
'📊 我的数据',
|
||
'',
|
||
`📚 收藏课程:${stats.favoriteCourses} 门`,
|
||
`📝 发布帖子:${stats.myPosts} 条`,
|
||
`💬 评论数量:${stats.myComments} 次`,
|
||
''
|
||
]
|
||
|
||
// 显示最近的评论(最多3条)
|
||
if (myCommentsList.length > 0) {
|
||
contentLines.push('最近评论:')
|
||
myCommentsList.slice(0, 3).forEach((comment, index) => {
|
||
const shortContent = comment.content.length > 20
|
||
? comment.content.substring(0, 20) + '...'
|
||
: comment.content
|
||
contentLines.push(`${index + 1}. 在《${comment.postTitle}》中评论`)
|
||
contentLines.push(` "${shortContent}"`)
|
||
})
|
||
if (myCommentsList.length > 3) {
|
||
contentLines.push(` ...还有${myCommentsList.length - 3}条评论`)
|
||
}
|
||
} else {
|
||
contentLines.push('暂无评论记录')
|
||
}
|
||
|
||
contentLines.push('')
|
||
contentLines.push('持续学习,不断进步!')
|
||
|
||
wx.showModal({
|
||
title: '数据统计',
|
||
content: contentLines.join('\n'),
|
||
showCancel: false,
|
||
confirmText: '继续加油',
|
||
confirmColor: '#667eea'
|
||
})
|
||
},
|
||
|
||
// 退出登录
|
||
handleLogout() {
|
||
if (!userManager.isUserLogin()) {
|
||
wx.showToast({
|
||
title: '您还未登录',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
|
||
wx.showModal({
|
||
title: '退出登录',
|
||
content: '确定要退出登录吗?\n退出后部分功能将无法使用',
|
||
confirmText: '退出',
|
||
cancelText: '取消',
|
||
confirmColor: '#FF5252',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
// 清除登录状态
|
||
userManager.clearUserInfo()
|
||
|
||
// 更新页面数据
|
||
this.setData({
|
||
userInfo: userManager.getUserInfo()
|
||
})
|
||
|
||
wx.showToast({
|
||
title: '已退出登录',
|
||
icon: 'success',
|
||
duration: 2000
|
||
})
|
||
|
||
// 震动反馈
|
||
wx.vibrateShort({
|
||
type: 'light'
|
||
})
|
||
|
||
// 1秒后跳转到首页
|
||
setTimeout(() => {
|
||
wx.switchTab({
|
||
url: '/pages/index/index'
|
||
})
|
||
}, 1500)
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
// 阻止事件冒泡
|
||
doNothing() {
|
||
// 空函数,防止点击对话框内容时关闭对话框
|
||
}
|
||
})
|