diff --git a/.env.dev b/.env.dev index 48233d54..1a3e9c4f 100644 --- a/.env.dev +++ b/.env.dev @@ -10,8 +10,11 @@ VUE_APP_TITLE = 洛玻集团驾驶舱 # VUE_APP_BASE_API = 'http://172.16.33.83:7070' # 杨姗姗 -# VUE_APP_BASE_API = 'http://172.16.20.218:7070' -VUE_APP_BASE_API = 'http://172.16.19.232:7070' +VUE_APP_BASE_API = 'http://172.16.20.218:7070' +# 小田 +# VUE_APP_BASE_API = 'http://172.16.19.232:7070' +# 测试 +# VUE_APP_BASE_API = 'http://192.168.0.35:8080' # 路由懒加载 diff --git a/src/layout/components/Sidebar/Logo.vue b/src/layout/components/Sidebar/Logo.vue index a8327b2e..3f1b7120 100644 --- a/src/layout/components/Sidebar/Logo.vue +++ b/src/layout/components/Sidebar/Logo.vue @@ -1,14 +1,14 @@ diff --git a/src/permission.js b/src/permission.js index 301fee65..6c4d46dc 100644 --- a/src/permission.js +++ b/src/permission.js @@ -8,6 +8,38 @@ import { isRelogin } from '@/utils/request' NProgress.configure({ showSpinner: false }) +/** + * 从菜单树中查找 jumpFlag===1 的默认跳转路径 + * 若打标在父节点则取其下第一个可见叶子路径;若在叶子则直接返回该路径 + */ +function findDefaultPathFromMenus(menus) { + if (!Array.isArray(menus) || menus.length === 0) return null + + function dfs(list, parentPath = '') { + for (const item of list) { + if (item.visible === false) continue + const rawPath = item.path || '' + const currentPath = rawPath.startsWith('/') + ? rawPath + : `${parentPath}/${rawPath}`.replace(/\/+/g, '/') + + if (item.jumpFlag === 1) { + if (item.children && item.children.length > 0) { + const childPath = dfs(item.children, currentPath) + return childPath != null ? childPath : currentPath + } + return currentPath + } + if (item.children && item.children.length > 0) { + const found = dfs(item.children, currentPath) + if (found != null) return found + } + } + return null + } + return dfs(menus) +} + // 增加三方登陆 update by 芋艿 const whiteList = ['/login', '/social-login', '/auth-redirect', '/bind', '/register', '/oauthLogin/gitee'] @@ -17,7 +49,7 @@ router.beforeEach((to, from, next) => { to.meta.title && store.dispatch('settings/setTitle', to.meta.title) /* has token*/ if (to.path === '/login') { - next({ path: '/' }) + next({ path: store.getters.defaultPath || '/' }) NProgress.done() } else { if (store.getters.roles.length === 0) { @@ -31,7 +63,13 @@ router.beforeEach((to, from, next) => { store.dispatch('GenerateRoutes', userInfo.menus).then(accessRoutes => { // 根据 roles 权限生成可访问的路由表 router.addRoutes(accessRoutes) // 动态添加可访问路由表 - next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 + const defaultPath = findDefaultPathFromMenus(userInfo.menus) || '/' + store.commit('permission/SET_DEFAULT_PATH', defaultPath) + if (to.path === '/' || to.matched.length === 0) { + next({ path: defaultPath, replace: true }) + } else { + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 + } }) }).catch(err => { store.dispatch('LogOut').then(() => { @@ -40,7 +78,12 @@ router.beforeEach((to, from, next) => { }) }) } else { - next() + if (to.path === '/') { + const defaultPath = store.getters.defaultPath || '/' + next({ path: defaultPath, replace: true }) + } else { + next() + } } } } else { diff --git a/src/store/getters.js b/src/store/getters.js index 79cdf2c3..d99cd120 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -17,6 +17,7 @@ const getters = { topbarRouters:state => state.permission.topbarRouters, defaultRoutes:state => state.permission.defaultRoutes, sidebarRouters:state => state.permission.sidebarRouters, + defaultPath: state => state.permission.defaultPath, // 数据字典 dict_datas: state => state.dict.dictDatas } diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js index f51fb238..b54b1531 100644 --- a/src/store/modules/permission.js +++ b/src/store/modules/permission.js @@ -11,8 +11,12 @@ const permission = { addRoutes: [], sidebarRouters: [], // 左侧边菜单的路由,被 Sidebar/index.vue 使用 topbarRouters: [], // 顶部菜单的路由,被 TopNav/index.vue 使用 + defaultPath: '/', // 登录后默认跳转路径(由菜单 jumpFlag===1 决定) }, mutations: { + SET_DEFAULT_PATH: (state, path) => { + state.defaultPath = path || '/' + }, SET_ROUTES: (state, routes) => { state.addRoutes = routes state.routes = constantRoutes.concat(routes)