Page({ data: { selected: 3, // 底部导航栏选中的 tab name: '', // 用户名 password: '', // 密码 errorMessage: '' // 错误信息 }, // 输入用户名 inputUsername: function (e) { this.setData({ name: e.detail.value }); }, // 输入密码 inputPassword: function (e) { this.setData({ password: e.detail.value }); }, // 登录逻辑 login: function () { const { name, password } = this.data; // 检查用户名和密码是否为空 if (!name || !password) { wx.showToast({ title: '用户名和密码不能为空', icon: 'none', duration: 2000 }); return; } wx.showLoading({ title: '登录中...' }); setTimeout(() => { // 发送请求到后端验证用户名和密码 wx.request({ url: 'https://soilgd.com:5000/login', // 后端登录接口 method: 'POST', data: { name: name, password: password }, success: (res) => { wx.hideLoading(); if (res.data.success) { // 登录成功后,将当前用户名和 ID 缓存到本地 wx.setStorageSync('currentUser', name); // 将当前登录用户名缓存 wx.setStorageSync('userId', res.data.userId); // 将用户 ID 缓存 wx.switchTab({ url: '/pages/threshold/threshold' }); } else { this.setData({ errorMessage: res.data.message || '用户名或密码错误' }); wx.showToast({ title: res.data.message || '用户名或密码错误', icon: 'none', duration: 2000 }); } }, fail: (err) => { wx.hideLoading(); wx.showToast({ title: '网络错误,请重试', icon: 'none', duration: 2000 }); console.error('登录失败:', err); } }); }, 1500); }, // 隐藏返回首页按钮 onShow: function() { if (wx.canIUse('hideHomeButton')) { wx.hideHomeButton(); } } });