123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- Page({
- data: {
- isLogin: false, // 登录状态
- userInfo: { // 用户信息
- nickName: '', // 用户昵称
- avatarUrl: '/assets/taddar/me.png', // 默认头像路径
- username: '', // 用户名
- password: '', // 密码
- },
- },
- // 页面加载时从缓存获取用户信息
- 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' // 登录页面的路径
- });
- }
- });
|