Commit e8e8dfd5 authored by trumansdo's avatar trumansdo
Browse files

done------

完成动态路由,等后面填补一下数据库和前端的路由数据就可以进行下一步的页面代码开发
重写HTTPRequestLocal线程共享变量方法,使之责任明确
添加beetlsql的类型处理,使之可以用时间戳,更加通用
wait-----
在确定是不是要重构一下项目模块,使之更明确
parent 62046c00
// 后台数据中的对应的路由表
/*
{
"path": "/profile",
"component": "Layout",
"redirect": "/profile/index",
"hidden": true,
"alwaysShow": true,
"name": "router-name",
"meta": {
"noCache": true,
"affix": true,
"breadcrumb": false,
"activeMenu": "/example/list"
},
"children": []
}
*/
import Layout from '@/layout'
const coreRouter = [
{
path: '/admin/user/index.do',
name: '用户功能',
component: Layout,
alwaysShow: true,
meta: {
affix: true,
title: '用户管理',
icon: null,
roles: [1, 173, 3]
},
children: []
}
]
export default coreRouter
......@@ -3,6 +3,36 @@
格式见最下方的注释
需要大改菜单表
*/
/*
前端路由映射表中单个路由映射全部具有的信息
{
"path": "/profile",
"component": "Layout",
"redirect": "/profile/index",
"hidden": true,
"alwaysShow": true,
"name": "router-name",
"meta": {
"noCache": true,
"affix": true,
"breadcrumb": false,
"activeMenu": "/example/list"
},
"children": []
}
后端路由表中单个路由应该具有的信息
{
"path": "/profile",
"name": "router-name",
"meta": {
"title": "Profile",
"roles": ["admin", "editor"],
"icon": "user"
},
"children": []
}
*/
/* Layout */
import Layout from '@/layout'
......@@ -11,6 +41,7 @@ import componentsRouter from './components'
import chartsRouter from './charts'
import tableRouter from './table'
import nestedRouter from './nested'
import coreRouter from './core'
/**
* Note: sub-menu only appear when route children.length >= 1
......@@ -38,7 +69,7 @@ import nestedRouter from './nested'
* the routes that need to be dynamically loaded based on user roles
* 用来匹配后台生成的路由表,根据路由名称
*/
export const asyncRoutesMap = [
let asyncRoutes = [
{
path: '/permission',
component: Layout,
......@@ -252,32 +283,7 @@ export const asyncRoutesMap = [
// 404 page must be placed at the end !!!
{ path: '*', redirect: '/404', hidden: true }
]
/*
前端路由映射表中单个路由映射全部具有的信息
{
"path": "/profile",
"component": "Layout",
"redirect": "/profile/index",
"hidden": true,
"alwaysShow": true,
"name": "router-name",
"meta": {
"noCache": true,
"affix": true,
"breadcrumb": false,
"activeMenu": "/example/list"
},
"children": []
}
后端路由表中单个路由应该具有的信息
{
"path": "/profile",
"name": "router-name",
"meta": {
"title": "Profile",
"roles": ["admin", "editor"],
"icon": "user"
},
"children": []
}
*/
const asyncRoutesMap = [...coreRouter, ...asyncRoutes]
export default asyncRoutesMap
import { constantRoutes } from '@/router'
import { getRoutes } from '@/api/role'
import { asyncRoutesMap } from '@/router/maps/index'
import { deepClone, objectMerge } from '@/utils/index'
import { default as asyncRoutesMap } from '@/router/maps/index'
import { deepClone, objectMerge, isNotNullAndNotUndefined } from '@/utils/index'
/**
* Use meta.role to determine if the current user has permission
......@@ -20,26 +20,35 @@ function hasPermission(roles, route) {
* Filter asynchronous routing tables by recursion
* 通过前端保留的路由映射表来生成路由表
* 将前端路由表对应的路由覆盖至后端路由中,以后端路由表为主
* @param routes asyncRoutes
* @param routesMap 前端路由映射表
* @param routes 后端路由表
* @param roles 后台获取的个人用户信息携带的roles
*/
export function filterAsyncRoutes(routesMap, routes, roles) {
let resRoutes = []
for (let route of routes) {
// 对象展开符也常用于浅拷贝
// 前端路由表
let tempRoute = { ...route }
// 后端路由表
let tempRouteMap
// 从前端路由表中选出与当前后端路由信息相对应的那条路由信息
for (let rm of routesMap) {
if ( !rm.path || !route.path) {
console.error(`检查路由表中 ${rm.name} 的path信息,path必须在同级唯一`)
continue
}
if (rm.path === route.path) {
if (
isNotNullAndNotUndefined(rm.path) &&
isNotNullAndNotUndefined(route.path) &&
rm.path === route.path
) {
tempRouteMap = { ...rm }
break
} else {
// 在开发时期可以看到路由表的残缺,生产环境中建议用另外的日志记录器,或者统一删除console语句
console.error(
`【name:${route.name},path:${route.path}】前后端路由信息不相符`
)
}
}
debugger
if (tempRouteMap && hasPermission(roles, tempRoute)) {
if (tempRoute.children) {
tempRoute.children = filterAsyncRoutes(
......@@ -57,7 +66,6 @@ export function filterAsyncRoutes(routesMap, routes, roles) {
resRoutes.push(tempRouteMap)
}
}
debugger
return resRoutes
}
......@@ -80,11 +88,13 @@ const actions = {
.then(response => {
let accessedRoutes,
asyncRoutes = response.data
console.log(asyncRoutesMap)
accessedRoutes = filterAsyncRoutes(
deepClone(asyncRoutesMap),
asyncRoutes,
roles
)
debugger
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
......
import { isUndefined, isNull } from 'lodash'
/**
* Created by PanJiaChen on 16/11/18.
*/
......@@ -17,10 +19,10 @@ export function parseTime(time, cFormat) {
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
if (typeof time === 'number' && time.toString().length === 10) {
time = time * 1000
}
date = new Date(time)
......@@ -37,7 +39,9 @@ export function parseTime(time, cFormat) {
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['', '', '', '', '', '', ''][value ] }
if (key === 'a') {
return ['', '', '', '', '', '', ''][value]
}
if (result.length > 0 && value < 10) {
value = '0' + value
}
......@@ -119,7 +123,7 @@ export function byteLength(str) {
const code = str.charCodeAt(i)
if (code > 0x7f && code <= 0x7ff) s++
else if (code > 0x7ff && code <= 0xffff) s += 2
if (code >= 0xDC00 && code <= 0xDFFF) i--
if (code >= 0xdc00 && code <= 0xdfff) i--
}
return s
}
......@@ -197,7 +201,7 @@ export function objectMerge(target, source) {
}
Object.keys(source).forEach(property => {
const sourceProperty = source[property]
if (typeof sourceProperty === 'object') {
if (!isNull(sourceProperty) && typeof sourceProperty === 'object') {
target[property] = objectMerge(target[property], sourceProperty)
} else {
target[property] = sourceProperty
......@@ -348,3 +352,11 @@ export function removeClass(ele, cls) {
ele.className = ele.className.replace(reg, ' ')
}
}
export function isNullOrUndefined(obj) {
return obj !== void 0 || isUndefined(obj) || isNull(obj)
}
export function isNotNullAndNotUndefined(obj) {
return obj !== void 0 && !isUndefined(obj) && !isNull(obj)
}
......@@ -23,7 +23,7 @@ export default {
])
},
created() {
if (!this.roles.includes('admin')) {
if (!this.roles.includes(1)) {
this.currentRole = 'editorDashboard'
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment