feat: 路由守卫调整/
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-02-17 11:58:44
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @LastEditTime: 2025-06-20 06:00:54
|
||||
* @LastEditTime: 2025-06-23 03:16:47
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
@ -46,11 +46,6 @@ export class Request {
|
||||
(config: AxiosRequestConfig) => {
|
||||
const token = localStorage.getItem('accessToken') as string;
|
||||
config.headers!.Authorization = token;
|
||||
if (token) {
|
||||
config.headers!.Authorization = token;
|
||||
} else {
|
||||
config.headers!.satoken = '123';
|
||||
}
|
||||
|
||||
if (enterprise) {
|
||||
config.headers!['enterprise-id'] = enterprise.id;
|
||||
|
||||
@ -3,14 +3,19 @@ import { useAppStore } from '@/stores';
|
||||
import { IconExport, IconFile, IconCaretDown } from '@arco-design/web-vue/es/icon';
|
||||
import { fetchMenusTree } from '@/api/all';
|
||||
import { handleUserLogout } from '@/utils/user';
|
||||
import { fetchLogOut } from '@/api/all/login';
|
||||
|
||||
const lists = ref([]);
|
||||
const router = useRouter();
|
||||
const clickExit = () => {
|
||||
handleUserLogout();
|
||||
const clickExit = async () => {
|
||||
const { code } = await fetchLogOut();
|
||||
if (code === 200) {
|
||||
handleUserLogout();
|
||||
}
|
||||
};
|
||||
const getMenus = async () => {
|
||||
const res = await fetchMenusTree();
|
||||
if (res.code == 200) {
|
||||
if (res.code === 200) {
|
||||
lists.value = res.data;
|
||||
}
|
||||
};
|
||||
@ -25,7 +30,7 @@ function setServerMenu() {
|
||||
}
|
||||
const handleSelect = (index: any) => {
|
||||
console.log(index);
|
||||
if (index == 0) {
|
||||
if (index === 0) {
|
||||
router.push('/workplace');
|
||||
} else {
|
||||
router.push('/dataEngine/dataEngine/hotTranslation');
|
||||
|
||||
@ -1,10 +1,3 @@
|
||||
/*
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-03-05 18:14:17
|
||||
* @LastEditors: 田鑫
|
||||
* @LastEditTime: 2023-03-05 18:24:40
|
||||
* @Description:
|
||||
*/
|
||||
export const WHITE_LIST = [
|
||||
{ name: 'notFound', children: [] },
|
||||
{ name: 'login', children: [] },
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/*
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-03-05 18:14:17
|
||||
* @LastEditors: 田鑫
|
||||
* @LastEditTime: 2023-03-05 19:21:15
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @LastEditTime: 2025-06-23 02:08:22
|
||||
* @Description:
|
||||
*/
|
||||
import type { Router } from 'vue-router';
|
||||
|
||||
@ -1,36 +1,27 @@
|
||||
/*
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-03-05 14:46:43
|
||||
* @LastEditors: 田鑫
|
||||
* @LastEditTime: 2023-03-05 15:59:25
|
||||
* @Description: 路由登录状态守卫
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-06-22 22:59:16
|
||||
*/
|
||||
import type { Router, LocationQueryRaw } from 'vue-router';
|
||||
import NProgress from 'nprogress'; // progress bar
|
||||
import type { Router } from 'vue-router';
|
||||
import NProgress from 'nprogress';
|
||||
import { goUserLogin } from '@/utils/user';
|
||||
|
||||
import { isLogin, clearAllLocalStorage } from '@/utils/auth';
|
||||
import { useUserStore } from '@/stores/modules/user';
|
||||
|
||||
export default function setupUserLoginInfoGuard(router: Router) {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
console.log('access login info router guard');
|
||||
NProgress.start();
|
||||
if (to.name === 'auth') {
|
||||
next();
|
||||
}
|
||||
const userStore = useUserStore();
|
||||
//* 判断用户是否登录,若登录则放过,进入下一步
|
||||
//* 若无,则清空所有缓存并弹回登录鉴权页
|
||||
if (isLogin()) {
|
||||
if (userStore.role) {
|
||||
next();
|
||||
} else {
|
||||
userStore.getUserInfo();
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
clearAllLocalStorage();
|
||||
next({ name: 'UserLogin' }); // 添加缺失的next调用
|
||||
|
||||
const requireLogin = to?.meta?.requireLogin || 0;
|
||||
const isLogin = !!userStore.isLogin;
|
||||
|
||||
if (requireLogin === 1 && !isLogin) {
|
||||
goUserLogin();
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
/*
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-03-05 18:14:17
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @LastEditTime: 2025-06-23 01:39:50
|
||||
* @Description:
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-06-22 22:59:16
|
||||
*/
|
||||
import { createRouter, createWebHistory } from 'vue-router';
|
||||
import { appRoutes } from './routes';
|
||||
@ -22,6 +19,9 @@ const router = createRouter({
|
||||
path: '/login',
|
||||
name: 'UserLogin',
|
||||
component: () => import('@/views/components/login'),
|
||||
meta: {
|
||||
requireLogin: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/workplace',
|
||||
@ -29,6 +29,7 @@ const router = createRouter({
|
||||
component: () => import('@/views/components/workplace'),
|
||||
meta: {
|
||||
hideSidebar: true,
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -36,37 +37,58 @@ const router = createRouter({
|
||||
name: 'Home',
|
||||
redirect: '/dataEngine/dataEngine/hotTranslation',
|
||||
children: [...appRoutes, REDIRECT_MAIN, NOT_FOUND_ROUTE],
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/dataEngine',
|
||||
name: 'dataEngine',
|
||||
redirect: '/dataEngine/dataEngine/hotTranslation',
|
||||
children: [...appRoutes, REDIRECT_MAIN, NOT_FOUND_ROUTE],
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/permission',
|
||||
name: 'permission',
|
||||
component: () => import('@/views/components/permission/choose-enterprise.vue'),
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/auth',
|
||||
name: 'auth',
|
||||
component: () => import('@/views/components/permission/auth.vue'),
|
||||
meta: {
|
||||
requireLogin: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/management/person',
|
||||
name: 'person',
|
||||
component: () => import('@/views/components/management/person'),
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/management/enterprise',
|
||||
name: 'enterprise',
|
||||
component: () => import('@/views/components/management/enterprise'),
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/management/account',
|
||||
name: 'account',
|
||||
component: () => import('@/views/components/management/account'),
|
||||
meta: {
|
||||
requireLogin: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
scrollBehavior() {
|
||||
|
||||
@ -20,7 +20,7 @@ export const useEnterpriseStore = defineStore('enterprise', {
|
||||
update_name_quota: 2,
|
||||
used_update_name_count: 1,
|
||||
sub_account_quota: 2,
|
||||
used_sub_account_count: 2,
|
||||
used_sub_account_count: 0,
|
||||
},
|
||||
}),
|
||||
actions: {
|
||||
|
||||
@ -1,7 +1,3 @@
|
||||
/*
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-06-22 22:26:22
|
||||
*/
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
interface UserInfo {
|
||||
@ -39,6 +35,11 @@ export const useUserStore = defineStore('user', {
|
||||
companyInfo: null,
|
||||
isLogin: false,
|
||||
}),
|
||||
getters: {
|
||||
isLogin(): boolean {
|
||||
return !!this.token;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
// 设置 Token
|
||||
setToken(token: string) {
|
||||
@ -74,14 +75,5 @@ export const useUserStore = defineStore('user', {
|
||||
getCompanyInfo(): CompanyInfo | null {
|
||||
return this.companyInfo;
|
||||
},
|
||||
|
||||
// 登录状态
|
||||
setIsLogin(isLogin: boolean) {
|
||||
this.isLogin = isLogin;
|
||||
},
|
||||
|
||||
getIsLogin(): boolean {
|
||||
return this.isLogin;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user