fix: 修复普通用户无法正常读取用户信息的问题
This commit is contained in:
parent
28af2df093
commit
15c6eefe30
|
@ -210,15 +210,22 @@ export class AuthManager {
|
|||
await client.testAdminAuth();
|
||||
return true;
|
||||
} catch (error) {
|
||||
// 如果是401错误,说明token有效但不是管理员,不需要清除token
|
||||
// 如果是其他错误,可能token无效,清除token
|
||||
// 只有在token完全无效的情况下才清除token
|
||||
// 401错误表示token有效但权限不足,不应清除token
|
||||
if (
|
||||
error &&
|
||||
typeof error === "object" &&
|
||||
"status" in error &&
|
||||
error.status !== 401
|
||||
"status" in error
|
||||
) {
|
||||
// 如果是403 (Forbidden) 或401 (Unauthorized),说明token有效但权限不足
|
||||
if (error.status === 401 || error.status === 403) {
|
||||
return false;
|
||||
}
|
||||
// 其他状态码可能表示token无效,清除token
|
||||
AuthManager.clearToken();
|
||||
} else {
|
||||
// 网络错误等,不清除token
|
||||
console.error('管理员权限验证失败:', error);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,10 @@
|
|||
<div v-if="activePage === 1">
|
||||
<UserInfo />
|
||||
</div>
|
||||
<div v-else-if="activePage === 2">
|
||||
<!-- 添加对应的组件或内容 -->
|
||||
<div>Item 2 内容</div>
|
||||
</div>
|
||||
<div v-else-if="activePage === 100">
|
||||
<BoardTable />
|
||||
</div>
|
||||
|
@ -37,11 +41,37 @@ const isAdmin = ref(false);
|
|||
|
||||
function setActivePage(event: Event) {
|
||||
const target = event.currentTarget as HTMLLinkElement;
|
||||
activePage.value = toNumber(target.id);
|
||||
const newPage = toNumber(target.id);
|
||||
|
||||
// 如果用户不是管理员但试图访问管理员页面,则忽略
|
||||
if (newPage === 100 && !isAdmin.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
activePage.value = newPage;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
// 首先验证用户是否已登录
|
||||
const isAuthenticated = await AuthManager.isAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
// 如果未登录,重定向到登录页面
|
||||
// 这里可以使用路由跳转
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证管理员权限
|
||||
isAdmin.value = await AuthManager.verifyAdminAuth();
|
||||
|
||||
// 如果当前页面是管理员页面但用户不是管理员,切换到用户信息页面
|
||||
if (activePage.value === 100 && !isAdmin.value) {
|
||||
activePage.value = 1;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('用户认证检查失败:', error);
|
||||
// 可以在这里处理错误,比如显示错误信息或重定向到登录页面
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
|
|
Loading…
Reference in New Issue