Browse Source

区分用户端与后端改造

hetong_dev
zhouhaibin 2 weeks ago
parent
commit
54ca63710f
  1. 11
      src/enums/pageEnum.ts
  2. 38
      src/router/guard/permissionGuard.ts
  3. 12
      src/router/routes/basic.ts
  4. 4
      src/router/routes/modules/dashboard.ts
  5. 40
      src/router/routes/modules/local.ts
  6. 7
      src/store/modules/user.ts

11
src/enums/pageEnum.ts

@ -1,13 +1,14 @@
export enum PageEnum {
// basic login path
BASE_LOGIN = '/login',
BASE_LOGIN = '/admin/login',
USER_LOGIN = '/user/login',
// 默认/dashboard 在登录时会带上redirect 然后死循环 原因未知 暂时写死地址
BASE_HOME = '/workbench',
BASE_HOME = '/admin/workbench',
// error page path
ERROR_PAGE = '/exception',
ERROR_PAGE = '/admin/exception',
// error log page path
ERROR_LOG_PAGE = '/error-log/list',
ERROR_LOG_PAGE = '/admin/error-log/list',
//oauth登录
SOCIAL_LOGIN = '/social-callback',
SOCIAL_LOGIN = '/admin/social-callback',
}
export const PageWrapperFixedHeightKey = 'PageWrapperFixedHeight';

38
src/router/guard/permissionGuard.ts

