Page({ data: { selected: 3, // 底部导航栏选中的 tab username: '', // 用户名 password: '', // 密码 errorMessage: '' // 错误信息 }, // 输入用户名 inputUsername: function (e) { this.setData({ username: e.detail.value }); }, // 输入密码 inputPassword: function (e) { this.setData({ password: e.detail.value }); }, // 登录逻辑 login: function () { const { username, password } = this.data; // 从本地存储获取有效的用户名和密码 const validUsers = wx.getStorageSync('validUsers') || { 'admin': '123456', '123': '123' }; // 检查用户名和密码是否为空 if (!username || !password) { wx.showToast({ title: '用户名和密码不能为空', icon: 'none', duration: 2000 }); return; } wx.showLoading({ title: '登录中...' }); setTimeout(() => { // 检查用户名和密码是否匹配 if (validUsers[username] === password) { wx.hideLoading(); // 登录成功后,将当前用户名和密码缓存到本地 const updatedUsers = { ...validUsers, [username]: password }; // 仅更新当前登录的用户名 wx.setStorageSync('validUsers', updatedUsers); // 更新本地存储 wx.switchTab({ // 跳转到 tabBar 页面 url: '/pages/threshold/threshold' }); } else { wx.hideLoading(); this.setData({ errorMessage: '用户名或密码错误' }); wx.showToast({ title: '用户名或密码错误', icon: 'none', duration: 2000 }); } }, 1500); }, // 隐藏返回首页按钮 onShow: function() { if (wx.canIUse('hideHomeButton')) { wx.hideHomeButton(); } } });