// pages/login/index.js const { userApi } = require("../../utils/api"); const AuthUtil = require("../../utils/auth"); Page({ /** * 页面的初始数据 */ data: { username: "", password: "", loading: false, wechatLoading: false, // 微信登录加载状态 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 检查是否已登录 this.checkLoginStatus(); }, /** * 生命周期函数--监听页面显示 */ onShow() { // 每次页面显示时也检查登录状态 this.checkLoginStatus(); }, // 检查登录状态 checkLoginStatus() { // 获取全局应用实例 const app = getApp(); // 如果全局状态显示已登录,则自动跳转到首页 if (app.globalData.isLoggedIn && app.globalData.userInfo) { wx.switchTab({ url: "/pages/surgery/index" }); } else { // 检查是否有token if (AuthUtil.hasToken()) { this.autoLogin(); } } }, // 尝试自动登录 async autoLogin() { this.setData({ loading: true }); try { const isValid = await AuthUtil.validateUser(); if (isValid) { const userInfo = AuthUtil.getUserInfo(); // 立即更新全局状态 const app = getApp(); app.globalData.isLoggedIn = true; app.globalData.userInfo = userInfo; // 显示提示 wx.showToast({ title: "自动登录成功", icon: "success", duration: 1000 }); // 启动token自动刷新 AuthUtil.startTokenRefresh(); // 立即跳转到首页 // 先尝试隐藏 toast,避免影响跳转 wx.hideToast(); wx.switchTab({ url: "/pages/surgery/index", success: () => { }, fail: (err) => { // 如果 switchTab 失败,尝试使用 reLaunch wx.reLaunch({ url: "/pages/surgery/index", success: () => { }, fail: (reLaunchErr) => { } }); }, }); } else { // 自动登录失败,已清除登录状态 } } catch (err) { // 自动登录异常 } finally { this.setData({ loading: false }); } }, // 用户名输入变化 onUsernameChange(e) { this.setData({ username: e.detail.value, }); }, // 密码输入变化 onPasswordChange(e) { this.setData({ password: e.detail.value, }); }, // 跳转到设备状态页面 goToDeviceStatus() { wx.navigateTo({ url: '/pages/device_status/index', }); }, // 微信一键登录 handleWechatLogin() { this.setData({ wechatLoading: true }); // 调用微信登录 wx.login({ success: (res) => { if (res.code) { // 获取用户信息(可选) wx.getUserProfile({ desc: '用于完善用户资料', success: (userRes) => { // 调用后端API进行微信登录 this.performWechatLogin(res.code, userRes.userInfo.nickName); }, fail: () => { // 用户拒绝授权,使用code进行登录(不带昵称) this.performWechatLogin(res.code); } }); } else { wx.showToast({ title: '获取微信授权失败', icon: 'none' }); this.setData({ wechatLoading: false }); } }, fail: (err) => { wx.showToast({ title: '微信登录调用失败', icon: 'none' }); this.setData({ wechatLoading: false }); } }); }, // 执行微信登录 performWechatLogin(code, nickname = null) { userApi.miniLogin(code, nickname) .then((res) => { if (res.code === 1 || res.code === 200) { // 登录成功,保存token和用户信息 const { token, user } = res.data; AuthUtil.setToken(token); AuthUtil.setUserInfo(user); // 启动token自动刷新 AuthUtil.startTokenRefresh(); // 立即更新全局状态 const app = getApp(); app.globalData.isLoggedIn = true; app.globalData.userInfo = user; wx.showToast({ title: "微信登录成功", icon: "success", duration: 1000 }); // 立即跳转到首页 // 先尝试隐藏 toast,避免影响跳转 wx.hideToast(); wx.switchTab({ url: "/pages/surgery/index", success: () => { }, fail: (err) => { // 如果 switchTab 失败,尝试使用 reLaunch wx.reLaunch({ url: "/pages/surgery/index", success: () => { }, fail: (reLaunchErr) => { } }); }, }); } else { // 登录失败 wx.showToast({ title: res.msg || "微信登录失败", icon: "none", }); } }) .catch((err) => { wx.showToast({ title: err.message || "微信登录失败,请稍后重试", icon: "none", }); }) .finally(() => { this.setData({ wechatLoading: false }); }); }, // 处理登录 handleLogin() { const { username, password } = this.data; // 基本表单验证 if (!username.trim()) { wx.showToast({ title: "请输入用户名", icon: "none", }); return; } if (!password.trim()) { wx.showToast({ title: "请输入密码", icon: "none", }); return; } // 显示加载状态 this.setData({ loading: true }); // 调用登录API userApi .login(username, password) .then((res) => { // 检查登录是否成功(支持 code: 1 或 code: 200) if (res.code !== 1 && res.code !== 200) { wx.showToast({ title: res.msg || "用户名或密码错误", icon: "none", }); this.setData({ loading: false }); return; } // 检查用户状态(小程序允许状态>=1的用户登录) if (res.data.user.user_status < 1) { wx.showToast({ title: "您的账户已被禁用或需要等待管理员审核", icon: "none", }); this.setData({ loading: false }); return; } // 登录成功,保存token const { token, user } = res.data; AuthUtil.setToken(token); AuthUtil.setUserInfo(user); // 启动token自动刷新 AuthUtil.startTokenRefresh(); // 立即更新全局状态 const app = getApp(); app.globalData.isLoggedIn = true; app.globalData.userInfo = user; wx.showToast({ title: "登录成功", icon: "success", duration: 1000 }); // 登录成功后立即导航到首页 // 先尝试隐藏 toast,避免影响跳转 wx.hideToast(); wx.switchTab({ url: "/pages/surgery/index", success: () => { }, fail: (err) => { // 如果 switchTab 失败,尝试使用 reLaunch wx.reLaunch({ url: "/pages/surgery/index", success: () => { }, fail: (reLaunchErr) => { } }); }, }); }) .catch((err) => { wx.showToast({ title: err.message || "登录失败,请稍后重试", icon: "none", }); }) .finally(() => { this.setData({ loading: false }); }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, });