|
@@ -1,4 +1,4 @@
|
|
|
-import type { Router, RouteRecordRaw } from 'vue-router';
|
|
|
|
|
|
|
+import type { Router, RouteRecordRaw, RouteLocationNormalized } from 'vue-router';
|
|
|
|
|
|
|
|
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
|
|
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
|
|
|
|
|
|
|
@@ -14,10 +14,11 @@ import { OAUTH2_THIRD_LOGIN_TENANT_ID } from '/@/enums/cacheEnum';
|
|
|
|
|
|
|
|
import { useGlobSetting } from '/@/hooks/setting';
|
|
import { useGlobSetting } from '/@/hooks/setting';
|
|
|
|
|
|
|
|
-import { getSavedHomeRoute } from '/@/utils/homeStyle';
|
|
|
|
|
|
|
+import { getSavedHomeRoute, resolveRootRedirect } from '/@/utils/homeStyle';
|
|
|
|
|
|
|
|
import { isEmpty, assign } from 'lodash-es';
|
|
import { isEmpty, assign } from 'lodash-es';
|
|
|
import { MOCK_LOGIN_URL_QUERY, SKIP_SSO_URL_QUERY } from '../constant';
|
|
import { MOCK_LOGIN_URL_QUERY, SKIP_SSO_URL_QUERY } from '../constant';
|
|
|
|
|
+import { MOCK_LOGIN_UESRNAME } from '/@/store/constant';
|
|
|
import { useSso } from '/@/hooks/web/useSso';
|
|
import { useSso } from '/@/hooks/web/useSso';
|
|
|
import { useAutoLogin } from '/@/hooks/vent/useAutoLogin';
|
|
import { useAutoLogin } from '/@/hooks/vent/useAutoLogin';
|
|
|
import { addBrowseLog } from '@/router/helper/menuHelper';
|
|
import { addBrowseLog } from '@/router/helper/menuHelper';
|
|
@@ -41,12 +42,28 @@ const whitePathList: PageEnum[] = [LOGIN_PATH, OAUTH2_LOGIN_PAGE_PATH, SYS_FILES
|
|
|
//update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
|
|
//update-end---author:wangshuai ---date:20220629 for:[issues/I5BG1I]vue3不支持auth2登录------------
|
|
|
const glob = useGlobSetting();
|
|
const glob = useGlobSetting();
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 本次导航是否要求免密(mock)登录。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 除了看路由 query,还要看地址栏原始地址:vue-router 会在所有导航守卫之前解析路由记录上的 redirect
|
|
|
|
|
+ * (详见 utils/homeStyle.ts 的 resolveRootRedirect),某些情况下入口地址的 query 到不了 `to.query`,
|
|
|
|
|
+ * 只看 query 会让 `/?mock-login=1` 这类根路径入口被漏掉。
|
|
|
|
|
+ */
|
|
|
|
|
+function isMockLoginRequest(to: RouteLocationNormalized): boolean {
|
|
|
|
|
+ if (to.query[MOCK_LOGIN_URL_QUERY.key] === MOCK_LOGIN_URL_QUERY.val) return true;
|
|
|
|
|
+ try {
|
|
|
|
|
+ return new URLSearchParams(window.location.search).get(MOCK_LOGIN_URL_QUERY.key) === MOCK_LOGIN_URL_QUERY.val;
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function createPermissionGuard(router: Router) {
|
|
export function createPermissionGuard(router: Router) {
|
|
|
const userStore = useUserStoreWithOut();
|
|
const userStore = useUserStoreWithOut();
|
|
|
const permissionStore = usePermissionStoreWithOut();
|
|
const permissionStore = usePermissionStoreWithOut();
|
|
|
const { doAutoLogin, doTokenLogin, validateRoute, tokenValidateRoute } = useAutoLogin();
|
|
const { doAutoLogin, doTokenLogin, validateRoute, tokenValidateRoute } = useAutoLogin();
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
- RootRoute.redirect = getSavedHomeRoute() || glob.homePath || PageEnum.BASE_HOME;
|
|
|
|
|
|
|
+ RootRoute.redirect = (rootTo) => resolveRootRedirect(getSavedHomeRoute() || glob.homePath || PageEnum.BASE_HOME, rootTo.query);
|
|
|
if (to.query['isNoReverse'] != '1') {
|
|
if (to.query['isNoReverse'] != '1') {
|
|
|
if (VENT_PARAM['is2DModel']) {
|
|
if (VENT_PARAM['is2DModel']) {
|
|
|
for (const key in to) {
|
|
for (const key in to) {
|
|
@@ -91,10 +108,22 @@ export function createPermissionGuard(router: Router) {
|
|
|
return next({ path: to.fullPath, replace: true, query: to.query });
|
|
return next({ path: to.fullPath, replace: true, query: to.query });
|
|
|
}
|
|
}
|
|
|
// 如果指定了需要模拟登录则执行模拟登录,覆盖原有的登录信息
|
|
// 如果指定了需要模拟登录则执行模拟登录,覆盖原有的登录信息
|
|
|
- if (to.query[MOCK_LOGIN_URL_QUERY.key] === MOCK_LOGIN_URL_QUERY.val) {
|
|
|
|
|
- await userStore.mockLogin({ goHome: false });
|
|
|
|
|
|
|
+ // 判断同时看 to.query 与地址栏原始地址(见 isMockLoginRequest 说明)
|
|
|
|
|
+ if (isMockLoginRequest(to)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 凭据缺失时直接判失败:避免发一次注定被拒的空用户名/密码请求
|
|
|
|
|
+ if (!MOCK_LOGIN_UESRNAME) throw new Error('mock 登录凭据未注入(VITE_MOCK_LOGIN_USERNAME_CHAR_CODE)');
|
|
|
|
|
+ await userStore.mockLogin({ goHome: false });
|
|
|
|
|
+ // 登录接口 200 但没带回 token 时 afterLoginAction 会静默返回,这里显式判失败
|
|
|
|
|
+ if (!userStore.getToken) throw new Error('mock 登录未取得 token');
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ // 失败不能让守卫抛错:守卫 await 抛错会中断导航,main.ts 的 router.isReady() 一直不 resolve → 白屏
|
|
|
|
|
+ console.error('[mock-login] 免密登录失败,转正常登录流程', error);
|
|
|
|
|
+ return next({ path: LOGIN_PATH, replace: true, query: { redirect: to.fullPath } });
|
|
|
|
|
+ }
|
|
|
delete to.query[MOCK_LOGIN_URL_QUERY.key];
|
|
delete to.query[MOCK_LOGIN_URL_QUERY.key];
|
|
|
- return next({ path: to.fullPath, replace: true, query: to.query });
|
|
|
|
|
|
|
+ // 用 path + 清洗后的 query 跳转:to.fullPath 里还留着 mock-login,交给 query 覆盖容易看漏
|
|
|
|
|
+ return next({ path: to.path, hash: to.hash, replace: true, query: to.query });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const token = userStore.getToken;
|
|
const token = userStore.getToken;
|