|
@@ -1,12 +1,11 @@
|
|
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
|
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
|
-import { dynamicRoutes,errorRouter } from "./module/dynamicRoutes";
|
|
|
|
|
|
+import { dynamicRoutes, errorRouter } from "./module/dynamicRoutes";
|
|
import { globalRoutes } from "./module/globalRoutes";
|
|
import { globalRoutes } from "./module/globalRoutes";
|
|
import { useStore } from 'vuex'
|
|
import { useStore } from 'vuex'
|
|
-
|
|
|
|
-
|
|
|
|
-const routes: Array<RouteRecordRaw> = [
|
|
|
|
- ...dynamicRoutes,
|
|
|
|
- ...globalRoutes
|
|
|
|
|
|
+import { menuList } from "@/api";
|
|
|
|
+import { fork } from 'child_process';
|
|
|
|
+const routes: any = [
|
|
|
|
+ ...globalRoutes,
|
|
]
|
|
]
|
|
|
|
|
|
const router = createRouter({
|
|
const router = createRouter({
|
|
@@ -14,39 +13,58 @@ const router = createRouter({
|
|
routes
|
|
routes
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+function initDynamicRouter() {
|
|
|
|
+ return new Promise(resolve => {
|
|
|
|
+ menuList({}).then((res: any) => {
|
|
|
|
+ if (res.code == 200 && res.msg == '查询成功') {
|
|
|
|
+ resolve(res?.data || [])
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 设置标题
|
|
* 设置标题
|
|
*/
|
|
*/
|
|
router.afterEach((to, from) => {
|
|
router.afterEach((to, from) => {
|
|
document.title = '宝智达科技 - ' + to.meta.title;
|
|
document.title = '宝智达科技 - ' + to.meta.title;
|
|
})
|
|
})
|
|
-
|
|
|
|
-
|
|
|
|
-router.beforeEach((to, from, next) => {
|
|
|
|
|
|
+router.beforeEach(async (to, from, next) => {
|
|
const token = sessionStorage.getItem('token')
|
|
const token = sessionStorage.getItem('token')
|
|
const store = useStore()
|
|
const store = useStore()
|
|
- if(to.path=='/')store.commit('setCompany','')
|
|
|
|
- if(to.path!='/'){
|
|
|
|
- store.commit('addTab', {
|
|
|
|
- title: to.meta.title,
|
|
|
|
- path: to.path,
|
|
|
|
- name: to.name
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- if (token) {//直接放行
|
|
|
|
- next()
|
|
|
|
- } else {
|
|
|
|
- //如果是登录页面路径,就直接next()
|
|
|
|
- if (to.path === '/') {
|
|
|
|
- next();
|
|
|
|
|
|
+ if (store.state.setMenu.length == 0) {//当前动态导航为空,调取
|
|
|
|
+ if (to.path == '/') {//登录界面直接放行
|
|
|
|
+ next()
|
|
} else {
|
|
} else {
|
|
- next('/');
|
|
|
|
|
|
+ const reslut: any = await initDynamicRouter()
|
|
|
|
+ const dynamicRouter = Fn(reslut)
|
|
|
|
+ routes[1].children = dynamicRouter
|
|
|
|
+ dynamicRouter.forEach((element:any) => {
|
|
|
|
+ router.addRoute(element)
|
|
|
|
+ });
|
|
|
|
+ store.commit('setMenuFun', reslut) //存储用户个人信息
|
|
|
|
+ return next({ ...to, replace: true })
|
|
}
|
|
}
|
|
- }
|
|
|
|
- if(to.name=='login'){//直接进入了登录,清除token
|
|
|
|
- sessionStorage.setItem('token','')
|
|
|
|
- store.commit('setIsUserInfo', null) //存储用户个人信息
|
|
|
|
|
|
+ } else {//当前存在动态导航
|
|
|
|
+ if (to.name == 'login') {//直接进入了登录,清除token
|
|
|
|
+ sessionStorage.setItem('token', '')
|
|
|
|
+ store.commit('setIsUserInfo', null) //存储用户个人信息
|
|
|
|
+ store.commit('setCompany', '')
|
|
|
|
+ }
|
|
|
|
+ next()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * 递归导航
|
|
|
|
+ * @param arr
|
|
|
|
+ * @returns
|
|
|
|
+ */
|
|
|
|
+function Fn(arr: any) {
|
|
|
|
+ for (let j = 0; j < arr.length; j++) {
|
|
|
|
+ arr[j].component = () => import(`${arr[j].component}`)
|
|
|
|
+ if(arr[j].children && arr[j].children.length) Fn(arr[j].children)
|
|
|
|
+ }
|
|
|
|
+ return arr
|
|
|
|
+}
|
|
export default router
|
|
export default router
|