import constant from './const'
export function getRouters (files) {
let filenames = files.keys()
let list = mapToList(filenames)
let routerArr = getTree(list)
return routerArr
} function getTree (list) {
let routers = []
list.map((obj, index) => {
// 当前菜单没有父菜单
if (!hasParent(obj)) {
let length = 0
let routerArr = findChild(obj, obj, list, length)
routers = routers.concat(routerArr)
return routers
}
})
return routers
} function hasParent (node) {
let arr = node.split('/')
if (arr.length > 1) {
return arr[arr.length - 2]
} else {
return null
}
} function findChild (node, path, list, length) {
let routerArr = []
let childrenArr = []
let name = node
if (endsWith(node, constant.end)) {
node = cleanAuth(node, constant.end)
}
list.map((obj, index) => {
let arr = obj.split('/')
if (node === arr[length]) {
if (arr.length === length + 2) {
arr.splice(0, length + 1)
let tempNode = arr.toString().replace(',', '/')
childrenArr = childrenArr.concat(findChild(tempNode, obj, list, length + 1))
}
return childrenArr
}
})
routerArr = packageRouter(name, path, routerArr, childrenArr)
return routerArr
} function packageRouter (name, route, routerArr, childrenArr) {
// path = path.toLocaleLowerCase()
// ../components 不能为第一个变量,否则会报错Cannot find module "."
let path = constant.prefix + '/' + route
if (endsWith(route, constant.end)) {
let tempRoute = cleanAuth(route, constant.end)
if (childrenArr === 'undefined' || childrenArr.length === 0) {
routerArr.push({
path: '/' + tempRoute,
name: tempRoute,
component: resolve => require([`@/${path}.vue`], resolve),
meta: {
requireAuth: true
}
})
} else {
routerArr.push({
path: '/' + tempRoute,
name: tempRoute,
component: resolve => require([`@/${path}.vue`], resolve),
meta: {
requireAuth: true
},
children: childrenArr
})
}
return routerArr
} else {
if (childrenArr === 'undefined' || childrenArr.length === 0) {
routerArr.push({
path: '/' + route,
name: route,
component: resolve => require([`@/${path}.vue`], resolve)
})
} else {
routerArr.push({
path: '/' + route,
name: route,
component: resolve => require([`@/${path}.vue`], resolve),
children: childrenArr
})
}
return routerArr
}
} function mapToList (filenames) {
let list = []
filenames.map((obj, index) => {
obj = obj.replace(/^\.\//, '').replace(/\.(vue)$/, '')
list.push(obj)
})
return list
} // 判断当前字符串是否以str开始
// function startsWith (str) {
// return this.slice(0, str.length) === str
// }
// 判断当前字符串是否以str结束
function endsWith (origin, str) {
if (!judgeStrLength) {
return false
}
return origin.slice(origin.length - str.length, origin.length) === str
} function cleanAuth (origin, str) {
if (!judgeStrLength) {
return origin
}
return origin.slice(0, origin.length - str.length)
} function judgeStrLength (origin, str) {
return origin.length - str.length > 0
} export default getRouters

最新文章

  1. 关于举办 2015年 Autodesk 助力云应用项目开发活动通知
  2. phpize建立php扩展 Cannot find config.m4
  3. 局域网内搭建git
  4. BZOJ2186 欧拉函数
  5. 什么是Ajax无刷新技术?
  6. CentOS7上GitHub/GitLab多帐号管理SSH Key
  7. sqlite使用blob类型存储/访问 结构体
  8. EMV规范 ---ISO7816 T=0协议的时间特性
  9. 快速的CDN加速服务
  10. 惊喜:opera换webkit内核后完美支持SDCH压缩协议
  11. SEO概念及SEO相关优化
  12. 081、Weave Scope 多主机监控(2019-04-29 周一)
  13. MySQL5.7 并行复制的学习
  14. FBV和CBV装饰器
  15. 在linux环境下搭建JDK+JAVA+Mysql,并完成jforum的安装
  16. Linux 的基本操作(系统用户及用户组的管理)
  17. [转] 基于NodeJS的前后端分离的思考与实践(五)多终端适配
  18. Windows 7中200M神秘隐藏分区
  19. Windows7安装nginx后,'nginx -t -c nginx.conf' 命令出现 “could not open error log file: CreateFile() "logs/error.log" failed” 错误的原因
  20. springMVC 简单应用

热门文章

  1. HDC2021技术分论坛:如何高效完成HarmonyOS分布式应用测试?
  2. 【JAVA今法修真】 第三章 关系非关系 redis法器
  3. Redis哨兵 部署和配置
  4. rpm-build方式制作rpm包
  5. input type="file"多图片上传
  6. <转>C/S架构分析
  7. 估计工期标识(Project)
  8. Asp.NetCore3.1开源项目升级为.Net6.0
  9. fcntl 加锁模块
  10. 好奇怪啊,如果邮箱JSON格式的字符串不是在一行上,那么转为JSON将转换不成功,估计是数据格式有问题吧