|
@@ -1,79 +1,78 @@
|
|
|
-import router from "./router";
|
|
|
-import { resetRouter } from "./router";
|
|
|
-import store from "./store";
|
|
|
-import { Message } from "element-ui";
|
|
|
-import NProgress from "nprogress"; // progress bar
|
|
|
-import "nprogress/nprogress.css"; // progress bar style
|
|
|
-import { getToken } from "@/utils/auth"; // get token from cookie
|
|
|
-import getPageTitle from "@/utils/get-page-title";
|
|
|
-import Layout from "@/layout";
|
|
|
-const _import = require("./router/_import_" + process.env.NODE_ENV); // 获取组件的方法
|
|
|
+import router from './router'
|
|
|
+import { resetRouter } from './router'
|
|
|
+import store from './store'
|
|
|
+import { Message } from 'element-ui'
|
|
|
+import NProgress from 'nprogress' // progress bar
|
|
|
+import 'nprogress/nprogress.css' // progress bar style
|
|
|
+import { getToken } from '@/utils/auth' // get token from cookie
|
|
|
+import getPageTitle from '@/utils/get-page-title'
|
|
|
+import Layout from '@/layout'
|
|
|
+const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
|
|
|
|
|
|
-NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
|
|
+NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
-const whiteList = ["/login"]; // no redirect whitelist
|
|
|
+const whiteList = ['/login'] // no redirect whitelist
|
|
|
|
|
|
-router.beforeEach(async (to, from, next) => {
|
|
|
+router.beforeEach(async(to, from, next) => {
|
|
|
// start progress bar
|
|
|
- NProgress.start();
|
|
|
+ NProgress.start()
|
|
|
|
|
|
// set page title
|
|
|
- document.title = getPageTitle(to.meta.title);
|
|
|
+ document.title = getPageTitle(to.meta.title)
|
|
|
|
|
|
// determine whether the user has logged in
|
|
|
- const hasToken = getToken();
|
|
|
+ const hasToken = getToken()
|
|
|
if (hasToken) {
|
|
|
- if (to.path === "/login") {
|
|
|
+ if (to.path === '/login') {
|
|
|
// if is logged in, redirect to the home page
|
|
|
- next({ path: "/" });
|
|
|
- NProgress.done();
|
|
|
+ next({ path: '/' })
|
|
|
+ NProgress.done()
|
|
|
} else {
|
|
|
- const hasGetUserInfo = store.getters.name;
|
|
|
+ const hasGetUserInfo = store.getters.name
|
|
|
if (hasGetUserInfo) {
|
|
|
- next();
|
|
|
+ next()
|
|
|
} else {
|
|
|
try {
|
|
|
// get user info
|
|
|
- await store.dispatch("user/getInfo");
|
|
|
+ await store.dispatch('user/getInfo')
|
|
|
|
|
|
// 请求获取路由表
|
|
|
- await store.dispatch("user/getRouter");
|
|
|
+ await store.dispatch('user/getRouter')
|
|
|
if (store.getters.menus.length < 1) {
|
|
|
- global.antRouter = [];
|
|
|
- next();
|
|
|
+ global.antRouter = []
|
|
|
+ next()
|
|
|
}
|
|
|
|
|
|
// 设置路由
|
|
|
- var newRoutes = [];
|
|
|
+ var newRoutes = []
|
|
|
|
|
|
for (var route of store.getters.menus) {
|
|
|
-
|
|
|
- const item = buildRootRoute(route);
|
|
|
- newRoutes.push(item);
|
|
|
+ const item = buildRootRoute(route)
|
|
|
+ newRoutes.push(item)
|
|
|
}
|
|
|
- console.log(newRoutes,'8589');
|
|
|
+ console.log(newRoutes, '8589')
|
|
|
// newRoutes.shift();
|
|
|
// 添加一项根目录重定向页面
|
|
|
- if (newRoutes[0].path != "/") {
|
|
|
- let path = newRoutes[0].path;
|
|
|
+ if (newRoutes[0].path !== '/') {
|
|
|
+ let path = newRoutes[0].path
|
|
|
if (newRoutes[0].children.length > 0) {
|
|
|
- path = `${path}/${newRoutes[0].children[0].path}`;
|
|
|
+ path = `${path}/${newRoutes[0].children[0].path}`
|
|
|
}
|
|
|
if (newRoutes[0].children[0].children.length > 0) {
|
|
|
- path = `${path}/${newRoutes[0].children[0].children[0].path}`;
|
|
|
+ path = `${path}/${newRoutes[0].children[0].children[0].path}`
|
|
|
}
|
|
|
newRoutes.unshift({
|
|
|
- path: "/",
|
|
|
+ path: '/',
|
|
|
component: Layout,
|
|
|
- redirect: path,
|
|
|
- });
|
|
|
+ redirect: path
|
|
|
+ })
|
|
|
} else {
|
|
|
- newRoutes[0].redirect = "/dashboard";
|
|
|
+ newRoutes[0].redirect = '/dashboard'
|
|
|
}
|
|
|
|
|
|
- console.log(newRoutes);
|
|
|
- router.addRoutes(newRoutes); // 2.动态添加路由
|
|
|
- global.antRouter = newRoutes; // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
|
|
|
+ console.log(newRoutes)
|
|
|
+ router.addRoutes(newRoutes) // 2.动态添加路由
|
|
|
+ global.antRouter = newRoutes // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
|
|
|
|
|
|
// const menus = filterAsyncRouter(store.getters.menus) // 1.过滤路由
|
|
|
// console.log(menus);
|
|
@@ -81,16 +80,16 @@ router.beforeEach(async (to, from, next) => {
|
|
|
// global.antRouter = menus // 3.将路由数据传递给全局变量,做侧边栏菜单渲染工作
|
|
|
next({
|
|
|
...to,
|
|
|
- replace: true,
|
|
|
- });
|
|
|
+ replace: true
|
|
|
+ })
|
|
|
// next()
|
|
|
} catch (error) {
|
|
|
// remove token and go to login page to re-login
|
|
|
- await store.dispatch("user/resetToken");
|
|
|
- Message.error(error || "Has Error");
|
|
|
+ await store.dispatch('user/resetToken')
|
|
|
+ Message.error(error || 'Has Error')
|
|
|
next('/login')
|
|
|
// next(`/login?redirect=${to.path}`);
|
|
|
- NProgress.done();
|
|
|
+ NProgress.done()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -99,20 +98,20 @@ router.beforeEach(async (to, from, next) => {
|
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
// in the free login whitelist, go directly
|
|
|
- next();
|
|
|
+ next()
|
|
|
} else {
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
// next(`/login?redirect=${to.path}`);
|
|
|
next('/login')
|
|
|
- NProgress.done();
|
|
|
+ NProgress.done()
|
|
|
}
|
|
|
}
|
|
|
-});
|
|
|
+})
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
// finish progress bar
|
|
|
- NProgress.done();
|
|
|
-});
|
|
|
+ NProgress.done()
|
|
|
+})
|
|
|
|
|
|
// 遍历后台传来的路由字符串,转换为组件对象
|
|
|
// function filterAsyncRouter(asyncRouterMap) {
|
|
@@ -134,148 +133,143 @@ router.afterEach(() => {
|
|
|
// }
|
|
|
|
|
|
function buildRootRoute(route) {
|
|
|
- const { url, icon, moduleName, moduleId, code, type,fullUrl } = route;
|
|
|
- var item = {};
|
|
|
- item.path = url;
|
|
|
- item.component = Layout;
|
|
|
- item.name = code;
|
|
|
+ const { url, icon, moduleName, moduleId, code, type, fullUrl } = route
|
|
|
+ var item = {}
|
|
|
+ item.path = url
|
|
|
+ item.component = Layout
|
|
|
+ item.name = fullUrl || code
|
|
|
item.meta = {
|
|
|
title: moduleName,
|
|
|
- icon: icon,
|
|
|
- };
|
|
|
- if (code == "index") {
|
|
|
- item.path = "/";
|
|
|
- item.alwaysShow = false;
|
|
|
- let children = route.children;
|
|
|
- route.children = [];
|
|
|
+ icon: icon
|
|
|
+ }
|
|
|
+ if (code == 'index') {
|
|
|
+ item.path = '/'
|
|
|
+ item.alwaysShow = false
|
|
|
+ const children = route.children
|
|
|
+ route.children = []
|
|
|
route.children.push({
|
|
|
- code: "dashboard",
|
|
|
- moduleName: "首页",
|
|
|
+ code: 'dashboard',
|
|
|
+ moduleName: '首页',
|
|
|
type: 2,
|
|
|
- url: "/dashboard",
|
|
|
- children: children,
|
|
|
- });
|
|
|
+ url: '/dashboard',
|
|
|
+ children: children
|
|
|
+ })
|
|
|
}
|
|
|
- if (code == "issue") {
|
|
|
- let children = route.children;
|
|
|
- route.children = [];
|
|
|
+ if (code == 'issue') {
|
|
|
+ const children = route.children
|
|
|
+ route.children = []
|
|
|
route.children.push({
|
|
|
- code: "issue_index",
|
|
|
- moduleName: "文件下发",
|
|
|
+ code: 'issue_index',
|
|
|
+ moduleName: '文件下发',
|
|
|
type: 2,
|
|
|
- url: "/index",
|
|
|
- children: children,
|
|
|
- });
|
|
|
- item.alwaysShow = false;
|
|
|
+ url: '/index',
|
|
|
+ children: children
|
|
|
+ })
|
|
|
+ item.alwaysShow = false
|
|
|
}
|
|
|
- if (code == "notice") {
|
|
|
- let children = route.children;
|
|
|
- route.children = [];
|
|
|
+ if (code === 'notice') {
|
|
|
+ const children = route.children
|
|
|
+ route.children = []
|
|
|
route.children.push({
|
|
|
- code: "notice_index",
|
|
|
- moduleName: "系统通知",
|
|
|
+ code: 'notice_index',
|
|
|
+ moduleName: '系统通知',
|
|
|
type: 2,
|
|
|
- url: "/index",
|
|
|
- children: children,
|
|
|
- });
|
|
|
- item.alwaysShow = false;
|
|
|
+ url: '/index',
|
|
|
+ children: children
|
|
|
+ })
|
|
|
+ item.alwaysShow = false
|
|
|
}
|
|
|
- item.children = [];
|
|
|
+ item.children = []
|
|
|
if (
|
|
|
- (route.hasOwnProperty("children") && type === 1) ||
|
|
|
- code == "issue" ||
|
|
|
- code == "notice" ||
|
|
|
- code == "index"
|
|
|
+ (route.hasOwnProperty('children') && type === 1) ||
|
|
|
+ code == 'issue' ||
|
|
|
+ code == 'notice' ||
|
|
|
+ code == 'index'
|
|
|
) {
|
|
|
-
|
|
|
for (var child of route.children) {
|
|
|
- item.children.push(buildRoute(child, fullUrl));
|
|
|
+ item.children.push(buildRoute(child, fullUrl))
|
|
|
}
|
|
|
}
|
|
|
- return item;
|
|
|
+ return item
|
|
|
}
|
|
|
|
|
|
function buildRoute(route, p_url) {
|
|
|
- const { url, moduleName, icon, moduleId, code, type, hidden,fullUrl } = route;
|
|
|
- var item = {};
|
|
|
- if (url.substr(0, 1) == "/") {
|
|
|
- item.path = url.substr(1);
|
|
|
+ const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl } = route
|
|
|
+ var item = {}
|
|
|
+ if (url.substr(0, 1) === '/') {
|
|
|
+ item.path = url.substr(1)
|
|
|
} else {
|
|
|
- item.path = url;
|
|
|
+ item.path = url
|
|
|
}
|
|
|
try {
|
|
|
- if (code == "dashboard") {
|
|
|
+ if (code == 'dashboard') {
|
|
|
// item.component = _import(`${url}/index`)
|
|
|
item.component = (resolve) =>
|
|
|
- require(["@/views" + `${url}/index`], resolve);
|
|
|
+ require(['@/views' + `${url}/index`], resolve)
|
|
|
} else {
|
|
|
-
|
|
|
if (fullUrl) {
|
|
|
- item.component = _import(`${fullUrl}`);
|
|
|
- }else{
|
|
|
- item.component = _import(`${p_url}${url}`);
|
|
|
-
|
|
|
+ item.component = _import(`${fullUrl}`)
|
|
|
+ } else {
|
|
|
+ item.component = _import(`${p_url}${url}`)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
} catch (e) {
|
|
|
- console.log(e);
|
|
|
+ console.log(e)
|
|
|
}
|
|
|
- item.name = code;
|
|
|
+ item.name = fullUrl || code
|
|
|
item.meta = {
|
|
|
title: moduleName,
|
|
|
- icon: icon,
|
|
|
- };
|
|
|
+ icon: icon
|
|
|
+ }
|
|
|
if (route.children && type == 2) {
|
|
|
- let roles = [];
|
|
|
+ const roles = []
|
|
|
for (var role of route.children) {
|
|
|
- roles.push(role.code);
|
|
|
+ roles.push(role.code)
|
|
|
}
|
|
|
- item.meta.roles = roles;
|
|
|
+ item.meta.roles = roles
|
|
|
}
|
|
|
- item.hidden = hidden;
|
|
|
- item.children = [];
|
|
|
- if (route.hasOwnProperty("children") && type === 1) {
|
|
|
+ item.hidden = hidden
|
|
|
+ item.children = []
|
|
|
+ // eslint-disable-next-line no-prototype-builtins
|
|
|
+ if (route.hasOwnProperty('children') && type === 1) {
|
|
|
for (var child of route.children) {
|
|
|
- item.children.push(buildThirdRoute(child, `${p_url}${url}`));
|
|
|
+ item.children.push(buildThirdRoute(child, `${p_url}${url}`))
|
|
|
}
|
|
|
}
|
|
|
- return item;
|
|
|
+ return item
|
|
|
}
|
|
|
|
|
|
function buildThirdRoute(route, p_url) {
|
|
|
-
|
|
|
- const { url, moduleName, icon, moduleId, code, hidden, type ,fullUrl } = route;
|
|
|
- var item = {};
|
|
|
- if (url.substr(0, 1) == "/") {
|
|
|
- item.path = url.substr(1);
|
|
|
+ const { url, moduleName, icon, moduleId, code, hidden, type, fullUrl } = route
|
|
|
+ var item = {}
|
|
|
+ if (url.substr(0, 1) == '/') {
|
|
|
+ item.path = url.substr(1)
|
|
|
} else {
|
|
|
- tem.path = url;
|
|
|
+ tem.path = url
|
|
|
}
|
|
|
try {
|
|
|
if (fullUrl) {
|
|
|
- item.component = _import(`${fullUrl}`);
|
|
|
- }else{
|
|
|
- item.component = _import(`${p_url}${url}`);
|
|
|
+ item.component = _import(`${fullUrl}`)
|
|
|
+ } else {
|
|
|
+ item.component = _import(`${p_url}${url}`)
|
|
|
}
|
|
|
-
|
|
|
} catch (e) {
|
|
|
- console.log(e);
|
|
|
+ console.log(e)
|
|
|
}
|
|
|
- item.name = code;
|
|
|
+ item.name = fullUrl || code
|
|
|
|
|
|
item.meta = {
|
|
|
title: moduleName,
|
|
|
- icon: icon,
|
|
|
- };
|
|
|
+ icon: icon
|
|
|
+ }
|
|
|
if (route.children && type == 2) {
|
|
|
- let roles = [];
|
|
|
+ const roles = []
|
|
|
for (var role of route.children) {
|
|
|
- roles.push(role.code);
|
|
|
+ roles.push(role.code)
|
|
|
}
|
|
|
- item.meta.roles = roles;
|
|
|
+ item.meta.roles = roles
|
|
|
}
|
|
|
- item.hidden = hidden;
|
|
|
- item.children = [];
|
|
|
- return item;
|
|
|
+ item.hidden = hidden
|
|
|
+ item.children = []
|
|
|
+ return item
|
|
|
}
|