123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- import router from './router'
- import store from './store'
- import { Message } from 'element-ui'
- import { getToken } from '@/utils/auth' // get token from cookie
- import NProgress from 'nprogress' // progress bar
- import 'nprogress/nprogress.css' // progress bar style
- import getPageTitle from '@/utils/get-page-title'
- import Layout from '@/layout'
- import RouterView from '@/views/routerView.vue'
- import IframeView from '@/views/iframeView.vue'
- NProgress.configure({ showSpinner: false }) // NProgress Configuration
- const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
- const whiteList = ['/login'] // no redirect whitelist
- const lay = {
- path: '/',
- redirect: '/dashboard',
- component: Layout,
- children: []
- }
- import { pages } from './settings'
- // 递归找最后一级页面
- function getc(obj) {
- if (!obj.children || !obj.children.length) {
- return obj
- } else {
- return getc(obj.children[0])
- }
- }
- router.beforeEach(async (to, from, next) => {
- NProgress.start()
- document.title = getPageTitle(to.meta.title)
- const hasToken = getToken()
- if (hasToken) {
- if (to.path === '/login') {
- next({ path: '/' })
- NProgress.done()
- } else {
- const hasGetUserInfo = store.getters.name
- if (hasGetUserInfo) {
- next()
- } else {
- try {
- await store.dispatch('user/getInfo')
- await store.dispatch('user/getRouter')
- if (store.getters.menus.length < 1) {
- global.antRouter = []
- next()
- }
- // 设置路由
- lay.children = []
- for (var route of store.getters.menus) {
- lay.children.push(buildRoute(route))
- }
- lay.redirect = getc(lay).path
- console.log(lay.redirect, 9999, lay)
- router.addRoutes([lay])
- global.antRouter = lay.children
- next({
- ...to,
- replace: true
- })
- } catch (error) {
- await store.dispatch('user/resetToken')
- Message.error(error || 'Has Error')
- next('/login')
- NProgress.done()
- }
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- next('/login')
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
- function buildRoute(route, parentUrl = '') {
- const { url, moduleName, icon, moduleId, code, type, hidden, fullUrl } = route
- var item = {}
- item.path = ~[3, 4].indexOf(type) ? fullUrl : parentUrl + url
- item.name = ~[3, 4].indexOf(type) ? fullUrl : code
- item.type = type
- item.meta = {
- url,
- title: moduleName,
- icon: icon,
- moduleId
- }
- item.hidden = hidden
- if (route.children && route.children.length) {
- if (type == 2) {
- try {
- item.component = _import(`${fullUrl}`)
- } catch (e) {
- console.log(e)
- }
- const roles = []
- for (var role of route.children) {
- roles.push(role.code)
- }
- item.meta.roles = roles
- if (!pages.find(ite => ite.path === item.path)) {
- pages.push(item)
- }
- } else if (type == 1) {
- item.component = RouterView
- item.children = route.children.filter(item => item.type !== 3).map(child => buildRoute(child, item.path))
- }
- } else {
- if (type == 4) {
- item.component = IframeView
- if (!pages.find(ite => ite.path === item.path)) {
- pages.push(item)
- }
- } else if (type == 2) {
- try {
- item.component = _import(`${fullUrl}`)
- if (!pages.find(ite => ite.path === item.path)) {
- pages.push(item)
- }
- } catch (e) {
- console.log(e)
- }
- }
- }
- return item
- }
|