// 用户中心页面逻辑 const { API, util, constants } = require('../../utils/api.js') Page({ data: { userInfo: null, userId: '', userStatus: 'online', statusText: '在线', taskStats: { total: 0, completed: 0, processing: 0 }, deviceInfo: {}, appVersion: '' }, onLoad() { const app = getApp() this.setData({ appVersion: app.globalData.version, deviceInfo: app.globalData.systemInfo }) this.loadUserData() }, onShow() { this.loadUserData() this.loadTaskStats() }, // 加载用户数据 loadUserData() { const userInfo = wx.getStorageSync('userInfo') const userId = wx.getStorageSync('userId') || 'anonymous' if (userInfo) { this.setData({ userInfo: userInfo, userId: userId }) } else { // 如果没有用户信息,尝试获取 this.getUserProfile() } }, // 获取用户资料 getUserProfile() { wx.getUserProfile({ desc: '用于完善用户资料', success: (res) => { const userInfo = res.userInfo wx.setStorageSync('userInfo', userInfo) this.setData({ userInfo: userInfo }) // 发送用户信息到服务器 this.sendUserInfoToServer(userInfo) }, fail: () => { // 使用默认信息 this.setData({ userInfo: { nickName: '访客用户', avatarUrl: '/assets/images/default-avatar.png' } }) } }) }, // 发送用户信息到服务器 async sendUserInfoToServer(userInfo) { try { await API.request('/user/info', 'POST', { userInfo: userInfo, deviceId: this.data.deviceInfo.model }) } catch (error) { console.error('发送用户信息失败:', error) } }, // 加载任务统计 async loadTaskStats() { try { const result = await API.getTaskList(1, 100) // 获取所有任务进行统计 if (result.success && result.data) { const tasks = result.data.tasks const stats = { total: tasks.length, completed: tasks.filter(task => task.status === 'completed').length, processing: tasks.filter(task => task.status === 'processing').length } this.setData({ taskStats: stats }) } } catch (error) { console.error('加载任务统计失败:', error) } }, // 显示任务历史 showTaskHistory() { wx.navigateTo({ url: '/pages/task-history/task-history' }) }, // 显示设置 showSettings() { wx.navigateTo({ url: '/pages/settings/settings' }) }, // 显示关于 showAbout() { wx.showModal({ title: '关于智控未来', content: `智控未来 v${this.data.appVersion}\n\n企业微信智能控制系统\n基于微信小程序原生技术栈开发\n\n功能特点:\n• 智能聊天对话\n• 任务管理处理\n• 多设备状态同步\n• WebSocket实时通信\n\n技术支持:\n• Rust后端服务\n• Embedded-Redis状态管理\n• HeedDB数据存储\n• LMStudio AI集成`, showCancel: false }) }, // 显示帮助 showHelp() { wx.showModal({ title: '使用帮助', content: `快速上手:\n\n1. 智能聊天\n • 在聊天页面输入问题\n • 支持文本、图片、文件消息\n • 实时接收AI回复\n\n2. 任务管理\n • 创建新任务并设置类型\n • 查看任务处理进度\n • 管理任务状态\n\n3. 用户中心\n • 查看个人信息\n • 查看任务统计\n • 系统设置\n\n常见问题:\n• 网络连接失败请检查网络\n• 任务长时间未响应可重试\n• 如有问题请联系技术支持`, showCancel: false }) }, // 刷新数据 async refreshData() { util.showLoading('刷新中...') try { // 重新加载用户数据 this.loadUserData() // 重新加载任务统计 await this.loadTaskStats() util.hideLoading() util.showSuccess('数据已刷新') } catch (error) { util.hideLoading() console.error('刷新数据失败:', error) util.showError('刷新失败') } }, // 清除缓存 async clearCache() { const confirmed = await util.showModal('确认清除', '确定要清除本地缓存吗?这将删除聊天记录和临时文件。') if (confirmed) { util.showLoading('清除中...') try { // 清除本地存储 wx.removeStorageSync('chatHistory') wx.removeStorageSync('taskHistory') // 清除临时文件 const fileSystemManager = wx.getFileSystemManager() try { fileSystemManager.readdir({ dirPath: `${wx.env.USER_DATA_PATH}/`, success: (res) => { res.files.forEach(file => { if (file.startsWith('tmp_')) { fileSystemManager.unlink({ filePath: `${wx.env.USER_DATA_PATH}/${file}` }) } }) } }) } catch (e) { console.log('清除临时文件失败:', e) } util.hideLoading() util.showSuccess('缓存已清除') } catch (error) { util.hideLoading() console.error('清除缓存失败:', error) util.showError('清除失败') } } }, // 退出登录 async logout() { const confirmed = await util.showModal('确认退出', '确定要退出登录吗?') if (confirmed) { util.showLoading('退出中...') try { // 通知服务器退出 await API.request('/user/logout', 'POST') // 清除本地数据 wx.removeStorageSync('token') wx.removeStorageSync('userInfo') wx.removeStorageSync('userId') wx.removeStorageSync('chatHistory') util.hideLoading() // 返回首页 wx.reLaunch({ url: '/pages/index/index' }) } catch (error) { util.hideLoading() console.error('退出登录失败:', error) util.showError('退出失败') } } }, // 分享 onShareAppMessage() { return { title: '智控未来 - 我的个人中心', path: '/pages/user/user', imageUrl: '/assets/images/share-user.png' } } })