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 @@ ...@@ -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 */ /* Layout */
import Layout from '@/layout' import Layout from '@/layout'
...@@ -11,6 +41,7 @@ import componentsRouter from './components' ...@@ -11,6 +41,7 @@ import componentsRouter from './components'
import chartsRouter from './charts' import chartsRouter from './charts'
import tableRouter from './table' import tableRouter from './table'
import nestedRouter from './nested' import nestedRouter from './nested'
import coreRouter from './core'
/** /**
* Note: sub-menu only appear when route children.length >= 1 * Note: sub-menu only appear when route children.length >= 1
...@@ -38,7 +69,7 @@ import nestedRouter from './nested' ...@@ -38,7 +69,7 @@ import nestedRouter from './nested'
* the routes that need to be dynamically loaded based on user roles * the routes that need to be dynamically loaded based on user roles
* 用来匹配后台生成的路由表,根据路由名称 * 用来匹配后台生成的路由表,根据路由名称
*/ */
export const asyncRoutesMap = [ let asyncRoutes = [
{ {
path: '/permission', path: '/permission',
component: Layout, component: Layout,
...@@ -252,32 +283,7 @@ export const asyncRoutesMap = [ ...@@ -252,32 +283,7 @@ export const asyncRoutesMap = [
// 404 page must be placed at the end !!! // 404 page must be placed at the end !!!
{ path: '*', redirect: '/404', hidden: true } { path: '*', redirect: '/404', hidden: true }
] ]
/*
前端路由映射表中单个路由映射全部具有的信息 const asyncRoutesMap = [...coreRouter, ...asyncRoutes]
{
"path": "/profile", export default asyncRoutesMap
"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": []
}
*/
import { constantRoutes } from '@/router' import { constantRoutes } from '@/router'
import { getRoutes } from '@/api/role' import { getRoutes } from '@/api/role'
import { asyncRoutesMap } from '@/router/maps/index' import { default as asyncRoutesMap } from '@/router/maps/index'
import { deepClone, objectMerge } from '@/utils/index' import { deepClone, objectMerge, isNotNullAndNotUndefined } from '@/utils/index'
/** /**
* Use meta.role to determine if the current user has permission * Use meta.role to determine if the current user has permission
...@@ -20,26 +20,35 @@ function hasPermission(roles, route) { ...@@ -20,26 +20,35 @@ function hasPermission(roles, route) {
* Filter asynchronous routing tables by recursion * Filter asynchronous routing tables by recursion
* 通过前端保留的路由映射表来生成路由表 * 通过前端保留的路由映射表来生成路由表
* 将前端路由表对应的路由覆盖至后端路由中,以后端路由表为主 * 将前端路由表对应的路由覆盖至后端路由中,以后端路由表为主
* @param routes asyncRoutes * @param routesMap 前端路由映射表
* @param routes 后端路由表
* @param roles 后台获取的个人用户信息携带的roles * @param roles 后台获取的个人用户信息携带的roles
*/ */
export function filterAsyncRoutes(routesMap, routes, roles) { export function filterAsyncRoutes(routesMap, routes, roles) {
let resRoutes = [] let resRoutes = []
for (let route of routes) { for (let route of routes) {
// 对象展开符也常用于浅拷贝 // 对象展开符也常用于浅拷贝
// 前端路由表
let tempRoute = { ...route } let tempRoute = { ...route }
// 后端路由表
let tempRouteMap let tempRouteMap
// 从前端路由表中选出与当前后端路由信息相对应的那条路由信息
for (let rm of routesMap) { for (let rm of routesMap) {
if ( !rm.path || !route.path) { if (
console.error(`检查路由表中 ${rm.name} 的path信息,path必须在同级唯一`) isNotNullAndNotUndefined(rm.path) &&
continue isNotNullAndNotUndefined(route.path) &&
} rm.path === route.path
if (rm.path === route.path) { ) {
tempRouteMap = { ...rm } tempRouteMap = { ...rm }
break break
} else {
// 在开发时期可以看到路由表的残缺,生产环境中建议用另外的日志记录器,或者统一删除console语句
console.error(
`【name:${route.name},path:${route.path}】前后端路由信息不相符`
)
} }
} }
debugger
if (tempRouteMap && hasPermission(roles, tempRoute)) { if (tempRouteMap && hasPermission(roles, tempRoute)) {
if (tempRoute.children) { if (tempRoute.children) {
tempRoute.children = filterAsyncRoutes( tempRoute.children = filterAsyncRoutes(
...@@ -57,7 +66,6 @@ export function filterAsyncRoutes(routesMap, routes, roles) { ...@@ -57,7 +66,6 @@ export function filterAsyncRoutes(routesMap, routes, roles) {
resRoutes.push(tempRouteMap) resRoutes.push(tempRouteMap)
} }
} }
debugger
return resRoutes return resRoutes
} }
...@@ -80,11 +88,13 @@ const actions = { ...@@ -80,11 +88,13 @@ const actions = {
.then(response => { .then(response => {
let accessedRoutes, let accessedRoutes,
asyncRoutes = response.data asyncRoutes = response.data
console.log(asyncRoutesMap)
accessedRoutes = filterAsyncRoutes( accessedRoutes = filterAsyncRoutes(
deepClone(asyncRoutesMap), deepClone(asyncRoutesMap),
asyncRoutes, asyncRoutes,
roles roles
) )
debugger
commit('SET_ROUTES', accessedRoutes) commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes) resolve(accessedRoutes)
}) })
......
import { isUndefined, isNull } from 'lodash'
/** /**
* Created by PanJiaChen on 16/11/18. * Created by PanJiaChen on 16/11/18.
*/ */
...@@ -17,10 +19,10 @@ export function parseTime(time, cFormat) { ...@@ -17,10 +19,10 @@ export function parseTime(time, cFormat) {
if (typeof time === 'object') { if (typeof time === 'object') {
date = time date = time
} else { } else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
time = parseInt(time) time = parseInt(time)
} }
if ((typeof time === 'number') && (time.toString().length === 10)) { if (typeof time === 'number' && time.toString().length === 10) {
time = time * 1000 time = time * 1000
} }
date = new Date(time) date = new Date(time)
...@@ -37,7 +39,9 @@ export function parseTime(time, cFormat) { ...@@ -37,7 +39,9 @@ export function parseTime(time, cFormat) {
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key] let value = formatObj[key]
// Note: getDay() returns 0 on Sunday // Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['', '', '', '', '', '', ''][value ] } if (key === 'a') {
return ['', '', '', '', '', '', ''][value]
}
if (result.length > 0 && value < 10) { if (result.length > 0 && value < 10) {
value = '0' + value value = '0' + value
} }
...@@ -119,7 +123,7 @@ export function byteLength(str) { ...@@ -119,7 +123,7 @@ export function byteLength(str) {
const code = str.charCodeAt(i) const code = str.charCodeAt(i)
if (code > 0x7f && code <= 0x7ff) s++ if (code > 0x7f && code <= 0x7ff) s++
else if (code > 0x7ff && code <= 0xffff) s += 2 else if (code > 0x7ff && code <= 0xffff) s += 2
if (code >= 0xDC00 && code <= 0xDFFF) i-- if (code >= 0xdc00 && code <= 0xdfff) i--
} }
return s return s
} }
...@@ -197,7 +201,7 @@ export function objectMerge(target, source) { ...@@ -197,7 +201,7 @@ export function objectMerge(target, source) {
} }
Object.keys(source).forEach(property => { Object.keys(source).forEach(property => {
const sourceProperty = source[property] const sourceProperty = source[property]
if (typeof sourceProperty === 'object') { if (!isNull(sourceProperty) && typeof sourceProperty === 'object') {
target[property] = objectMerge(target[property], sourceProperty) target[property] = objectMerge(target[property], sourceProperty)
} else { } else {
target[property] = sourceProperty target[property] = sourceProperty
...@@ -348,3 +352,11 @@ export function removeClass(ele, cls) { ...@@ -348,3 +352,11 @@ export function removeClass(ele, cls) {
ele.className = ele.className.replace(reg, ' ') 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 { ...@@ -23,7 +23,7 @@ export default {
]) ])
}, },
created() { created() {
if (!this.roles.includes('admin')) { if (!this.roles.includes(1)) {
this.currentRole = 'editorDashboard' 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