123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import router from './router'
- import store from './store'
- import { Message } from 'element-ui'
- import NProgress from 'nprogress'
- import 'nprogress/nprogress.css'
- import { getToken } from '@/utils/auth'
- import getPageTitle from '@/utils/get-page-title'
- import Layout from '@/layout'
- import { setTree, listToTree } from '@/utils/validate'
- import * as _ from 'lodash'
- NProgress.configure({ showSpinner: false })
- const whiteList = ['/login']
- function setStaticRoutes (menus, staticRoutes) {
- staticRoutes.forEach(route => {
- const routeIndex = menus.findIndex(
- permissionRoute => permissionRoute.name === route.name
- )
- if (routeIndex > -1) {
- menus[routeIndex] = {
- ...menus[routeIndex],
- component: route.component,
- children: route.children,
- }
- }
- })
- }
- 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 hasRoles = store.getters.roles && store.getters.roles.length > 0
- if (hasRoles) {
- next()
- } else {
- try {
- const treeData = await store.dispatch('user/getMenuInfo')
- const nd = treeData.filter(item => item.auth_id)
- store.dispatch('auth/changeAuthArrs', nd)
-
- const menusArray = parseMenuItem(nd)
- const treeMenu = listToTree(menusArray, 'up_auth_id', 'auth_id')
- const dataMenu = _.unionBy(treeMenu, 'auth_id')
- const ndatamenu = dataMenu.filter(item => item.auth_id == 212)
- if (ndatamenu?.length) {
- const nitemchild = ndatamenu[0].children || []
- nitemchild.map(item => {
- item.meta.keepAlive = true
- })
- }
- const menus = parseMenu(dataMenu)
- if (PLATFROM_CONFIG.hasStaticRoutes) {
- const staticRoutes = await (await import('@/router/routes/routes-file-temp')).default
- setStaticRoutes(menus, staticRoutes)
- }
- store.dispatch('permission/setRoutes', menus)
- router.addRoutes(menus)
- next({ ...to, replace: true })
- } catch (error) {
-
- await store.dispatch('user/resetToken')
- Message.error(error || 'Has Error')
- next(`/login?redirect=${to.path}`)
- NProgress.done()
- }
- }
- }
- } else {
-
- if (whiteList.indexOf(to.path) !== -1) {
-
- next()
- } else {
-
- next(`/login?redirect=${to.path}`)
- NProgress.done()
- }
- }
- })
- function parseMenuItem (arr) {
- const menus = []
- const data = _.cloneDeep(arr)
- data.map(item => {
- if (item.auth_type == 1) {
- const routeParm = {
- auth_id: item.auth_id,
- up_auth_id: item.up_auth_id,
- path: item.route_info,
- name: item.auth_ident,
- meta: {
- title: item.auth_name,
- elSvgIcon: item.show_icon ?? 'Fold',
- show_index: item.show_index,
- qid: item.queryTemplateID,
- keepAlive: true
- },
- component: Layout,
- hidden: !item.is_show,
- }
- menus.push(routeParm)
- } else if (item.auth_type == 2) {
- const routeParm = {
- auth_id: item.auth_id,
- up_auth_id: item.up_auth_id,
- path: item.route_info,
- name: item.auth_ident,
- meta: {
- title: item.auth_name,
- elSvgIcon: item.show_icon ?? 'Fold',
- show_index: item.show_index,
- qid: item.queryTemplateID,
- auth_id: item.auth_id,
- up_auth_id: item.up_auth_id
- },
- component: resolve => require(['@/views' + item.file_link], resolve),
-
- hidden: !item.is_show,
- }
- if (item.auth_ident == 'search_page') {
- routeParm.meta['keepAlive'] = true
- }
- menus.push(routeParm)
- }
- })
- return menus
- }
- function setType (arr, parentKey, key) {
- const datas = []
- for (let i = 0; i < arr.length; i++) {
- for (let j = 0; j < arr.length; j++) {
- if (arr[i][key] == arr[j][parentKey]) {
- if (arr[j].auth_type == 3) {
- datas.push(arr[j])
- arr[i].other = datas
- }
- }
- }
- }
- return arr
- }
- function parseMenu (arr) {
- const menus = arr
- const newMenus = menus.flat()
- const allMenus = newMenus.length
- ? newMenus.sort((a, b) => a.meta.show_index - b.meta.show_index)
- : []
-
-
-
- allMenus.push({
- path: '/:pathMatch(.*)',
- redirect: '/404',
- hidden: true,
- })
-
-
-
-
-
-
-
- return allMenus
- }
- router.afterEach(() => {
-
- NProgress.done()
- })
|