Files
dvcp_v2_webapp/bin/build.js
aixianling b7c0350134 feat(config): 添加自定义首页配置并优化导航功能
-增加自定义首页配置项,支持设置不同的首页组件
-优化导航功能,添加固定首页到导航栏
- 重构路由生成逻辑,支持自定义签到页和首页
- 更新组件以适应新的导航结构
2024-12-23 14:19:00 +08:00

100 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require('axios')
const {fsExtra, copyFiles, findApp, chalkTag, fs} = require("./tools");
const compiler = require('vue-template-compiler')
const getBuildConfig = id => {
axios.post('http://192.168.1.87:12525/node/custom/detail', null, {params: {id}}).then(res => {
if (res?.data) {
const config = res.data.data
fsExtra.outputJson('src/config.json', config.extra)
createPages(config)
}
})
}
const getAppInfo = (file, apps) => {
if (/[\\\/](App[A-Z][^\\\/]+)\.vue$/g.test(file)) {
const name = file.replace(/.+[\\\/](App[^\\\/]+)\.vue$/, '$1'),
source = fs.readFileSync(file).toString(),
parsed = compiler.parseComponent(source),
script = parsed.script?.content || "",
label = script.match(/label:[^,]+/)?.[0]?.replace(/.+["']([^"']+).+/, '$1')
const paths = file.split(/[\\\/]/)
apps.push({
id: file.replace(/\.vue$/, '').replace(/[\\\/]/g, '_'),
label: label || name,
path: `/${file.replace(/\.vue$/, '').replace(/[\\\/]/g, '/')}`,
workspace: paths.at(0),
esm: file.replace(/[\\\/]/g, '/').substring(4),
name
})
}
}
/**
* 根据配置生成应用路由
* @param {Object} config - 配置对象,用于定制化路由生成过程
* @returns {Promise} - 返回一个Promise对象表示路由生成完成
*/
const createRoutes = (config = {}) => {
// 初始化路由数组
const routes = []
// 获取签到页面的路径,如果未指定,则使用默认路径
let signPage = '../views/sign'
let {signPage: sign, homePage: home = "console"} = config.extra || {}
if (config.extra?.signPage) {
signPage = `../apps/custom/${sign}/${sign}`
}
let homePage = `../views/console`
if (config.extra?.homePage) {
homePage = `../apps/custom/${home}/${home}`
}
// 查找并处理所有应用,将它们的信息添加到路由中
return findApp("src/apps", app => getAppInfo(app, routes)).then(() => {
// 生成并输出apps.js文件定义所有应用的路由
fsExtra.outputFile('src/utils/apps.js', `export default [
{path: "/login", name: "登录", component: () => import('${signPage}')},
{path: '/dv', name: '数据大屏入口', component: () => import('../views/dvIndex')},
{path: '/v', name: 'Home', component: () => import('../views/home'), children: [
{path:'/',name:'mainEntry', component:()=>import('../views/mainEntry'),children:[
{name: "工作台", path: "${home}", component: () => import('${homePage}')},
${routes.filter(e => ![sign, home].includes(e.name)).map(e => {
// 解构每个路由的属性,用于生成路由配置
const {name, label, esm} = e
// 生成单个路由配置的字符串表示
return `{name:"${name}",label:"${label}",path:"${name}",component:()=>import("../${esm}")}`
}).join(',\n')},
{path: '*',name: '404',component: ()=>import('../views/building')},
]}
]},
{path: '/', name: "init"},
]`)
// 扫描完毕使用chalkTag标记任务完成
chalkTag.done("扫描完毕")
})
}
const createPages = (config = {}) => {
fsExtra.emptyDir("src/apps", err => {
if (!err) {
const {customPath, appList} = config
const stdApps = {}
appList.filter(e => !/project/.test(e.id))?.forEach(e => {
const paths = e.libPath.split('/').filter(Boolean) || []
paths.pop()
stdApps[paths.join("/")] = 1
})
Promise.all([
copyFiles("src/apps/core", "packages/core"),
copyFiles("src/apps/custom", `project/${customPath}`),
...Object.keys(stdApps).map(e => copyFiles(`src/apps/${e.replace(/^packages[\\\/]/, '')}`, e)),
]).then(() => createRoutes(config)).then(() => fsExtra.ensureFile("src/apps/actions.js"))
}
})
}
const start = () => {
const buildId = process.argv[2] || process.env.VUE_APP_OMS_ID || 'f670cc46-7cf7-4a0f-86ee-3077044c0b17'
getBuildConfig(buildId)
}
start()