From 5deb6387c41b3c6b993cbc23638dea0947e7b767 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Mon, 2 Mar 2026 23:36:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=99=BB=E5=BD=95=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/main/home.vue | 160 +++++++++++++++++++++++++++++++++++++++- src/utils/request.ts | 16 ++++ 2 files changed, 174 insertions(+), 2 deletions(-) diff --git a/src/pages/main/home.vue b/src/pages/main/home.vue index 84af513..28de620 100644 --- a/src/pages/main/home.vue +++ b/src/pages/main/home.vue @@ -580,6 +580,10 @@ definePageConfig({ backgroundTextStyle: "dark", }); const store: any = useRootUserStore(); +// 防止重复检查登录状态 +let isCheckingLogin = false; +// 防止重复弹窗 +let isShowingLoginDialog = false; // 导航拦信息 const navBarHeight = ref(); const windowInfo = Taro.getWindowInfo(); @@ -711,7 +715,7 @@ Taro.usePullDownRefresh(() => { pond2(); } // 模式一滚动条高度 - m1_height.value = Taro.getSystemInfoSync().windowHeight - 200; + m1_height.value = Taro.getWindowInfo().windowHeight - 200; nextTick(() => { if (pondChildInfo.value) { if (alertShow.value) { @@ -725,6 +729,9 @@ Taro.usePullDownRefresh(() => { }); }); Taro.useDidShow(() => { + // 检查登录状态 + checkLoginStatus(); + startPolling(); // 默认 pond1(); @@ -760,7 +767,7 @@ Taro.useDidShow(() => { // }); } // 模式一滚动条高度 - m1_height.value = Taro.getSystemInfoSync().windowHeight - 200; + m1_height.value = Taro.getWindowInfo().windowHeight - 200; nextTick(() => { if (alertShow.value) { pondChildInfo.value.hideTour(alertShow.value); @@ -893,6 +900,155 @@ function stopPolling() { timer.value = null; } } + +// 检查登录状态 +function checkLoginStatus() { + // 防止重复检查 + if (isCheckingLogin) { + return; + } + + // 游客模式直接跳过 + if (store.getUnLogin == 1) { + return; + } + + const token = Taro.getStorageSync('Access-Token'); + const userId = Taro.getStorageSync('UserId'); + const loginType = Taro.getStorageSync('LoginType'); + + // 方式1:检查本地是否有token和userId + if (!token || !userId) { + isCheckingLogin = true; + // 如果之前是微信登录(LoginType=1),尝试引导重新授权 + if (loginType === '1') { + attemptAutoLogin(); + } else { + // 非微信登录,提示用户重新登录 + showLoginExpiredDialog(); + } + return; + } + + // 方式2:通过接口验证token是否有效 + // 这里调用一个轻量级接口,如果返回401,request.ts会统一拦截处理 + verifyTokenValid(); +} + +// 验证token是否有效 +function verifyTokenValid() { + // 使用塘口列表接口来验证token + getPond1().then((res: any) => { + console.log('getPond1返回结果:', res); + + if (res && res.code === 200) { + // token有效,正常继续 + console.log('登录状态有效'); + isCheckingLogin = false; + } else if (res && (res.code === 401 || res.statusCode === 401)) { + // token无效,主动弹窗提示 + console.log('检测到401,token已过期,弹出提示'); + isCheckingLogin = false; + showLoginExpiredDialog(); + } else if (!res || res.code !== 200) { + // 其他异常情况,也可能是token问题 + console.log('接口返回异常,code:', res?.code); + isCheckingLogin = false; + // 如果返回的不是200,可能也是登录问题 + if (res && res.msg && res.msg.includes('登录')) { + showLoginExpiredDialog(); + } + } else { + // 其他错误码 + isCheckingLogin = false; + } + }).catch((error) => { + // 接口调用失败 + console.log('登录状态检查失败,error:', error); + isCheckingLogin = false; + + // 如果是401错误,也弹出提示 + if (error && (error.code === 401 || error.statusCode === 401)) { + console.log('catch到401错误,弹出提示'); + showLoginExpiredDialog(); + } + }); +} + +// 显示登录过期提示 +function showLoginExpiredDialog() { + // 防止重复弹窗 + if (isShowingLoginDialog) { + console.log('登录过期弹窗已显示,跳过重复弹窗'); + return; + } + + isShowingLoginDialog = true; + console.log('显示登录过期弹窗'); + + Taro.showModal({ + title: '提示', + content: '登录信息已过期,请重新登录', + showCancel: false, + success: function (res) { + if (res.confirm) { + Taro.clearStorageSync(); + store.updateUnLogin(1); + Taro.redirectTo({ + url: '/pages/login/index', + }); + } + }, + complete: function() { + // 1秒后重置状态,允许再次弹窗 + setTimeout(() => { + isShowingLoginDialog = false; + isCheckingLogin = false; + }, 1000); + } + }); +} + +// 尝试自动登录 +function attemptAutoLogin() { + // 防止重复弹窗 + if (isShowingLoginDialog) { + return; + } + + isShowingLoginDialog = true; + console.log('显示重新授权弹窗'); + + // 由于后端需要手机号授权code,无法实现完全静默登录 + // 直接提示用户重新授权 + Taro.showModal({ + title: '登录已过期', + content: '需要重新授权获取手机号以继续使用', + confirmText: '去授权', + cancelText: '稍后再说', + success: function (res) { + if (res.confirm) { + // 跳转到登录页,让用户重新授权 + Taro.clearStorageSync(); + store.updateUnLogin(1); + Taro.redirectTo({ + url: '/pages/login/index', + }); + } else { + // 用户选择稍后,进入游客模式 + store.updateUnLogin(1); + pond1(); + } + }, + complete: function() { + // 1秒后重置状态 + setTimeout(() => { + isShowingLoginDialog = false; + isCheckingLogin = false; + }, 1000); + } + }); +} ///////////////////// function getPondInfo(e) { // Taro.nextTick(() => { diff --git a/src/utils/request.ts b/src/utils/request.ts index ab7680e..9b37a90 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -114,15 +114,23 @@ const request = async (method, url, params) => { success: function (res) { if (res.confirm) { Taro.clearStorageSync() + store.updateUnLogin(1); Taro.redirectTo({ url: "/pages/login/index", }); } else if (res.cancel) { Taro.clearStorageSync() + store.updateUnLogin(1); Taro.redirectTo({ url: "/pages/login/index", }); } + }, + complete: function() { + // 1秒后重置状态,防止永久锁定 + setTimeout(() => { + modalShown = false; + }, 1000); } }) } @@ -141,15 +149,23 @@ const request = async (method, url, params) => { success: function (res) { if (res.confirm) { Taro.clearStorageSync() + store.updateUnLogin(1); Taro.redirectTo({ url: "/pages/login/index", }); } else if (res.cancel) { Taro.clearStorageSync() + store.updateUnLogin(1); Taro.redirectTo({ url: "/pages/login/index", }); } + }, + complete: function() { + // 1秒后重置状态,防止永久锁定 + setTimeout(() => { + modalShown = false; + }, 1000); } }) }