以下代码用于有菜单关系的文本结构转换为代码对应的结构。
__
两个下划线后面,二级菜单使用 -
表示,目前仅支持二级菜单,望好心人改改,支持多级菜单,例如三级菜单使用 --
表示。
const str = `
合规平台 __ Compliance Platform
任务管理 __ task management
监管法规库 __ Regulatory repository
合规报告 __ Compliance Report
监管落实 __ Supervision and Implementation
-法规列表 __ - List of Regulations
-调研列表 __ - Research List
-自查列表 __ - self-checking list
-通知列表 __ - Notification List
-授权文件 __ - Authorization document
合规测试 __ Compliance testing
数据报表 __ Data Report
-统计报表 __ - Statistical Statement
-动态报表 __ - Dynamic Report
-Matrix报表 __ - Matrix Report
系统配置 __ system configuration
-到期提醒管理 __ - Management of expiration reminders
-公告管理 __ - Announcement Management
-邮件模版管理 __ - Mail template management
-分行管理 __ - Branch management
-码值管理 __ - Code Value Management
-业务分类 __ - Business Classification
-部门管理 __ - Departmental management
-群组管理 __ - Group Management
系统管理 __ system management
-角色管理 __ - Role management
-日志报表 __ - Log Report
-邮件发送日志 __ - Mail Send Log
`
function getMemuJson(str) {
const arr = str.split(‘\n‘).map(item => item.trim()).filter(item => item) // 拆分每行
let manMenuIndex = 0 // 主菜单索引
let subMenuIndex = 0 // 子菜单索引
let letterIndex = 65 // 从字母 A 编码开始
const menuRes = []
arr.forEach(name => {
if(!name.match(/^-/)) {
subMenuIndex = 0
menuRes.push({
id: String.fromCharCode(letterIndex + manMenuIndex),
name: getEnOrCh(name, ‘ch‘),
route: `/${tf(getEnOrCh(name))}`,
})
manMenuIndex = manMenuIndex + 1
} else { // 子菜单
menuRes.push({
id: String.fromCharCode(letterIndex + (manMenuIndex - 1)) + (subMenuIndex + 1),
name: getEnOrCh(name, ‘ch‘),
route: `/${tf(getEnOrCh(arr[manMenuIndex - 1]))}/${tf(getEnOrCh(name))}`,
})
subMenuIndex = subMenuIndex + 1
}
})
return menuRes
function getEnOrCh(str, isEn = ‘en‘) { // 截取每行上的英文或中文
return isEn === ‘en‘ ? str.match(/__ (.*)/)[1].replace(/^-+/, ‘‘).trim() : str.match(/(.*)__/)[1].replace(/^-+/, ‘‘)
}
function tf(str){ // 转换 `ab-c d_ef` 为小驼峰
let arr=str.split(‘ ‘).join(‘-‘).split(‘-‘).join(‘_‘).split(‘_‘);
for(let i=1; i<arr.length; i++){
arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substring(1);
}
arr[0] = arr[0].toLowerCase() // 此行为小驼峰
return arr.join(‘‘);
};
}
function getShell(menuRes) {
// 简单生成脚本, 用于根据基准页面创建各个路由的页面。注意 window 下的脚本不是这样的
const basePage = `/Users/xw/Documents/git/boilerplate/clients/src/pages/basePage/` // 基准页面, 我这里是一个文件夹
const outDir = `/Users/xw/Documents/git/boilerplate/clients/src/pages/` // 输出的位置, 我这里与基准页面在同一目录
let bat = menuRes.map(item => {
const out = `${outDir}${item.route}`
return `mkdir -p ${out} & cp -r ${basePage} ${out}`.replace(/\/\//g, ‘/‘)
}).join(‘\r\n\r\n‘)
return bat
}
function getAuth(str) {
let obj = {}
getMemuJson(str).map(item => item.id).forEach(item => {
if(!obj[item[0]]) {
obj[item[0]] = [item]
} else {
obj[item[0]].push(item)
}
})
return Object.keys(obj).map(key => obj[key])
}
console.log(‘生成的菜单数据‘, getMemuJson(str)) // 配置在代码里
console.log(‘生成的管理员菜单配置‘, getAuth(str)) // 配置在数据库
console.log(‘生成的脚本‘, getShell(getMemuJson(str))) // 创建与配置对应的文件结构
[ { id: ‘A‘, name: ‘合规平台 ‘, route: ‘/compliancePlatform‘ },
{ id: ‘B‘, name: ‘任务管理 ‘, route: ‘/taskManagement‘ },
{ id: ‘C‘, name: ‘监管法规库 ‘, route: ‘/regulatoryRepository‘ },
{ id: ‘D‘, name: ‘合规报告 ‘, route: ‘/complianceReport‘ },
{ id: ‘E‘,
name: ‘监管落实 ‘,
route: ‘/supervisionAndImplementation‘ },
{ id: ‘E1‘,
name: ‘法规列表 ‘,
route: ‘/supervisionAndImplementation/listOfRegulations‘ },
{ id: ‘E2‘,
name: ‘调研列表 ‘,
route: ‘/supervisionAndImplementation/researchList‘ },
{ id: ‘E3‘,
name: ‘自查列表 ‘,
route: ‘/supervisionAndImplementation/selfCheckingList‘ },
{ id: ‘E4‘,
name: ‘通知列表 ‘,
route: ‘/supervisionAndImplementation/notificationList‘ },
{ id: ‘E5‘,
name: ‘授权文件 ‘,
route: ‘/supervisionAndImplementation/authorizationDocument‘ },
{ id: ‘F‘, name: ‘合规测试 ‘, route: ‘/complianceTesting‘ },
{ id: ‘G‘, name: ‘数据报表 ‘, route: ‘/dataReport‘ },
{ id: ‘G1‘,
name: ‘统计报表 ‘,
route: ‘/researchList/statisticalStatement‘ },
{ id: ‘G2‘, name: ‘动态报表 ‘, route: ‘/researchList/dynamicReport‘ },
{ id: ‘G3‘,
name: ‘Matrix报表 ‘,
route: ‘/researchList/matrixReport‘ },
{ id: ‘H‘, name: ‘系统配置 ‘, route: ‘/systemConfiguration‘ },
{ id: ‘H1‘,
name: ‘到期提醒管理 ‘,
route: ‘/selfCheckingList/managementOfExpirationReminders‘ },
{ id: ‘H2‘,
name: ‘公告管理 ‘,
route: ‘/selfCheckingList/announcementManagement‘ },
{ id: ‘H3‘,
name: ‘邮件模版管理 ‘,
route: ‘/selfCheckingList/mailTemplateManagement‘ },
{ id: ‘H4‘,
name: ‘分行管理 ‘,
route: ‘/selfCheckingList/branchManagement‘ },
{ id: ‘H5‘,
name: ‘码值管理 ‘,
route: ‘/selfCheckingList/codeValueManagement‘ },
{ id: ‘H6‘,
name: ‘业务分类 ‘,
route: ‘/selfCheckingList/businessClassification‘ },
{ id: ‘H7‘,
name: ‘部门管理 ‘,
route: ‘/selfCheckingList/departmentalManagement‘ },
{ id: ‘H8‘,
name: ‘群组管理 ‘,
route: ‘/selfCheckingList/groupManagement‘ },
{ id: ‘I‘, name: ‘系统管理 ‘, route: ‘/systemManagement‘ },
{ id: ‘I1‘,
name: ‘角色管理 ‘,
route: ‘/notificationList/roleManagement‘ },
{ id: ‘I2‘, name: ‘日志报表 ‘, route: ‘/notificationList/logReport‘ },
{ id: ‘I3‘,
name: ‘邮件发送日志 ‘,
route: ‘/notificationList/mailSendLog‘ } ]
[ [ ‘A‘ ],
[ ‘B‘ ],
[ ‘C‘ ],
[ ‘D‘ ],
[ ‘E‘, ‘E1‘, ‘E2‘, ‘E3‘, ‘E4‘, ‘E5‘ ],
[ ‘F‘ ],
[ ‘G‘, ‘G1‘, ‘G2‘, ‘G3‘ ],
[ ‘H‘, ‘H1‘, ‘H2‘, ‘H3‘, ‘H4‘, ‘H5‘, ‘H6‘, ‘H7‘, ‘H8‘ ],
[ ‘I‘, ‘I1‘, ‘I2‘, ‘I3‘ ] ]
mkdir -p /Users/xw/Documents/git/boilerplate/clients/src/pages/compliancePlatform & cp -r /Users/xw/Documents/git/boilerplate/clients/src/pages/basePage/ /Users/xw/Documents/git/boilerplate/clients/src/pages/compliancePlatform
mkdir -p /Users/xw/Documents/git/boilerplate/clients/src/pages/taskManagement & cp -r /Users/xw/Documents/git/boilerplate/clients/src/pages/basePage/ /Users/xw/Documents/git/boilerplate/clients/src/pages/taskManagement
mkdir -p /Users/xw/Documents/git/boilerplate/clients/src/pages/regulatoryRepository & cp -r /Users/xw/Documents/git/boilerplate/clients/src/pages/basePage/ /Users/xw/Documents/git/boilerplate/clients/src/pages/regulatoryRepository
# 更多省略...
原文:https://www.cnblogs.com/daysme/p/11477002.html