|
@@ -1,38 +1,39 @@
|
|
|
// import Vue from 'vue'
|
|
|
-import router 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 router 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";
|
|
|
|
|
|
-NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
+NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
|
|
|
|
|
-const whiteList = ['/login','/noPermission'] // no redirect whitelist
|
|
|
+const whiteList = ["/login", "/noPermission"]; // no redirect whitelist
|
|
|
|
|
|
-let isOpen = false
|
|
|
+let isOpen = false;
|
|
|
|
|
|
-router.onError((error) => {
|
|
|
+router.onError(error => {
|
|
|
if (error instanceof Error) {
|
|
|
- const isChunkLoadFailed = error.name.indexOf('chunk')
|
|
|
+ const isChunkLoadFailed = error.name.indexOf("chunk");
|
|
|
const targetPath = router.history.pending.fullPath;
|
|
|
if (isChunkLoadFailed && !isOpen) {
|
|
|
- isOpen = true
|
|
|
+ isOpen = true;
|
|
|
// (router.app.$confirm)
|
|
|
// router.push({ path: "/403", query: { path: targetPath } })
|
|
|
- router.app.$confirm("网站有更新请点击确定刷新页面?", "更新提示", {
|
|
|
- confirmButtonText: "确定",
|
|
|
- cancelButtonText: "取消",
|
|
|
- type: "warning"
|
|
|
- })
|
|
|
+ router.app
|
|
|
+ .$confirm("网站有更新请点击确定刷新页面?", "更新提示", {
|
|
|
+ confirmButtonText: "确定",
|
|
|
+ cancelButtonText: "取消",
|
|
|
+ type: "warning"
|
|
|
+ })
|
|
|
.then(() => {
|
|
|
// router.replace(targetPath);
|
|
|
- location.hash = targetPath
|
|
|
- window.location.reload()
|
|
|
+ location.hash = targetPath;
|
|
|
+ window.location.reload();
|
|
|
})
|
|
|
.catch(() => {
|
|
|
- return
|
|
|
+ return;
|
|
|
});
|
|
|
}
|
|
|
}
|
|
@@ -47,118 +48,114 @@ function getFirstMenu(routes) {
|
|
|
// }
|
|
|
// })
|
|
|
// console.log('firstMenu',firstMenu)
|
|
|
- return routes[0].redirect
|
|
|
+ return routes[0].redirect;
|
|
|
}
|
|
|
|
|
|
function pathErgodic(item) {
|
|
|
- let firstMenu = null
|
|
|
+ let firstMenu = null;
|
|
|
item.children.forEach(i => {
|
|
|
- if(!firstMenu && i.children?.length > 0) {
|
|
|
- firstMenu = pathErgodic(i)
|
|
|
+ if (!firstMenu && i.children?.length > 0) {
|
|
|
+ firstMenu = pathErgodic(i);
|
|
|
} else {
|
|
|
- if(!firstMenu && checkPathUrl(i.path)) {
|
|
|
- firstMenu = i.path
|
|
|
+ if (!firstMenu && checkPathUrl(i.path)) {
|
|
|
+ firstMenu = i.path;
|
|
|
} else {
|
|
|
- if(!firstMenu && !i.hidden) {
|
|
|
- firstMenu = item.path + '/' + i.path
|
|
|
+ if (!firstMenu && !i.hidden) {
|
|
|
+ firstMenu = item.path + "/" + i.path;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
- return firstMenu
|
|
|
+ });
|
|
|
+ return firstMenu;
|
|
|
}
|
|
|
|
|
|
// 判断path有没有带/,并且是第一个位置
|
|
|
function checkPathUrl(path) {
|
|
|
- return path.indexOf('/') === 0 ? true : false
|
|
|
+ return path.indexOf("/") === 0 ? true : false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
-
|
|
|
// from.query = to.query
|
|
|
// start progress bar
|
|
|
- NProgress.start()
|
|
|
+ NProgress.start();
|
|
|
// set page title
|
|
|
// document.title = getPageTitle(to.meta.title)
|
|
|
- document.title = getPageTitle()
|
|
|
+ document.title = getPageTitle();
|
|
|
// determine whether the user has logged in
|
|
|
- const hasToken = getToken()
|
|
|
+ const hasToken = getToken();
|
|
|
if (hasToken) {
|
|
|
- if (to.path === '/login') {
|
|
|
+ if (to.path === "/login") {
|
|
|
// 如果有tonken直接跳转到首页
|
|
|
- next({ path: '/' })
|
|
|
+ next({ path: "/" });
|
|
|
|
|
|
- NProgress.done()
|
|
|
+ NProgress.done();
|
|
|
} else {
|
|
|
- const hasGetUserInfo = store.getters.phone
|
|
|
+ const hasGetUserInfo = store.getters.phone;
|
|
|
// 有名字 说明有用户信息 跳走
|
|
|
if (hasGetUserInfo) {
|
|
|
// const accessRoutes = await store.dispatch('permission/generateRoutes')
|
|
|
|
|
|
// 动态添加可访问的路由
|
|
|
// router.addRoutes(accessRoutes)
|
|
|
- next()
|
|
|
+ next();
|
|
|
} else {
|
|
|
-
|
|
|
try {
|
|
|
// 异步获取用户信息
|
|
|
- await store.dispatch('user/getInfo')
|
|
|
+ await store.dispatch("user/getInfo");
|
|
|
// 请求接口 生成可访问路由
|
|
|
- const accessRoutes = await store.dispatch('permission/generateRoutes')
|
|
|
+ const accessRoutes = await store.dispatch(
|
|
|
+ "permission/generateRoutes"
|
|
|
+ );
|
|
|
// console.log(accessRoutes, 'accessRoutes')
|
|
|
- const isMenu = accessRoutes && accessRoutes.length > 0 ? true : false
|
|
|
-
|
|
|
- accessRoutes.push({ path: '*', redirect: '/404', hidden: true })
|
|
|
+ const isMenu = accessRoutes && accessRoutes.length > 0 ? true : false;
|
|
|
|
|
|
+ accessRoutes.push({ path: "*", redirect: "/404", hidden: true });
|
|
|
|
|
|
- console.log(accessRoutes,'accessRoutes')
|
|
|
+ console.log(accessRoutes, "accessRoutes");
|
|
|
// 动态添加可访问的路由
|
|
|
- router.addRoutes(accessRoutes)
|
|
|
+ router.addRoutes(accessRoutes);
|
|
|
// 确保addroutes完整的hack方法
|
|
|
- localStorage.removeItem('firstMenuUrl')
|
|
|
+ localStorage.removeItem("firstMenuUrl");
|
|
|
// 设置replace:true,这样导航就不会留下历史记录。
|
|
|
- let firstMenu = getFirstMenu(accessRoutes)
|
|
|
- localStorage.setItem('firstMenuUrl', firstMenu)
|
|
|
+ let firstMenu = getFirstMenu(accessRoutes);
|
|
|
+ localStorage.setItem("firstMenuUrl", firstMenu);
|
|
|
// console.log(firstMenu, 'firstMenu', isMenu)
|
|
|
// 判断是否有菜单
|
|
|
- if(isMenu) {
|
|
|
- if(to.path == '/workbench') {
|
|
|
- next({ path: firstMenu, replace: true })
|
|
|
+ if (isMenu) {
|
|
|
+ if (to.path == "/workbench") {
|
|
|
+ next({ path: firstMenu, replace: true });
|
|
|
} else {
|
|
|
- next({ ...to, replace: true })
|
|
|
+ next({ ...to, replace: true });
|
|
|
}
|
|
|
} else {
|
|
|
- next({ path: '/noPermission', replace: true })
|
|
|
+ next({ path: "/noPermission", replace: true });
|
|
|
}
|
|
|
} catch (error) {
|
|
|
// remove token and go to login page to re-login
|
|
|
- await store.dispatch('user/resetToken')
|
|
|
- if(error.msg) {
|
|
|
- Message.error(error.msg)
|
|
|
+ await store.dispatch("user/resetToken");
|
|
|
+ if (error.msg) {
|
|
|
+ Message.error(error.msg);
|
|
|
}
|
|
|
- next(`/login`)
|
|
|
- NProgress.done()
|
|
|
+ next(`/login`);
|
|
|
+ NProgress.done();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
/* has no token*/
|
|
|
- console.log(to.path,'to.path')
|
|
|
+ console.log(to.path, "to.path");
|
|
|
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`)
|
|
|
- NProgress.done()
|
|
|
+ next(`/login`);
|
|
|
+ NProgress.done();
|
|
|
}
|
|
|
}
|
|
|
-})
|
|
|
+});
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
// finish progress bar
|
|
|
- NProgress.done()
|
|
|
-})
|
|
|
-
|
|
|
-
|
|
|
+ NProgress.done();
|
|
|
+});
|