feat(web): call getProfile in auth-guard, handle 401
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
65de9c6fce
commit
60abcddfdf
@ -1,9 +1,11 @@
|
||||
import Vue from 'vue';
|
||||
import VueRouter from 'vue-router';
|
||||
import { hasLogin } from '@/pages/index/api/login';
|
||||
import { getProfile } from '@/pages/index/api/mhzc-user';
|
||||
import routes from './routes';
|
||||
|
||||
import Main from '@/pages/index/views/main.vue'
|
||||
|
||||
let profileLoaded = false;
|
||||
// import Home2 from '@/pages/index/views/home2/index.vue'
|
||||
// import login from '@/pages/index/views/login/login.vue'
|
||||
// import yhzx from '@/pages/index/views/glxtSy/glxtSy.vue'
|
||||
@ -63,12 +65,41 @@ const router = new VueRouter({
|
||||
});
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
// 路由切换时清掉全局 figma 视觉高度 clamp 变量,
|
||||
// 避免旧页面残留的 --home-figma-visual-height 把新页面裁切掉。
|
||||
// 新页面的 portal-figma-scale-mixin 会在 mounted/activated 时基于真实高度重新写入。
|
||||
if (typeof document !== 'undefined') {
|
||||
const root = document.documentElement;
|
||||
root.style.removeProperty('--home-figma-visual-height');
|
||||
root.style.removeProperty('--portal-shell-min-design-height');
|
||||
root.style.removeProperty('--portal-market-shell-min-height');
|
||||
}
|
||||
// 检查是否需要登录且未登录
|
||||
if (to.meta.needLogin && !hasLogin()) {
|
||||
// 使用路由跳转至登录页面
|
||||
next('/login');
|
||||
return;
|
||||
}
|
||||
// 已登录时,懒加载门户用户信息。401 走清理 token + 跳登录
|
||||
if (hasLogin() && to.path !== '/login' && !profileLoaded) {
|
||||
getProfile()
|
||||
.then((res) => {
|
||||
if (Number(res?.code) === 401) {
|
||||
window.sessionStorage.removeItem('sfdl');
|
||||
profileLoaded = false;
|
||||
next('/login');
|
||||
return;
|
||||
}
|
||||
profileLoaded = true;
|
||||
next();
|
||||
})
|
||||
.catch(() => {
|
||||
// 网络异常不阻塞路由
|
||||
profileLoaded = true;
|
||||
next();
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 允许路由继续跳转
|
||||
next();
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user