@ -6,12 +6,13 @@ import { PAGE_NOT_FOUND_ROUTE } from '@/router/routes/basic';
const LOGIN_PATH = PageEnum.BASE_LOGIN;
const SOCIAL_LOGIN = PageEnum.SOCIAL_LOGIN;
const USER_LOGIN_PATH = '/user/login';
/**
* 访
* oauth页
* oauth页
*/
const whitePathList: PageEnum[] = [LOGIN_PATH, SOCIAL_LOGIN];
const whitePathList: string[] = [LOGIN_PATH, SOCIAL_LOGIN, USER_LOGIN_PATH, '/'];
export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut();
@ -22,12 +23,14 @@ export function createPermissionGuard(router: Router) {
// Whitelist can be directly entered
if (whitePathList.includes(to.path as PageEnum)) {
if (to.path === LOGIN_PATH && token) {
if ((to.path === LOGIN_PATH || to.path === USER_LOGIN_PATH) && token) {
const isSessionTimeout = userStore.getSessionTimeout;
try {
await userStore.afterLoginAction();
if (!isSessionTimeout) {
next(decodeURIComponent((to.query?.redirect as string) || '/'));
// 根据登录页面类型决定默认跳转路径
const defaultRedirect = to.path === USER_LOGIN_PATH ? '/user/home' : '/admin/workbench';
next(decodeURIComponent((to.query?.redirect as string) || defaultRedirect));
return;
}
} catch {
@ -45,9 +48,13 @@ export function createPermissionGuard(router: Router) {
return;
}
// 根据访问路径决定跳转到哪个登录页面
const isUserRoute = to.path.startsWith('/user/');
const loginPath = isUserRoute ? USER_LOGIN_PATH : LOGIN_PATH;
// redirect login page
const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
path: LOGIN_PATH,
path: loginPath,
replace: true,
};
if (to.path) {
@ -94,20 +101,25 @@ export function createPermissionGuard(router: Router) {
// 遇到不存在页面,后续逻辑不再处理redirect(阻止下面else逻辑)
from.query.redirect = '';
if (from.path === LOGIN_PATH && to.fullPath !== PageEnum.BASE_HOME) {
// 登陆重定向不存在路由,转去“首页”
next({ path: PageEnum.BASE_HOME, replace: true });
} else {
// 正常前往“404”页面
next();
if (from.path === LOGIN_PATH || from.path === USER_LOGIN_PATH) {
// 根据登录页面类型决定重定向路径
const defaultHome = from.path === USER_LOGIN_PATH ? '/user/home' : PageEnum.BASE_HOME;
if (to.fullPath !== defaultHome) {
// 登陆重定向不存在路由,转去对应的"首页"
next({ path: defaultHome, replace: true });
return;
}
}
// 正常前往"404"页面
next();
} else if (from.query.redirect) {
// 存在redirect
let redirect = decodeURIComponent((from.query.redirect as string) || '');
// redirect为%2F(/)时 如果包含菜单 会跳转到空白页&不会显示任何内容
if (redirect === '/') {
// 为/时 跳转到首页 需要手动处理
redirect = PageEnum.BASE_HOME;
// 为/时 根据当前路径判断跳转到哪个首页
const isUserRoute = from.path.startsWith('/user/');
redirect = isUserRoute ? '/user/home' : PageEnum.BASE_HOME;
}
// 只处理一次 from.query.redirect

12
src/router/routes/basic.ts

@ -4,7 +4,7 @@ import { REDIRECT_NAME, LAYOUT, EXCEPTION_COMPONENT, PAGE_NOT_FOUND_NAME } from
// 404 on a page
export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
path: '/:path(.*)*',
path: '/admin/:path(.*)*',
name: PAGE_NOT_FOUND_NAME,
component: LAYOUT,
meta: {
@ -28,7 +28,7 @@ export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
};
export const REDIRECT_ROUTE: AppRouteRecordRaw = {
path: '/redirect',
path: '/admin/redirect',
component: LAYOUT,
name: 'RedirectTo',
meta: {
@ -38,7 +38,7 @@ export const REDIRECT_ROUTE: AppRouteRecordRaw = {
},
children: [
{
path: '/redirect/:path(.*)/:_redirect_type(.*)/:_origin_params(.*)?',
path: '/admin/redirect/:path(.*)/:_redirect_type(.*)/:_origin_params(.*)?',
name: REDIRECT_NAME,
component: () => import('@/layouts/components/redirect/index.vue'),
meta: {
@ -50,10 +50,10 @@ export const REDIRECT_ROUTE: AppRouteRecordRaw = {
};
export const ERROR_LOG_ROUTE: AppRouteRecordRaw = {
path: '/error-log',
path: '/admin/error-log',
name: 'ErrorLog',
component: LAYOUT,
redirect: '/error-log/list',
redirect: '/admin/error-log/list',
meta: {
title: 'ErrorLog',
hideBreadcrumb: true,
@ -67,7 +67,7 @@ export const ERROR_LOG_ROUTE: AppRouteRecordRaw = {
meta: {
title: t('routes.basic.errorLogList'),
hideBreadcrumb: true,
currentActiveMenu: '/error-log',
currentActiveMenu: '/admin/error-log',
},
},
],

4
src/router/routes/modules/dashboard.ts

@ -4,10 +4,10 @@ import { LAYOUT } from '@/router/constant';
import { t } from '@/hooks/web/useI18n';
const dashboard: AppRouteModule = {
path: '/dashboard',
path: '/admin/dashboard',
name: 'Dashboard',
component: LAYOUT,
redirect: '/dashboard/analysis',
redirect: '/admin/dashboard/analysis',
meta: {
orderNo: 10,
icon: 'ion:grid-outline',

40
src/router/routes/modules/local.ts

@ -9,7 +9,7 @@ import { LAYOUT } from '@/router/constant';
*/
export const localRoutes: AppRouteModule[] = [
{
path: '/social-callback',
path: '/admin/social-callback',
name: 'socialCallback',
component: () => import('@/views/auth/social-callback/index.vue'),
meta: {
@ -18,7 +18,7 @@ export const localRoutes: AppRouteModule[] = [
},
{
component: LAYOUT,
path: '/system/oss-config',
path: '/admin/system/oss-config',
name: 'OssConfigRoot',
meta: { title: 'OSS配置管理' },
children: [
@ -29,14 +29,14 @@ export const localRoutes: AppRouteModule[] = [
meta: {
hidden: true,
title: 'OSS配置管理',
currentActiveMenu: '/system/oss',
currentActiveMenu: '/admin/system/oss',
},
},
],
},
{
component: LAYOUT,
path: '/workflow/leaveEdit',
path: '/admin/workflow/leaveEdit',
name: 'WorkflowLeave',
meta: { title: '请假信息' },
children: [
@ -47,14 +47,14 @@ export const localRoutes: AppRouteModule[] = [
meta: {
hidden: true,
title: '请假信息',
currentActiveMenu: '/demo/leave',
currentActiveMenu: '/admin/demo/leave',
},
},
],
},
{
component: LAYOUT,
path: '/demo/purchaseEdit',
path: '/admin/demo/purchaseEdit',
name: 'SubForm',
meta: { title: '采购申请' },
children: [
@ -65,14 +65,14 @@ export const localRoutes: AppRouteModule[] = [
meta: {
hidden: true,
title: '采购申请',
currentActiveMenu: '/demo/purchase',
currentActiveMenu: '/admin/demo/purchase',
},
},
],
},
{
component: LAYOUT,
path: '/tool/generate',
path: '/admin/tool/generate',
name: 'ToolRoute',
meta: {
title: '修改生成配置',
@ -86,16 +86,16 @@ export const localRoutes: AppRouteModule[] = [
meta: {
hidden: true,
title: '修改生成配置',
currentActiveMenu: '/tool/gen',
currentActiveMenu: '/admin/tool/gen',
},
},
],
},
{
component: LAYOUT,
path: '/account',
path: '/admin/account',
name: 'AccountInfo',
redirect: '/setting',
redirect: '/admin/account/setting',
meta: {
hideMenu: true,
title: '账号',
@ -113,9 +113,9 @@ export const localRoutes: AppRouteModule[] = [
},
{
component: LAYOUT,
path: '/system/assign-roles',
path: '/admin/system/assign-roles',
name: 'AssignRolesRoot',
redirect: '/:roleId',
redirect: '/admin/system/assign-roles/:roleId',
meta: {
hideMenu: true,
title: '分配角色',
@ -127,16 +127,16 @@ export const localRoutes: AppRouteModule[] = [
component: () => import('@/views/system/role/AssignRoles/index.vue'),
meta: {
title: '分配角色',
currentActiveMenu: '/system/role',
currentActiveMenu: '/admin/system/role',
},
},
],
},
{
component: LAYOUT,
path: '/workflow/definition/history',
path: '/admin/workflow/definition/history',
name: 'WorkflowDefinitionHistory',
redirect: '/:key',
redirect: '/admin/workflow/definition/history/:key',
meta: {
hideMenu: true,
title: '历史记录',
@ -148,16 +148,16 @@ export const localRoutes: AppRouteModule[] = [
component: () => import('@/views/workflow/processDefinition/history/index.vue'),
meta: {
title: '历史记录',
currentActiveMenu: '/workflow/processDefinition',
currentActiveMenu: '/admin/workflow/processDefinition',
},
},
],
},
{
component: LAYOUT,
path: '/workflow/design',
path: '/admin/workflow/design',
name: 'BpmnDesign',
redirect: '/:id',
redirect: '/admin/workflow/design/:id',
meta: {
hideMenu: true,
title: '流程设计',
@ -169,7 +169,7 @@ export const localRoutes: AppRouteModule[] = [
component: () => import('@/views/workflow/components/BpmnDesign/index.vue'),
meta: {
title: '流程设计',
currentActiveMenu: '/workflow/model',
currentActiveMenu: '/admin/workflow/model',
},
},
],

7
src/store/modules/user.ts

@ -143,7 +143,8 @@ export const useUserStore = defineStore({
permissionStore.setDynamicAddedRoute(true);
}
goHome && (await router.replace(PageEnum.BASE_HOME));
// 移除硬编码的重定向逻辑,让路由守卫来处理
// goHome && (await router.replace(PageEnum.BASE_HOME));
}
return userInfo;
},
@ -197,11 +198,11 @@ export const useUserStore = defineStore({
this.setUserInfo(null);
if (withoutRedirect) {
// 直接回登陆页
router.replace(PageEnum.BASE_LOGIN);
router.replace(PageEnum.USER_LOGIN);
} else {
// 回登陆页带上当前路由地址
router.replace({
path: PageEnum.BASE_LOGIN,
path: PageEnum.USER_LOGIN,
query: {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
},

Loading…
Cancel
Save