Files
dvcp_v2_webapp/examples/router/autoRoutes.js

50 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2021-12-14 18:36:19 +08:00
import store from "../store";
2022-04-27 18:18:57 +08:00
import appEntry from "../views/appEntry";
import {waiting} from "../utils";
2022-04-27 19:36:32 +08:00
import router from "./router";
import axios from "./axios";
2021-12-14 18:36:19 +08:00
export default {
2022-04-27 18:18:57 +08:00
routes: [],
2021-12-14 18:36:19 +08:00
init() {
//约束正则式
store.commit("cleanApps")
2022-04-27 18:18:57 +08:00
this.routes = []
2021-12-14 18:36:19 +08:00
// 自动化本工程应用
2022-04-27 18:18:57 +08:00
return this.loadApps()
2021-12-14 18:36:19 +08:00
},
loadApps() {
//新App的自动化格式
2022-08-17 10:50:56 +08:00
let apps = require.context('../../packages/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy'),
projects = require.context('../../project/', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, 'lazy')
const promise = (mods, base) => Promise.all(mods.keys().map(path => mods(path).then(file => {
2022-05-10 16:54:21 +08:00
if (file.default) {
let {name, label} = file.default,
2021-12-14 18:36:19 +08:00
addApp = {
name: path.replace(/\.\/?(vue)?/g, '')?.split("/").join("_"), label: label || name,
2022-08-17 10:50:56 +08:00
path: `/${base}${path.replace(/\.(\/.+\/App.+)\.vue$/, '$1')}`,
component: appEntry,
2022-05-10 16:54:21 +08:00
module: file.default
2021-12-14 18:36:19 +08:00
}
2022-05-10 16:54:21 +08:00
waiting.setContent(`加载${name}...`)
router.addRoute(addApp)
2021-12-14 18:36:19 +08:00
//命名规范入口文件必须以App开头
2022-05-10 16:54:21 +08:00
return store.commit("addApp", addApp)
} else return 0
2022-08-17 10:50:56 +08:00
})))
waiting.init({innerHTML: '应用加载中..'})
Promise.all([
promise(apps, "packages"),
promise(projects, "project")
]).then(() => {
axios.post("/node/wechatapps/addOrUpdate", {
2022-06-08 16:32:01 +08:00
type: "web",
list: this.routes().map(({path: libPath, label, module: {name}, name: id}) => ({
id, type: 'web', libPath, label, name
}))
}, {baseURL: "/ns"}).catch(() => 0)
waiting.close()
})
2021-12-14 18:36:19 +08:00
}
}