You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.6 KiB
82 lines
2.6 KiB
/**
|
|
* Author: Fu Guobin
|
|
* Date: 2022/12/28
|
|
* Last Modified by: Fu Guobin
|
|
* Last Modified time: 2024/01/22
|
|
* Copyright:Daniel(Fu Guobin)
|
|
* Description:router配置
|
|
*/
|
|
import router from '@/router';
|
|
import { useUserStoreHook } from '@/store/modules/user';
|
|
import { usePermissionStoreHook } from '@/store/modules/permission';
|
|
import socket from '@/utils/socket';
|
|
import NProgress from 'nprogress';
|
|
import 'nprogress/nprogress.css';
|
|
NProgress.configure({ showSpinner: false }); // 进度条
|
|
|
|
const permissionStore = usePermissionStoreHook();
|
|
|
|
// 白名单路由
|
|
const whiteList = ['/login'];
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
NProgress.start();
|
|
const hasToken = localStorage.getItem('userToken');
|
|
if (hasToken) {
|
|
if (to.path === '/login') {
|
|
// 如果已登录,跳转首页
|
|
next({ path: '/' });
|
|
NProgress.done();
|
|
} else {
|
|
const userStore = useUserStoreHook();
|
|
const hasRoles = userStore.roles && userStore.roles.length > 0;
|
|
if (hasRoles) {
|
|
// 未匹配到任何路由,跳转404
|
|
if (to.matched.length === 0) {
|
|
from.name ? next({ name: from.name }) : next('/404');
|
|
} else {
|
|
// console.log('切换页面发送websocket信息');
|
|
// const exitStatu = {
|
|
// code: 'exitDataMonitor'
|
|
// };
|
|
// socket.onSend(exitStatu);
|
|
next();
|
|
}
|
|
} else {
|
|
try {
|
|
const { roles } = await userStore.getInfo();
|
|
const accessRoutes = await permissionStore.generateRoutes(roles);
|
|
accessRoutes.forEach(route => {
|
|
router.addRoute(route);
|
|
});
|
|
|
|
// console.log('登录后链接websocket');
|
|
//登录后连接webSocket
|
|
const userStorageInfo = sessionStorage.getItem('userInfo');
|
|
const userInfo = JSON.parse(userStorageInfo === null ? '' : userStorageInfo);
|
|
// const wsUrl = `ws://${window.location.host}/ws/websocket/${userInfo.userName}`; //websocket地址
|
|
// const wsUrl = `ws://10.10.10.56:9010/websocket/${userInfo.userName}`; //websocket地址
|
|
// socket.initialize(wsUrl);
|
|
next({ ...to, replace: true });
|
|
} catch (error) {
|
|
// 移除 token 并跳转登录页
|
|
await userStore.resetToken();
|
|
next(`/login?redirect=${to.path}`);
|
|
NProgress.done();
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// 未登录可以访问白名单页面
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
next();
|
|
} else {
|
|
next(`/login?redirect=${to.path}`);
|
|
NProgress.done();
|
|
}
|
|
}
|
|
});
|
|
|
|
router.afterEach(() => {
|
|
NProgress.done();
|
|
});
|
|
|