feat: 登陆和退出逻辑优化

This commit is contained in:
RubyLiu 2024-01-29 12:08:30 +08:00 committed by 刘瑞斌
parent 6b3a686c71
commit 0b07442daf
4 changed files with 17 additions and 8 deletions

View File

@ -15,7 +15,7 @@
import { GetPlatformIconUrl } from '@/api/requrls/setting/config';
// import GlobalSetting from '@/components/pure/global-setting/index.vue';
import useLocale from '@/locale/useLocale';
import { WHITE_LIST } from '@/router/constants';
import { NO_PROJECT_ROUTE_NAME, WHITE_LIST } from '@/router/constants';
import { useUserStore } from '@/store';
import useAppStore from '@/store/modules/app';
import useLicenseStore from '@/store/modules/setting/license';
@ -70,13 +70,13 @@
const checkIsLogin = async () => {
const isLogin = await userStore.isLogin();
const isLoginPage = route.name === 'login';
if (isLogin && appStore.currentProjectId) {
if (isLogin && appStore.currentProjectId && appStore.currentProjectId !== 'no_such_project') {
//
try {
const res = await getProjectInfo(appStore.currentProjectId);
if (res.deleted || !res.enable) {
//
router.push(WorkbenchRouteEnum.WORKBENCH);
router.push(NO_PROJECT_ROUTE_NAME);
return;
}
appStore.setCurrentMenuConfig(res.moduleIds);

View File

@ -2,16 +2,19 @@ import { useRouter } from 'vue-router';
import { Message } from '@arco-design/web-vue';
import { useI18n } from '@/hooks/useI18n';
import { useUserStore } from '@/store';
import { useAppStore, useUserStore } from '@/store';
export default function useUser() {
const router = useRouter();
const userStore = useUserStore();
const appStore = useAppStore();
const { t } = useI18n();
const logout = async (logoutTo?: string) => {
await userStore.logout();
const currentRoute = router.currentRoute.value;
// 清空顶部菜单
appStore.setTopMenus([]);
Message.success(t('message.logoutSuccess'));
router.push({
name: logoutTo && typeof logoutTo === 'string' ? logoutTo : 'login',

View File

@ -1,4 +1,4 @@
import { RouteLocationNormalized, RouteRecordNormalized, RouteRecordRaw, useRouter } from 'vue-router';
import { RouteLocationNormalized, RouteRecordNormalized, RouteRecordRaw } from 'vue-router';
import { includes } from 'lodash-es';
import { INDEX_ROUTE } from '@/router/routes/base';
@ -99,3 +99,9 @@ export function getFirstRouteNameByPermission(routerList: RouteRecordNormalized[
.find((item) => hasAnyPermission(item.meta.roles || []));
return currentRoute?.name || INDEX_ROUTE.name;
}
// 判断当前路由名有没有权限
export function routerNameHasPermission(routerName: string, routerList: RouteRecordNormalized[]) {
const currentRoute = routerList.find((item) => item.name === routerName);
return currentRoute ? hasAnyPermission(currentRoute.meta?.roles || []) : false;
}

View File

@ -63,10 +63,9 @@
import { useAppStore, useUserStore } from '@/store';
import { encrypted } from '@/utils';
import { setLoginExpires } from '@/utils/auth';
import { getFirstRouteNameByPermission } from '@/utils/permission';
import { getFirstRouteNameByPermission, routerNameHasPermission } from '@/utils/permission';
import type { LoginData } from '@/models/user';
import { WorkbenchRouteEnum } from '@/enums/routeEnum';
import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
@ -126,10 +125,11 @@
loginConfig.value.username = rememberPassword ? username : '';
loginConfig.value.password = rememberPassword ? password : '';
const { redirect, ...othersQuery } = router.currentRoute.value.query;
const redirectHasPermission = redirect && routerNameHasPermission(redirect as string, router.getRoutes());
const currentRouteName = getFirstRouteNameByPermission(router.getRoutes());
setLoginExpires();
router.push({
name: (redirect as string) || currentRouteName,
name: redirectHasPermission ? (redirect as string) : currentRouteName,
query: {
...othersQuery,
organizationId: appStore.currentOrgId,