feat(utils): 重构路由生成逻辑
- 移除 autoRoutes.js 文件,改为使用 apps.js 存储路由信息 - 新增 createRoutes 函数,用于动态生成应用路由 - 更新 build.js,增加路由生成步骤 - 修改 router.js,使用新的路由配置 - 更新 .gitignore,忽略新的 apps.js 文件
This commit is contained in:
@@ -6,7 +6,6 @@ import utils from './utils';
|
||||
import vcUI from 'dui/packages';
|
||||
import appComp from 'dui/dv';
|
||||
import store from './utils/store';
|
||||
import autoRoutes from "./utils/autoRoutes";
|
||||
import extra from "./config.json"
|
||||
import axios from "./utils/axios";
|
||||
//备注底座信息,勿删
|
||||
@@ -19,7 +18,7 @@ Vue.config.productionTip = false;
|
||||
Vue.prototype.$cdn = "https://cdn.cunwuyun.cn"
|
||||
Vue.prototype.$request = axios
|
||||
Object.keys(utils).map((e) => (Vue.prototype[e] = utils[e]));
|
||||
const loadPage = () => autoRoutes.init().finally(() => new Vue({router, store, render: h => h(App)}).$mount("#app"))
|
||||
const loadPage = () => new Vue({router, store, render: h => h(App)}).$mount("#app")
|
||||
let theme = null
|
||||
store.dispatch('getSystem', extra.sysInfo).then(res => {
|
||||
theme = JSON.parse(res?.colorScheme || null)
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
import {waiting} from "./index";
|
||||
import router from "./router";
|
||||
import store from "./store";
|
||||
import {Message} from "element-ui";
|
||||
import Vue from "vue";
|
||||
import extra from "../config.json"
|
||||
|
||||
let {state: {user}, commit, dispatch} = store
|
||||
const signOut = () => commit("signOut"),
|
||||
getUserInfo = () => dispatch("getUserInfo"),
|
||||
existRoute = route => {
|
||||
return router.getRoutes()?.find(e => e.name == route?.name || e.path == route?.path)
|
||||
},
|
||||
goto = (route, next) => {
|
||||
const exist = !!existRoute(route)
|
||||
return exist ? route.name ? next() : router.replace(route) :
|
||||
!route.name && route.path == "/" ? router.replace({name: "Home"}).catch(() => 0) :
|
||||
Message.error("无法找到路由,请联系系统管理员!")
|
||||
}
|
||||
const loadApps = () => {
|
||||
//新App的自动化格式
|
||||
waiting.init({innerHTML: '应用加载中..'})
|
||||
let apps = require.context('../apps', true, /\.(\/.+)\/App[A-Z][^\/]+\.vue$/, "lazy")
|
||||
return Promise.all(apps.keys().map(path => apps(path).then(file => {
|
||||
if (file.default) {
|
||||
let {name} = file.default
|
||||
waiting.setContent(`加载${name}...`)
|
||||
Vue.component(name, file.default)
|
||||
} else return 0
|
||||
}))).then(() => {
|
||||
waiting.setContent(`正在进入系统...`)
|
||||
setTimeout(() => waiting.close(), 1000)
|
||||
})
|
||||
}
|
||||
const addHome = homePage => {
|
||||
const component = extra?.homePage || homePage.path
|
||||
if (extra?.homePage && Vue.component(component)) {
|
||||
homePage = {...homePage, path: component, component: () => import('../views/mainEntry'), meta: component}
|
||||
}
|
||||
router.addRoute('Home', homePage)
|
||||
router.options.routes[2].children.unshift(homePage)
|
||||
commit("setHomePage", {
|
||||
...homePage,
|
||||
label: homePage.name,
|
||||
id: `/v/${component}`,
|
||||
isMenu: 1,
|
||||
route: homePage.name,
|
||||
component,
|
||||
path: component,
|
||||
})
|
||||
}
|
||||
const generateRoutes = (to, from, next) => {
|
||||
if (router.options.routes[2].children.length > 0) {
|
||||
goto(to, next)
|
||||
} else {
|
||||
Promise.all([getUserInfo(), loadApps()]).then(() => {
|
||||
//初始化默认工作台
|
||||
let homePage = {name: "工作台", path: "console", style: "iconfont iconNav_Dashborad", component: () => import('../views/console')}
|
||||
addHome(homePage)
|
||||
const mods = user.info.menuSet?.filter(e => !!e.component)?.map(e => ({route: e.id, ...e}))
|
||||
mods?.map(({route: name, path, component}) => {
|
||||
if (!!Vue.component(component) && path && !existRoute({name})) {
|
||||
let search = path.split("?")
|
||||
path = search?.[0] || path
|
||||
const route = {name, path, component: () => import('../views/mainEntry'), meta: component}
|
||||
router.addRoute('Home', route)
|
||||
router.options.routes[2].children.push(route)
|
||||
}
|
||||
})
|
||||
to.name == "Home" ? next({name: homePage.name, replace: true}) : next({...to, replace: true})
|
||||
}).then(() => commit("setRoutes", router.options.routes[2].children))
|
||||
}
|
||||
}
|
||||
export const routes = [
|
||||
{path: "/login", name: "登录", component: () => import('../views/sign')},
|
||||
{path: '/dv', name: '数据大屏入口', component: () => import('../views/dvIndex')},
|
||||
{path: '/v', name: 'Home', component: () => import('../views/home'), children: []},
|
||||
{path: '/', name: "init"},
|
||||
]
|
||||
export default {
|
||||
init: () => {
|
||||
router.beforeEach((to, from, next) => {
|
||||
console.log('%s=>%s', from.name, to.name)
|
||||
if (to.hash == "#pddv") {
|
||||
const {query} = to
|
||||
dispatch("getToken", {
|
||||
username: "18971406276",
|
||||
password: "admin321!"
|
||||
}).then(() => next({name: "数据大屏入口", query, hash: "#dv"}))
|
||||
} else if (["数据大屏入口", "登录"].includes(to.name)) {
|
||||
next()
|
||||
} else if (to.hash == "#dv") {
|
||||
//数据大屏进行的独立页面跳转
|
||||
let {query, hash} = to
|
||||
next({name: "数据大屏入口", query, hash})
|
||||
} else if (user.token) {
|
||||
to.name == "init" ? next({name: "Home"}) : generateRoutes(to, from, next)
|
||||
} else {
|
||||
signOut()
|
||||
}
|
||||
})
|
||||
router.onError(err => {
|
||||
console.error(err)
|
||||
})
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import Vue from 'vue'
|
||||
import VueRouter from 'vue-router'
|
||||
import {routes} from "./autoRoutes"
|
||||
import routes from "./apps.js"
|
||||
import config from "../config.json"
|
||||
import store from "@/utils/store";
|
||||
|
||||
const {state: {user}, commit, dispatch} = store
|
||||
const signOut = () => commit("signOut")
|
||||
Vue.use(VueRouter)
|
||||
export default new VueRouter({
|
||||
const router = new VueRouter({
|
||||
base: config.base || '/',
|
||||
mode: 'history',
|
||||
hashbang: false,
|
||||
@@ -17,3 +20,29 @@ export default new VueRouter({
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
console.log('%s=>%s', from.name, to.name)
|
||||
if (to.hash == "#pddv") {
|
||||
const {query} = to
|
||||
dispatch("getToken", {
|
||||
username: "18971406276",
|
||||
password: "admin321!"
|
||||
}).then(() => next({name: "数据大屏入口", query, hash: "#dv"}))
|
||||
} else if (["数据大屏入口", "登录"].includes(to.name)) {
|
||||
next()
|
||||
} else if (to.hash == "#dv") {
|
||||
//数据大屏进行的独立页面跳转
|
||||
let {query, hash} = to
|
||||
next({name: "数据大屏入口", query, hash})
|
||||
} else if (user.token) {
|
||||
to.name == "init" ? next({name: "Home"}) : next()
|
||||
} else {
|
||||
signOut()
|
||||
}
|
||||
})
|
||||
router.onError(err => {
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user