2020-09-29 17:53:01 +08:00

230 lines
7.8 KiB
JavaScript

/**
* 全站路由配置
*
* 建议:
* 1. 代码中路由统一使用name属性跳转(不使用path属性)
*/
import Vue from 'vue'
import Router from 'vue-router'
import { apiGetMenuNav } from '@/api/api_menu'
import { isURL } from '@/utils/validate'
import {clearLoginInfo} from '@/utils'
//import VConsole from 'vconsole'
//let vConsole = new VConsole()
Vue.use(Router)
// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)
// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
{ path: '/dingtalklogin', component: _import('common/dingtalklogin'), name: 'dingtalklogin', meta: { title: '钉钉自动登录' } },
{ path: '/lzluck', component: _import('common/lzluck'), name: 'lzluck', meta: { title: '霖梓抽奖' } },
{ path: '/dingtalkluck', component: _import('common/dingtalkluck'), name: 'dingtalkluck', meta: { title: '钉钉抽奖登记' } },
{ path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
{ path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } }
]
const dingtalkRoutes=[
{ path: '/devicemain', component: _import('modules/device/device-main'), name: 'devicemain', meta: { title: '资产盘点主入口' } },
{ path: '/devicetype', component: _import('modules/device/device-type'), name: 'devicetype', meta: { title: '设备类型' } },
{ path: '/deviceqr', component: _import('modules/device/device-qr'), name: 'deviceqr', meta: { title: '设备自带编码' } },
{ path: '/devicemark', component: _import('modules/device/device-mark'), name: 'devicemark', meta: { title: '设备备注' } },
{ path: '/devicestaff', component: _import('modules/device/device-staff-device'), name: 'devicestaff', meta: { title: '人员资产信息' } },
{ path: '/devicestaffdepat', component: _import('modules/device/device-staff-depat'), name: 'devicestaffdepat', meta: { title: '搜索人员' } },
{ path: '/devicedetail', component: _import('modules/device/device-detail'), name: 'devicedetail', meta: { title: '资产盘点设备详情页' } }
]
const configRoutes = [
{
path: '/recorddetail',
component: _import('modules/result/record/recorddetail'),
name: 'recorddetail',
meta: {title: '绩效详情', isDynamic: true, isTab: true}
},
{
path: '/resultDistribution',
component: _import('modules/result/distribution/index'),
name: 'resultDistribution',
meta: {title: '绩效结果分布', isDynamic: true, isTab: true}
},
{
path: '/resultReport',
component: _import('modules/result/report/index'),
name: 'resultReport',
meta: {title: '绩效列表', isDynamic: true, isTab: true}
}
]
// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
path: '/',
component: _import('layout/main'),
name: 'main',
redirect: { name: 'home' },
meta: { title: '主入口整体布局' },
children: [
// 通过meta对象设置路由展示方式
// 1. isTab: 是否通过tab展示内容, true: 是, false: 否
// 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
// 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
{ path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
{ path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } }
],
beforeEnter (to, from, next) {
//console.log('检查token')
if(fnCheckToken()){
next()
} else {
clearLoginInfo()
next({ name: 'login' })
}
/*let token = Vue.cookie.get('token')
if (!token || !/\S/.test(token)) {
clearLoginInfo()
next({ name: 'login' })
} else {
next()
}*/
}
}
function fnCheckToken() {
console.log('检查token')
let token = Vue.cookie.get('token')
if (!token || !/\S/.test(token)) {
return false;
}
return true
}
const router = new Router({
base: '/management/',
mode: 'history',
scrollBehavior: () => ({ y: 0 }),
isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
routes: globalRoutes.concat(mainRoutes).concat(dingtalkRoutes)
})
router.beforeEach((to, from, next) => {
// 添加动态(菜单)路由
// 1. 已经添加 or 全局路由, 直接访问
// 2. 获取菜单列表, 添加并保存本地存储
if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') {
next()
} else if (fnIsInRoutes(to, dingtalkRoutes)) {//判断是否全屏路由
if(fnCheckToken()){
next()
} else {
clearLoginInfo()
next({ name: 'login' })
}
//next()
/*console.log(to)
let newpage = router.resolve({
name: to.path,
query:to.query
})
console.log(newpage.href)
window.open(newpage.href, '_blank')*/
//next()
} else {
apiGetMenuNav({}).then(res => {
if (res && res.code === 0) {
fnAddDynamicMenuRoutes(res.menuList)
router.options.isAddDynamicMenuRoutes = true
sessionStorage.setItem('menuList', JSON.stringify(res.menuList || '[]'))
sessionStorage.setItem('permissions', JSON.stringify(res.permissions || '[]'))
next({ ...to, replace: true })
} else {
sessionStorage.setItem('menuList', '[]')
sessionStorage.setItem('permissions', '[]')
next()
}
})
}
})
/**
* 判断当前路由类型, global: 全局路由, main: 主入口路由
* @param {*} route 当前路由
*/
function fnCurrentRouteType (route, globalRoutes = []) {
var temp = []
for (var i = 0; i < globalRoutes.length; i++) {
if (route.path === globalRoutes[i].path) {
return 'global'
} else if (globalRoutes[i].children && globalRoutes[i].children.length >= 1) {
temp = temp.concat(globalRoutes[i].children)
}
}
return temp.length >= 1 ? fnCurrentRouteType(route, temp) : 'main'
}
function fnIsInRoutes(route, routes = []){
for(var i = 0; i < routes.length; i++) {
if(route.path === routes[i].path){
return true;
}
}
return false;
}
/**
* 添加动态(菜单)路由
* @param {*} menuList 菜单列表
* @param {*} routes 递归创建的动态(菜单)路由
*/
function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
var temp = []
for (var i = 0; i < menuList.length; i++) {
if (menuList[i].list && menuList[i].list.length >= 1) {
temp = temp.concat(menuList[i].list)
} else if (menuList[i].url && /\S/.test(menuList[i].url)) {
menuList[i].url = menuList[i].url.replace(/^\//, '')
var route = {
path: menuList[i].url.replace(/(\/)/g, '-'),
component: null,
name: menuList[i].url.replace(/(\/)/g, '-'),
meta: {
menuId: menuList[i].menuId,
title: menuList[i].name,
isDynamic: true,
isTab: true,
iframeUrl: ''
}
}
// url以http[s]://开头, 通过iframe展示
if (isURL(menuList[i].url)) {
route['path'] = `i-${menuList[i].menuId}`
route['name'] = `i-${menuList[i].menuId}`
route['meta']['iframeUrl'] = menuList[i].url
} else {
try {
route['component'] = _import(`modules/${menuList[i].url}`) || null
} catch (e) { }
}
routes.push(route)
}
}
if (temp.length >= 1) {
fnAddDynamicMenuRoutes(temp, routes)
} else {
mainRoutes.name = 'main-dynamic'
mainRoutes.children = routes.concat(configRoutes)
router.addRoutes([
mainRoutes,
{ path: '*', redirect: { name: '404' } }
])
sessionStorage.setItem('dynamicMenuRoutes', JSON.stringify(mainRoutes.children || '[]'))
console.log('%c!<--- 动态(菜单)路由加载完成 --->', 'color:blue', mainRoutes.children)
}
}
export default router