Page({ data: { isLogin: false, // 登录状态 userInfo: { // 用户信息 nickName: '', // 用户昵称 }, }, // 页面加载时从缓存获取用户信息 onLoad() { this.loadUserInfo(); // 加载缓存中的用户信息 }, // 页面显示时确保用户信息刷新 onShow() { this.loadUserInfo(); // 每次进入页面时刷新用户信息 if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 3 // 设置当前tab为选中状态 }); } // 隐藏返回首页按钮 if (wx.canIUse('hideHomeButton')) { wx.hideHomeButton(); } }, // 加载缓存的用户信息 loadUserInfo() { // 获取缓存中的用户信息 const storedUserInfo = wx.getStorageSync('userInfo') || {}; // 获取缓存的 userInfo 对象 const storedAvatarUrl = storedUserInfo.avatarUrl || '/assets/taddar/me.png'; // 获取缓存的头像 const storedPassword = storedUserInfo.password || ''; // 获取缓存的密码 const storedUsername = storedUserInfo.username || ''; // 获取缓存的用户名 const storedUserRole = wx.getStorageSync('userRole') || ''; // 获取缓存的用户角色 // 合并缓存信息 const userInfo = { nickName: storedUserInfo.nickName || '', // 如果没有昵称,设置为空 avatarUrl: storedAvatarUrl, // 如果没有头像,使用默认头像 username: storedUsername, // 设置用户名 password: storedPassword, // 设置密码 }; // 设置页面数据 this.setData({ userInfo, userRole: storedUserRole, // 设置用户角色 }); // 调试信息,确认缓存是否加载成功 console.log('Loaded userInfo from cache:', userInfo); console.log('Loaded avatarUrl from cache:', storedAvatarUrl); console.log('Loaded password from cache:', storedPassword); console.log('Loaded username from cache:', storedUsername); console.log('Loaded userRole from cache:', storedUserRole); }, // 修改昵称 onNicknameChange(e) { this.setData({ 'userInfo.nickName': e.detail.value, // 更新昵称 }); }, // 修改用户名 onUsernameChange(e) { this.setData({ 'userInfo.username': e.detail.value, // 更新用户名 }); }, // 修改密码 onPasswordChange(e) { this.setData({ 'userInfo.password': e.detail.value, // 更新密码 }); }, // 选择头像 chooseAvatar() { wx.chooseMedia({ count: 1, // 选择1个文件 sizeType: ['original', 'compressed'], // 原图或压缩图 sourceType: ['album', 'camera'], // 相册或拍照 success: (res) => { const avatarUrl = res.tempFiles[0].tempFilePath; this.setData({ 'userInfo.avatarUrl': avatarUrl, // 更新头像 }); // 更新缓存中的头像信息 let userInfo = wx.getStorageSync('userInfo') || {}; userInfo.avatarUrl = avatarUrl; wx.setStorageSync('userInfo', userInfo); }, fail: () => { wx.showToast({ title: '头像选择失败', icon: 'none', }); }, }); }, // 提交表单 submitForm() { const { userInfo } = this.data; // 打印 userInfo 查看提交的数据 console.log('Submitting userInfo:', userInfo); // 检查昵称和用户名是否同时为空 if (!userInfo.nickName.trim() && !userInfo.username.trim()) { wx.showToast({ title: '昵称和用户名至少填写一个', icon: 'none', }); return; } // 检查昵称和用户名是否同时填写 if (userInfo.nickName.trim() && userInfo.username.trim()) { wx.showToast({ title: '只能填写一个:昵称或用户名', icon: 'none', }); return; } // 检查密码是否为空 if (!userInfo.password || !userInfo.password.trim()) { wx.showToast({ title: '密码不能为空', icon: 'none', }); return; } // 保存用户信息到缓存 wx.setStorageSync('userInfo', userInfo); // 更新整个 userInfo 对象 wx.setStorageSync('avatarUrl', userInfo.avatarUrl); // 更新头像 wx.setStorageSync('username', userInfo.username); // 更新用户名 wx.setStorageSync('password', userInfo.password); // 更新密码 wx.setStorageSync('userRole', this.data.userRole); // 保存用户角色 // 重新加载缓存并更新页面 this.loadUserInfo(); // 显示保存成功提示 wx.showToast({ title: '保存成功', icon: 'success', }); // 返回上一页 wx.navigateBack(); }, // 获取显示的用户名(优先显示昵称,如果没有则显示用户名) getDisplayName() { const { nickName, username } = this.data.userInfo; return nickName.trim() || username.trim() || '未设置'; // 如果昵称为空,则显示用户名,否则显示 '未设置' }, // 显示登录弹窗 goLogin() { this.setData({ isHidden: false }); }, // 编辑个人资料 EditProfile() { wx.navigateTo({ url: '/shoping/EditProfile/EditProfile' }); }, // 取消登录弹窗 potNo() { this.setData({ isHidden: true }); }, // 确认登录弹窗 popYes() { const { avatarUrl, nickName, username, password } = this.data.userInfo; if (!avatarUrl || !nickName || !username || !password) { wx.showToast({ icon: 'error', title: '请填写头像、昵称、用户名和密码', }); return; } // 保存头像、昵称、用户名和密码到缓存 wx.setStorageSync('userInfo', this.data.userInfo); this.setData({ isLogin: true, // 设置登录状态为 true isHidden: true, // 隐藏弹窗 }); }, // 跳转到阈值页面 goToThreshold() { wx.navigateTo({ url: '/pages/threshold/threshold' }); }, // 点击退出登录 tuichu() { // 清除缓存 wx.clearStorageSync(); // 重置登录状态 this.setData({ isLogin: false, userInfo: {} }); // 跳转到登录页面 wx.reLaunch({ url: '/pages/admin/admin' // 登录页面的路径 }); } });