From 984e0de9a952e383a2f655a5dc89273fd6c7c210 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 9 Jul 2024 21:10:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E7=99=BB=E9=99=86=E8=B0=83=E8=AF=95=E9=A6=96?= =?UTF-8?q?=E6=AC=A1=E8=B7=B3=E8=BD=AC=E8=B0=83=E8=AF=95=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/App.vue | 7 +++ frontend/src/store/modules/user/index.ts | 58 ++++--------------- frontend/src/utils/index.ts | 21 +++++++ .../src/views/login/components/login-form.vue | 36 +----------- 4 files changed, 39 insertions(+), 83 deletions(-) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 3f428f64cf..e8a0a39932 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -19,6 +19,8 @@ import { useUserStore } from '@/store'; import useAppStore from '@/store/modules/app'; import useLicenseStore from '@/store/modules/setting/license'; + import { getQueryVariable } from '@/utils'; + import { setToken } from '@/utils/auth'; import { getLocalStorage, setLocalStorage } from '@/utils/local-storage'; import { setFavicon, watchStyle, watchTheme } from '@/utils/theme'; @@ -77,6 +79,11 @@ onBeforeMount(async () => { await getPublicKey(); if (WHITE_LIST.find((el) => el.path === window.location.hash.split('#')[1]) === undefined) { + const TOKEN = getQueryVariable('_token'); + const CSRF = getQueryVariable('_csrf'); + if (TOKEN !== null && TOKEN !== undefined && CSRF !== null && CSRF !== undefined) { + setToken(window.atob(TOKEN), CSRF); + } await userStore.checkIsLogin(); } const { height } = useWindowSize(); diff --git a/frontend/src/store/modules/user/index.ts b/frontend/src/store/modules/user/index.ts index 33f405226e..096739e066 100644 --- a/frontend/src/store/modules/user/index.ts +++ b/frontend/src/store/modules/user/index.ts @@ -14,14 +14,13 @@ import { useI18n } from '@/hooks/useI18n'; import useUser from '@/hooks/useUser'; import { NO_PROJECT_ROUTE_NAME } from '@/router/constants'; import useLicenseStore from '@/store/modules/setting/license'; -import { getHashParameters } from '@/utils'; +import { getHashParameters, getQueryVariable } from '@/utils'; import { clearToken, setToken } from '@/utils/auth'; import { composePermissions, getFirstRouteNameByPermission } from '@/utils/permission'; import { removeRouteListener } from '@/utils/route-listener'; import type { LoginData } from '@/models/user'; import { LoginRes } from '@/models/user'; -import { ProjectManagementRouteEnum } from '@/enums/routeEnum'; import useAppStore from '../app'; import { UserState } from './types'; @@ -186,7 +185,13 @@ const useUserStore = defineStore('user', { const appStore = useAppStore(); setToken(res.sessionId, res.csrfToken); this.setInfo(res); - const { orgId, pId } = getHashParameters(); + let { orgId, pId } = getHashParameters(); + if (!pId) { + pId = getQueryVariable('_pId') || ''; + } + if (!orgId) { + orgId = getQueryVariable('_orgId') || ''; + } // 1. forceSet是强制设置,需要设置res的,2.非force且地址栏有,则也设置 3.地址栏参数为空就不设置 // 如果访问页面的时候携带了组织 ID和项目 ID,则不设置 if (!forceSet && orgId) { @@ -207,47 +212,6 @@ const useUserStore = defineStore('user', { } }, - async setUserInfoByAuth(pId: string, orgId: string) { - const appStore = useAppStore(); - const router = useRouter(); - const res = await userIsLogin(); - this.setInfo(res); - appStore.setCurrentOrgId(orgId); - appStore.setCurrentProjectId(pId); - try { - const HasProjectPermission = await getUserHasProjectPermission(appStore.currentProjectId); - if (!HasProjectPermission) { - // 没有项目权限(用户所在的当前项目被禁用&用户被移除出去该项目) - router.push({ - name: NO_PROJECT_ROUTE_NAME, - }); - return; - } - const resp = await getProjectInfo(appStore.currentProjectId); - if (!resp) { - // 如果项目被删除或者被禁用,跳转到无项目页面 - router.push({ - name: NO_PROJECT_ROUTE_NAME, - }); - } - if (resp) { - appStore.setCurrentMenuConfig(resp?.moduleIds || []); - } - } catch (err) { - appStore.setCurrentMenuConfig([]); - // eslint-disable-next-line no-console - console.log(err); - } - const { isLoginPage } = useUser(); - if (isLoginPage()) { - // 当前页面为登录页面,且已经登录,跳转到首页 - const currentRouteName = router.getRoutes() - ? getFirstRouteNameByPermission(router.getRoutes()) - : ProjectManagementRouteEnum.PROJECT_MANAGEMENT_PERMISSION_BASIC_INFO; - router.push({ name: currentRouteName }); - } - }, - // 更新本地设置 updateLocalConfig(partial: Partial) { this.$patch(partial); @@ -274,8 +238,8 @@ const useUserStore = defineStore('user', { } }, async checkIsLogin(forceSet = false) { - const { isLoginPage } = useUser(); const router = useRouter(); + const { isLoginPage } = useUser(); const appStore = useAppStore(); const isLogin = await this.isLogin(forceSet); if (isLogin && appStore.currentProjectId !== 'no_such_project') { @@ -308,9 +272,7 @@ const useUserStore = defineStore('user', { } if (isLoginPage() && isLogin) { // 当前页面为登录页面,且已经登录,跳转到首页 - const currentRouteName = router.getRoutes() - ? getFirstRouteNameByPermission(router.getRoutes()) - : ProjectManagementRouteEnum.PROJECT_MANAGEMENT_PERMISSION_BASIC_INFO; + const currentRouteName = getFirstRouteNameByPermission(router.getRoutes()); router.push({ name: currentRouteName }); } }, diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 17d039f998..79d13b17c4 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -675,6 +675,27 @@ export const getHashParameters = (): Record => { return params; }; +export function getQueryVariable(variable: string) { + const urlString = window.location.href; + const queryIndex = urlString.indexOf('?'); + if (queryIndex !== -1) { + const query = urlString.substring(queryIndex + 1); + + // 分割查询参数 + const params = query.split('&'); + // 遍历参数,找到 _token 参数的值 + let variableValue; + params.forEach((param) => { + const equalIndex = param.indexOf('='); + const variableName = param.substring(0, equalIndex); + if (variableName === variable) { + variableValue = param.substring(equalIndex + 1); + } + }); + return variableValue; + } +} + let lastTimestamp = 0; let sequence = 0; diff --git a/frontend/src/views/login/components/login-form.vue b/frontend/src/views/login/components/login-form.vue index eae6e9da31..82871e8335 100644 --- a/frontend/src/views/login/components/login-form.vue +++ b/frontend/src/views/login/components/login-form.vue @@ -132,7 +132,7 @@ import { useAppStore, useUserStore } from '@/store'; import useLicenseStore from '@/store/modules/setting/license'; import { encrypted } from '@/utils'; - import { setLoginExpires, setToken } from '@/utils/auth'; + import { setLoginExpires } from '@/utils/auth'; import { getFirstRouteNameByPermission, routerNameHasPermission } from '@/utils/permission'; import type { LoginData } from '@/models/user'; @@ -359,43 +359,9 @@ }); } - function getQueryVariable(variable: string) { - const urlString = window.location.href; - - const queryIndex = urlString.indexOf('?'); - if (queryIndex !== -1) { - const query = urlString.substring(queryIndex + 1); - - // 分割查询参数 - const params = query.split('&'); - // 遍历参数,找到 _token 参数的值 - let variableValue; - params.forEach((param) => { - const equalIndex = param.indexOf('='); - const variableName = param.substring(0, equalIndex); - if (variableName === variable) { - variableValue = param.substring(equalIndex + 1); - } - }); - return variableValue; - } - } - - async function checkAuthUrlParam() { - const TOKEN = getQueryVariable('_token'); - const CSRF = getQueryVariable('_csrf'); - const pId = getQueryVariable('_pId'); - const orgId = getQueryVariable('_orgId'); - if (TOKEN !== null && TOKEN !== undefined && CSRF !== null && CSRF !== undefined) { - setToken(window.atob(TOKEN), CSRF); - await userStore.setUserInfoByAuth(pId || '', orgId || ''); - } - } - onMounted(() => { userStore.getAuthentication(); initPlatformInfo(); - checkAuthUrlParam(); });