12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import router from '../router'
- import NProgress from 'nprogress'
- import 'nprogress/nprogress.css'
- import {
- getToken,
- getComment,
- removeToken
- } from '@/uitls/auth'
- import store from '../store';
- import {
- Message
- } from 'element-ui'
- NProgress.configure({
- showSpinner: false
- })
- const baseUrl = ['/', '/401', '/404', '/ui', '/loginRegister/login', '/work', '/message', '/work/space/project',
- '/iot/device/detail', '/work/staging/project', '/work/operation/week/detail'
- ];
- const testBaseUrl = (path) => { //判断公共路由
- let nowBaseUrl = ['/loginRegister/login'].filter((item) => {
- return (item == path && item != '/')
- })
- return nowBaseUrl.length == 0;
- }
- /* 消息提示 */
- const tip = (msg, type) => {
- let types = type || 'warning';
- Message({
- message: msg,
- type: types,
- duration: 2000
- });
- }
- const testComment = (path) => { //判断权限路由
- let comment = getComment() ? JSON.parse(getComment()) : [];
- let nowComment = comment.concat(baseUrl).filter((item) => {
- return item == path
- })
- return nowComment.length == 0;
- }
- router.beforeEach((to, from, next) => {
- if (getToken() && !(store.getters && store.getters.user && store.getters.user.userId) && !sessionStorage
- .getItem('store')) removeToken();
- if (to.path.indexOf('website') === -1) {
- /* 判断是否登录 */
- if (!getToken() && testBaseUrl(to.path)) return next('/loginRegister/login');
- /* 判断路由是否存在 */
- if (to.matched.length === 0) return next('/404');
- /* 判断是否有权限 */
- if (testComment(to.path)) return next('/401');
- /* 无项目时跳转项目列表页面 */
- if (getToken() && localStorage.getItem('projectId') == 0 && to.path !== '/work/space/project') {
- tip('暂无项目,请先添加项目');
- return next('/work/space/project');
- }
- }
- NProgress.start();
- next();
- });
- router.afterEach(() => {
- NProgress.done();
- });
- router.onError((error) => {
- NProgress.done();
- location.reload();
- });
|