|
@@ -2,9 +2,9 @@ import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
|
|
import { dynamicRoutes, errorRouter } from "./module/dynamicRoutes";
|
|
|
import { globalRoutes } from "./module/globalRoutes";
|
|
|
import { useStore } from 'vuex'
|
|
|
-import { menuList } from "@/api";
|
|
|
-import { fork } from 'child_process';
|
|
|
-import { toRaw } from "vue";
|
|
|
+import stores from '@/store'
|
|
|
+
|
|
|
+import { menuList, postEnter } from "@/api";
|
|
|
const routes: any = [
|
|
|
...globalRoutes,
|
|
|
]
|
|
@@ -23,6 +23,21 @@ function initDynamicRouter() {
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
+/**
|
|
|
+ * 进入公司
|
|
|
+ */
|
|
|
+export const enterCompanyFun = async(data: any)=>{
|
|
|
+ const reslut: any = await postEnter({ id: data.id })
|
|
|
+ if (reslut.code == 200) {
|
|
|
+ stores.commit('setCompany', data.deptName) //存储公司
|
|
|
+ const dynamicRouter = Fn(reslut.data)
|
|
|
+ dynamicRouter.forEach((item: any) => {
|
|
|
+ router.addRoute('layout', item)
|
|
|
+ });
|
|
|
+ stores.commit('setMenuFun', [...dynamicRouter]) //存储用户个人信息
|
|
|
+ router.push('/home')
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* 设置标题
|
|
@@ -31,24 +46,24 @@ router.afterEach((to, from) => {
|
|
|
document.title = '宝智达科技 - ' + to.meta.title;
|
|
|
})
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
- const token = sessionStorage.getItem('token')
|
|
|
+ // console.log('to, from', to, from)
|
|
|
const store = useStore()
|
|
|
if (store.state.setMenu.length == 0) {//当前动态导航为空,调取
|
|
|
- if (to.path == '/') {//登录界面直接放行
|
|
|
- next()
|
|
|
- } else {
|
|
|
+ if (to.path != '/') {//登录界面直接放行
|
|
|
const reslut: any = await initDynamicRouter()
|
|
|
const dynamicRouter = Fn(reslut)
|
|
|
- routes[1].children = [...dynamicRouter]
|
|
|
- console.log('返回',dynamicRouter)
|
|
|
- dynamicRouter.forEach((item:any) => {
|
|
|
- router.addRoute(item)
|
|
|
+ // routes[1].children = [...dynamicRouter]
|
|
|
+ dynamicRouter.forEach((item: any) => {
|
|
|
+ router.addRoute('layout', item)
|
|
|
});
|
|
|
- store.commit('setMenuFun', dynamicRouter) //存储用户个人信息
|
|
|
- return next({ ...to, replace: true })
|
|
|
+ store.commit('setMenuFun', [...dynamicRouter]) //存储用户个人信息
|
|
|
+ next({ path: to.path, replace: true })// 登录之后跳转页面使用 replace 避免用户回退到 login 页面
|
|
|
+ } else {
|
|
|
+ next()
|
|
|
}
|
|
|
} else {//当前存在动态导航
|
|
|
if (to.name == 'login') {//直接进入了登录,清除token
|
|
|
+ store.commit('setMenuFun', []) //存储用户个人信息
|
|
|
sessionStorage.setItem('token', '')
|
|
|
store.commit('setIsUserInfo', null) //存储用户个人信息
|
|
|
store.commit('setCompany', '')
|
|
@@ -62,11 +77,13 @@ router.beforeEach(async (to, from, next) => {
|
|
|
* @param arr
|
|
|
* @returns
|
|
|
*/
|
|
|
-function Fn(arr: any) {
|
|
|
+export 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)
|
|
|
+ // arr[j].component = (resolve:any) => require([`@/${arr[j].component}`], resolve(null))
|
|
|
+ arr[j].component = () => import('../views' + arr[j].path + '/index.vue')
|
|
|
+ if (arr[j].children && arr[j].children.length) Fn(arr[j].children)
|
|
|
}
|
|
|
- return toRaw(arr)
|
|
|
+ return arr
|
|
|
}
|
|
|
+
|
|
|
export default router
|