pull/17/head^2
xiaozhiyong 2 years ago
parent 4da9e42eba
commit 114f1ad1be
  1. 82
      src/permission.js

@ -1,79 +1,79 @@
import router from "./router"; import router from './router'
import store from "./store"; import store from './store'
import NProgress from "nprogress"; // progress bar import NProgress from 'nprogress' // progress bar
import "nprogress/nprogress.css"; // progress bar style import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from "@/utils/get-page-title"; import getPageTitle from '@/utils/get-page-title'
import Layout from "@/layout"; import Layout from '@/layout'
import serve from "api/login.js"; import serve from 'api/login.js'
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) => {
NProgress.start(); NProgress.start()
document.title = getPageTitle(to.meta.title); document.title = getPageTitle(to.meta.title)
const hasToken = localStorage.getItem("businessToken"); const hasToken = localStorage.getItem('businessToken')
if (hasToken) { if (hasToken) {
if (to.path === "/login") { if (to.path === '/login') {
next({ path: "/" }); next({ path: '/' })
NProgress.done(); NProgress.done()
} else { } else {
const hasAuth = store.getters.auth && store.getters.auth.length; const hasAuth = store.getters.auth && store.getters.auth.length
if (hasAuth) { if (hasAuth) {
next(); next()
} else { } else {
try { try {
// let infoRes = await serve.getUserInfo(); // let infoRes = await serve.getUserInfo();
// infoRes.data.authList = [1]; // infoRes.data.authList = [1];
let infoRes = await serve.info(); let infoRes = await serve.info()
// infoRes.data.authList = infoRes.authList; // infoRes.data.authList = infoRes.authList;
store.dispatch("user/info", infoRes.data); store.dispatch('user/info', infoRes.data)
let routerRes = await serve.getCustomerRouters(); let routerRes = await serve.getCustomerRouters()
let realRouter = filterAsyncRouter(routerRes.data); let realRouter = filterAsyncRouter(routerRes.data)
store.dispatch("permission/generateRoutes", realRouter); store.dispatch('permission/generateRoutes', realRouter)
router.addRoutes(realRouter); router.addRoutes(realRouter)
next({ ...to, replace: true }); next({ ...to, replace: true })
} catch (err) { } catch (err) {
console.log("catch"); console.log('catch')
next("/login"); // next("/login");
NProgress.done(); NProgress.done()
} }
} }
} }
} else { } else {
if (whiteList.includes(to.path)) { if (whiteList.includes(to.path)) {
next(); next()
} else { } else {
next("/login"); next('/login')
NProgress.done(); NProgress.done()
} }
} }
}); })
router.afterEach(() => { router.afterEach(() => {
NProgress.done(); NProgress.done()
}); })
function filterAsyncRouter(routers) { function filterAsyncRouter(routers) {
return routers.map((route) => { return routers.map(route => {
if (route.component) { if (route.component) {
if (route.component === "Layout") { if (route.component === 'Layout') {
route.component = Layout; route.component = Layout
} else { } else {
route.component = lazyLoad(route.component); route.component = lazyLoad(route.component)
} }
} }
if (route.children && route.children.length) { if (route.children && route.children.length) {
route.children = filterAsyncRouter(route.children); route.children = filterAsyncRouter(route.children)
} }
return route; return route
}); })
} }
function lazyLoad(path) { function lazyLoad(path) {
return (resolve) => require([`@/views/${path}`], resolve); return resolve => require([`@/views/${path}`], resolve)
} }

Loading…
Cancel
Save