ertonglianxibiao/pages/mine/mine.js

232 lines
5.5 KiB
JavaScript
Raw Normal View History

2026-01-14 15:33:15 +08:00
import { injectPage } from '@jdmini/api'
const { getUserInfo } = require('../../utils/api.js')
const defaultAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page(injectPage({})({
data: {
isLoggedIn: false,
userInfo: {},
defaultAvatar: defaultAvatar,
downloadCount: 0,
inviteCount: 0,
cacheSize: '0KB'
},
onShow() {
this.checkLoginStatus()
this.loadStats()
this.calculateCacheSize()
},
onShareAppMessage() {
const userId = wx.getStorageSync('userId')
let path = '/pages/index/index'
if (userId) {
path += `?inviter=${userId}`
}
return {
title: '儿童练习表 - 幼儿启蒙必备,快来一起学习吧!',
path: path
}
},
// 检查登录状态
checkLoginStatus() {
const userId = wx.getStorageSync('userId')
const userInfo = wx.getStorageSync('userInfo') || {}
if (userId) {
this.setData({
isLoggedIn: true,
userInfo: userInfo
})
// 刷新用户信息
this.refreshUserInfo(userId)
} else {
this.setData({
isLoggedIn: false,
userInfo: {}
})
}
},
// 刷新用户信息
async refreshUserInfo(userId) {
try {
const res = await getUserInfo(userId)
if (res.success) {
const userInfo = {
...this.data.userInfo,
nickname: res.data.nickname,
avatar: res.data.avatar,
points: res.data.points
}
this.setData({ userInfo })
wx.setStorageSync('userInfo', userInfo)
}
} catch (error) {
console.error('刷新用户信息失败:', error)
}
},
// 加载统计数据
loadStats() {
try {
const downloads = wx.getStorageSync('downloads') || []
// 邀请数暂时从本地存储获取,实际应该从服务端获取
const inviteCount = wx.getStorageSync('inviteCount') || 0
this.setData({
downloadCount: downloads.length,
inviteCount: inviteCount
})
} catch (error) {
console.error('加载统计失败:', error)
}
},
// 计算缓存大小
calculateCacheSize() {
try {
const res = wx.getStorageInfoSync()
const usedSize = res.currentSize // KB
let sizeStr = ''
if (usedSize < 1024) {
sizeStr = usedSize + 'KB'
} else {
sizeStr = (usedSize / 1024).toFixed(2) + 'MB'
}
this.setData({ cacheSize: sizeStr })
} catch (error) {
console.error('计算缓存大小失败:', error)
}
},
// 跳转登录
goLogin() {
wx.setStorageSync('returnTo', {
type: 'switchTab',
url: '/pages/mine/mine'
})
wx.navigateTo({
url: '/pages/login/login'
})
},
// 跳转下载页面
goDownloads() {
wx.switchTab({
url: '/pages/download/download'
})
},
// 跳转积分明细
goPointsLog() {
if (!this.data.isLoggedIn) {
this.goLogin()
return
}
wx.showToast({
title: '积分明细功能开发中',
icon: 'none'
})
},
// 清除缓存
clearCache() {
wx.showModal({
title: '提示',
content: '确定清除所有缓存吗?这将删除下载记录(不会退出登录)。',
success: (res) => {
if (res.confirm) {
try {
// 保存登录信息
const userId = wx.getStorageSync('userId')
const token = wx.getStorageSync('token')
const userInfo = wx.getStorageSync('userInfo')
// 清除下载的文件
const downloads = wx.getStorageSync('downloads') || []
const fs = wx.getFileSystemManager()
downloads.forEach(item => {
if (item.filePath) {
try {
fs.unlinkSync(item.filePath)
} catch (e) {
// 忽略文件不存在的错误
}
}
})
// 清除存储
wx.clearStorageSync()
// 恢复登录信息
if (userId) {
wx.setStorageSync('userId', userId)
wx.setStorageSync('token', token)
wx.setStorageSync('userInfo', userInfo)
}
this.setData({
downloadCount: 0,
inviteCount: 0,
cacheSize: '0KB'
})
wx.showToast({
title: '清除成功',
icon: 'success'
})
} catch (error) {
console.error('清除缓存失败:', error)
wx.showToast({
title: '清除失败',
icon: 'none'
})
}
}
}
})
},
// 退出登录
logout() {
wx.showModal({
title: '提示',
content: '确定退出登录吗?',
success: (res) => {
if (res.confirm) {
wx.removeStorageSync('userId')
wx.removeStorageSync('token')
wx.removeStorageSync('userInfo')
this.setData({
isLoggedIn: false,
userInfo: {}
})
wx.showToast({
title: '已退出登录',
icon: 'success'
})
}
}
})
},
// 关于我们
about() {
wx.showModal({
title: '关于儿童练习表',
content: '儿童练习表是一款专为幼儿设计的启蒙教育小程序,提供字母、数字、绘画、涂色等多种练习表,帮助孩子在快乐中学习成长。\n\n版本v1.0.0',
showCancel: false,
confirmText: '确定'
})
}
}))