fix: 登录问题修复。
This commit is contained in:
@@ -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(() => {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user