import {isURL} from '../../packages/utils/validate'
|
import http from '../../packages/utils/request'
|
import Cookies from 'js-cookie'
|
import {getUUID} from '../../packages/utils'
|
import {EventBus2} from "../commonJS/eventbus2";
|
|
let $router
|
let $moduleRoutes // 模块路由(基于主入口布局页面)
|
let $fnGetComponent // 模块引用方法
|
export function setConfig(router, moduleRoutes, fnGetComponent) {
|
$router = router
|
$moduleRoutes = moduleRoutes
|
$fnGetComponent = fnGetComponent
|
}
|
|
// 页面路由(独立页面)
|
export const pageRoutes = [
|
{
|
path: '/404',
|
component: () => import('../views/pages/404'),
|
name: '404',
|
meta: {title: '404未找到'},
|
beforeEnter(to, from, next) {
|
// 拦截处理特殊业务场景
|
// 如果, 重定向路由包含__双下划线, 为临时添加路由
|
if (/__.*/.test(to.redirectedFrom)) {
|
return next(to.redirectedFrom.replace(/__.*/, ''))
|
}
|
next()
|
}
|
}
|
]
|
|
export function getMenu() {
|
console.log('getMenu1111')
|
http.get('/sys/menu/nav').then(res => {
|
console.log(res.data,'getMenu22222')
|
if (res.code !== 0) {
|
return
|
}
|
let menuList = res.data
|
console.log(res.data,'getMenu3333')
|
if (menuList && menuList.length > 0) {
|
console.log(res.data,'getMenu22222')
|
//window.SITE_CONFIG['menuList'] = menuList
|
//给开发者添加代码生成功能
|
if (window.SITE_CONFIG['nodeEnv'] === 'development') {
|
menuList[0].children.push({
|
id: 'development',
|
name: '开发工具',
|
isRefresh: 0,
|
icon: 'icon-solution',
|
url: 'development',
|
showMenu:1,
|
pid:menuList[0].id,
|
params: ''
|
})
|
/* menuList[0].children.push({
|
id: 'generator',
|
name: '代码生成',
|
isRefresh: 0,
|
icon: 'icon-solution',
|
url: 'generator',
|
showMenu:1,
|
pid:menuList[0].id,
|
params: ''
|
})*/
|
}
|
fnAddDynamicMenuRoutes(menuList, [])
|
console.log(window.SITE_CONFIG['dynamicMenuRoutes'],'window.SITE_CONFIG[\'dynamicMenuRoutes\']')
|
}
|
else{
|
tip.error('您没有使用系统的权限!')
|
return next({name: 'login'})
|
}
|
window.SITE_CONFIG['menuList'] = menuList
|
console.log(window.SITE_CONFIG['menuList'], '0window.SITE_CONFIG[\'menuList\']')
|
// let that = this
|
setTimeout(() => {
|
//this.flag = getUUID()
|
EventBus2.$emit('setMenuList')
|
console.log('set flag')
|
}, 250)
|
})
|
}
|
|
export function beforeEach(to, from, next) {
|
//alert('beforeEach')
|
// 添加动态(菜单)路由
|
// 已添加或者当前路由为页面路由, 可直接访问
|
|
/*
|
console.log(to, ' beforeEach to')
|
console.log(from, ' beforeEach from')
|
console.log(next, ' beforeEach next')
|
console.log(pageRoutes, 'beforeEach pageRoutes')
|
console.log(window.SITE_CONFIG['menuList'], 'beforeEach window.SITE_CONFIG[\'menuList\'] ')
|
*/
|
|
if (to.name == 'home' || to.name == 'single' || window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) {
|
return next()
|
}
|
console.log('重新获取菜单权限', ' beforeEach')
|
|
//alert('重新获取菜单权限')
|
// 获取字典
|
|
if (Cookies.get('token') == null) {
|
tip.error('您没有使用登录系统!')
|
return next({name: 'login'})
|
} else if (window.SITE_CONFIG['menuList'].length < 1) {
|
http.get('/sys/menu/nav').then(res => {
|
if (res.code !== 0) {
|
return
|
}
|
let menuList = res.data
|
if (menuList && menuList.length > 0) {
|
//给开发者添加代码生成功能
|
if (window.SITE_CONFIG['nodeEnv'] === 'development') {
|
menuList[0].children.push({
|
id: 'development',
|
name: '开发工具',
|
icon: 'icon-solution',
|
url: 'development',
|
params: ''
|
})
|
}
|
//window.SITE_CONFIG['menuList'] = menuList
|
fnAddDynamicMenuRoutes(menuList, [])
|
window.SITE_CONFIG['menuList'] = menuList
|
console.log('enter beforeEach1')
|
console.log(window.SITE_CONFIG['menuList'], '1window.SITE_CONFIG[\'menuList\']')
|
console.log(from.path, 'from.path')
|
if (from.path === '/') {
|
next({...to, replace: true})
|
} else if (from.path === '/map') {
|
next({...to})
|
} else {
|
next({...to})
|
}
|
setTimeout(() => {
|
//this.flag = getUUID()
|
EventBus2.$emit('setMenuList')
|
console.log('set flag')
|
}, 50)
|
} else {
|
tip.error('您没有使用系统的权限!')
|
return next({name: 'login'})
|
}
|
})
|
}else{
|
console.log('enter beforeEach2')
|
console.log(window.SITE_CONFIG['menuList'], '2window.SITE_CONFIG[\'menuList\']')
|
return next()
|
}
|
console.log('enter beforeEach3')
|
console.log(window.SITE_CONFIG['menuList'], '3window.SITE_CONFIG[\'menuList\']')
|
}
|
|
/**
|
* 判断当前路由是否为页面路由
|
* @param {*} route 当前路由
|
* @param {*} pageRoutes 页面路由
|
*/
|
function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
|
var temp = []
|
for (var i = 0; i < pageRoutes.length; i++) {
|
if (route.path === pageRoutes[i].path) {
|
return true
|
}
|
if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
|
temp = temp.concat(pageRoutes[i].children)
|
}
|
}
|
return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false
|
}
|
|
function isJSON(str) {
|
if (typeof str == 'string') {
|
try {
|
var obj = JSON.parse(str);
|
if (typeof obj == 'object' && obj) {
|
return true;
|
} else {
|
return false;
|
}
|
|
} catch (e) {
|
console.log('error:' + str + '!!!' + e);
|
return false;
|
}
|
}
|
console.log('It is not a string!')
|
}
|
|
/**
|
* 添加动态(菜单)路由
|
* @param {*} menuList 菜单列表
|
* @param {*} routes 递归创建的动态(菜单)路由
|
*/
|
export function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
|
var temp = []
|
console.log(menuList, 'menuList')
|
for (var i = 0; i < menuList.length; i++) {
|
if (menuList[i].children && menuList[i].children.length >= 1) {
|
temp = temp.concat(menuList[i].children)
|
continue
|
}
|
// 组装路由
|
let params = {}
|
if (menuList[i].params !== null && menuList[i].params !== '') {
|
let paramsStr = menuList[i].params.replace(/^\s+|\s+$/gm, '')
|
if (isJSON(paramsStr)) {
|
//alert(paramsStr)
|
params = JSON.parse(paramsStr)
|
}
|
}
|
var route = {
|
path: '',
|
component: null,
|
name: '',
|
meta: {
|
...window.SITE_CONFIG['contentTabDefault'],
|
menuId: menuList[i].id,
|
title: menuList[i].name,
|
isRefresh: menuList[i].isRefresh,
|
params: params,
|
remark: menuList[i].remark
|
}
|
}
|
// eslint-disable-next-line
|
let URL = (menuList[i].url || '').replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2)) // URL支持{{ window.xxx }}占位符变量
|
if (isURL(URL)) {
|
console.log(menuList[i].name, '111')
|
route['path'] = `i-${menuList[i].id}`
|
route['name'] = `i-${menuList[i].id}`
|
route['meta']['iframeURL'] = URL
|
} else {
|
URL = URL.replace(/^\//, '').replace(/_/g, '-')
|
route['path'] = URL.replace(/\//g, '-')
|
route['name'] = menuList[i].id
|
route['component'] = () => {
|
if (URL === 'generator') {
|
return import('../../packages/views/pages/generator')
|
}
|
else if (URL === 'development') {
|
return import('../../packages/views/pages/development')
|
}
|
else if (PackagesViews.indexOf(URL + '.vue') >= 0) {
|
return import(`../../packages/views/modules/${URL}`)
|
} else {
|
return $fnGetComponent(URL)
|
}
|
}
|
}
|
routes.push(route)
|
}
|
if (temp.length >= 1) {
|
return fnAddDynamicMenuRoutes(temp, routes)
|
}
|
// 添加路由
|
$router.addRoutes([
|
{
|
...$moduleRoutes,
|
name: 'main-dynamic-menu',
|
children: routes
|
},
|
{path: '*', redirect: {name: '404'}}
|
])
|
console.log($router, '$router')
|
console.log(routes, 'routes')
|
window.SITE_CONFIG['dynamicMenuRoutes'] = routes
|
window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = true
|
}
|
|
// 添加动态路由
|
export function addDynamicRoute(routeParams) {
|
// 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
|
var routeName = routeParams.routeName
|
var dynamicRoute = window.SITE_CONFIG['dynamicRoutes'].filter(item => item.name === routeName)[0]
|
if (dynamicRoute) {
|
return $router.push({
|
name: routeName,
|
params: routeParams.params
|
})
|
}
|
// 否则: 添加并全局变量保存, 再跳转
|
dynamicRoute = {
|
path: routeName,
|
component: () => {
|
if (PackagesViews.indexOf(routeParams.path) >= 0) {
|
return import(`../../packages/views/modules/${routeParams.path}`)
|
} else {
|
return $fnGetComponent(routeParams.path)
|
}
|
},
|
name: routeName,
|
meta: {
|
...window.SITE_CONFIG['contentTabDefault'],
|
menuId: routeParams.menuId,
|
title: `${routeParams.title}`,
|
remark: `${routeParams.remark}`
|
}
|
}
|
$router.addRoutes([{
|
...$moduleRoutes,
|
name: `main-dynamic__${dynamicRoute.name}`,
|
children: [dynamicRoute]
|
}])
|
window.SITE_CONFIG['dynamicRoutes'].push(dynamicRoute)
|
$router.push({
|
name: dynamicRoute.name,
|
params: routeParams.params
|
})
|
}
|
|
// 框架页面find $PWD/* | xargs ls -d | grep '.vue'
|
const PackagesViews = [
|
'message/mail-log.vue',
|
'message/mail-template-add-or-update.vue',
|
'message/mail-template-config.vue',
|
'message/mail-template-send.vue',
|
'message/mail-template.vue',
|
'message/sms-log.vue',
|
'message/sms-send.vue',
|
'message/sms-template-add-or-update.vue',
|
'message/sms-template.vue',
|
'notice/notice-add-or-update.vue',
|
'notice/notice-user-view.vue',
|
'notice/notice-user.vue',
|
'notice/notice-view.vue',
|
'notice/notice.vue',
|
'oss/oss.vue',
|
'sys/company-add-or-update.vue',
|
'sys/company.vue',
|
'sys/dept-add-or-update.vue',
|
'sys/dept.vue',
|
'sys/dict-data-add-or-update.vue',
|
'sys/dict-data.vue',
|
'sys/dict-type-add-or-update.vue',
|
'sys/dict-type.vue',
|
'sys/job-add-or-update.vue',
|
'sys/job.vue',
|
'sys/log-error.vue',
|
'sys/log-login.vue',
|
'sys/log-operation.vue',
|
'sys/menu-add-or-update.vue',
|
'sys/menu.vue',
|
'sys/params-add-or-update.vue',
|
'sys/params.vue',
|
'sys/post-add-or-update.vue',
|
'sys/post.vue',
|
'sys/role-add-or-update.vue',
|
'sys/role-data-scope.vue',
|
'sys/role-menu.vue',
|
'sys/role.vue',
|
'sys/tenant-add-or-update.vue',
|
'sys/tenant-menu.vue',
|
'sys/tenant.vue',
|
'sys/user-add-or-update.vue',
|
'sys/user-role.vue',
|
'sys/user.vue'
|
]
|