|
|
@@ -10,135 +10,101 @@ import {
|
|
|
LAYOUT_PATH,
|
|
|
SYSTEM_NAME,
|
|
|
HOME_PATH,
|
|
|
- isHomeAliasPath,
|
|
|
} from '@/config/setting';
|
|
|
import store from '@/store';
|
|
|
import { getToken, setToken, getCurrentUser } from '@/utils/token-util';
|
|
|
import { routes, getMenuRoutes } from './routes';
|
|
|
import { changeRole } from '@/api/layout';
|
|
|
-import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
|
|
|
|
|
|
Vue.use(VueRouter);
|
|
|
|
|
|
-/**
|
|
|
- * 乾坤:/page-hr/(同 EOM 的 /page-eos/)
|
|
|
- * 独立:/hr/(同 EOM 的 /eos/)
|
|
|
- */
|
|
|
-export function getAppRouterBase() {
|
|
|
- return window.__POWERED_BY_QIANKUN__
|
|
|
+const router = new VueRouter({
|
|
|
+ // 与 EOM 一致:乾坤 /page-hr/ ,独立 /hr/
|
|
|
+ base: window.__POWERED_BY_QIANKUN__
|
|
|
? `/page-${SYSTEM_NAME}/`
|
|
|
- : `/${SYSTEM_NAME}/`;
|
|
|
+ : `/${SYSTEM_NAME}/`,
|
|
|
+ routes,
|
|
|
+ mode: 'history',
|
|
|
+ scrollBehavior() {
|
|
|
+ return { y: 0 };
|
|
|
+ },
|
|
|
+});
|
|
|
+
|
|
|
+export function getAppRouterBase() {
|
|
|
+ return router.options.base;
|
|
|
}
|
|
|
|
|
|
-function createRouter() {
|
|
|
- return new VueRouter({
|
|
|
- base: getAppRouterBase(),
|
|
|
+export function resetRouter() {
|
|
|
+ const newRouter = new VueRouter({
|
|
|
+ base: window.__POWERED_BY_QIANKUN__
|
|
|
+ ? `/page-${SYSTEM_NAME}/`
|
|
|
+ : `/${SYSTEM_NAME}/`,
|
|
|
routes,
|
|
|
mode: 'history',
|
|
|
scrollBehavior() {
|
|
|
return { y: 0 };
|
|
|
},
|
|
|
});
|
|
|
-}
|
|
|
-
|
|
|
-const router = createRouter();
|
|
|
-
|
|
|
-let dynamicRoutesReady = false;
|
|
|
-
|
|
|
-export function resetRouter() {
|
|
|
- const newRouter = createRouter();
|
|
|
router.matcher = newRouter.matcher;
|
|
|
router.options.base = newRouter.options.base;
|
|
|
- dynamicRoutesReady = false;
|
|
|
-}
|
|
|
-
|
|
|
-function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
|
|
|
- const nextMenus = rewriteLegacyHrMenus(menus || []);
|
|
|
- const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
|
|
|
- store.commit('user/setMenus', nextMenus);
|
|
|
- store.commit('user/setAuthoritiesRouter', nextAuth);
|
|
|
- if (nextMenus.length || nextAuth.length) {
|
|
|
- router.addRoute(
|
|
|
- getMenuRoutes(
|
|
|
- [...nextMenus, ...nextAuth],
|
|
|
- homePath || HOME_PATH || '/home/index',
|
|
|
- ),
|
|
|
- );
|
|
|
- }
|
|
|
- dynamicRoutesReady = true;
|
|
|
-}
|
|
|
-
|
|
|
-export function setupDynamicRoutes(menus, homePath, authoritiesRouter = []) {
|
|
|
- resetRouter();
|
|
|
- registerMenuRoutes(menus, homePath, authoritiesRouter);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 路由守卫(对齐 EOM)
|
|
|
+ */
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
if (!from.path.includes(REDIRECT_PATH)) {
|
|
|
NProgress.start();
|
|
|
}
|
|
|
|
|
|
if (getToken()) {
|
|
|
- const homePath = HOME_PATH || '/home/index';
|
|
|
-
|
|
|
- if (isHomeAliasPath(to.path) && to.path !== homePath) {
|
|
|
- next({
|
|
|
- path: homePath,
|
|
|
- query: to.query,
|
|
|
- hash: to.hash,
|
|
|
- replace: true,
|
|
|
- });
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 与 EOM:menus 未加载时拉取;有菜单才 addRoute
|
|
|
- if (store.state.user.menus == null) {
|
|
|
+ // 还未注册动态路由则先获取
|
|
|
+ if (!store.state.user.menus) {
|
|
|
store
|
|
|
.dispatch('user/fetchUserInfo')
|
|
|
- .then(({ menus, homePath: menuHome, authoritiesRouter }) => {
|
|
|
- if (menus?.length || authoritiesRouter?.length) {
|
|
|
- registerMenuRoutes(
|
|
|
- menus || [],
|
|
|
- menuHome || homePath,
|
|
|
- authoritiesRouter || [],
|
|
|
+ .then(({ menus, homePath, authoritiesRouter }) => {
|
|
|
+ if (menus) {
|
|
|
+ router.addRoute(
|
|
|
+ getMenuRoutes(
|
|
|
+ [...menus, ...(authoritiesRouter || [])],
|
|
|
+ homePath || HOME_PATH,
|
|
|
+ ),
|
|
|
);
|
|
|
+ next({ ...to, replace: true });
|
|
|
} else {
|
|
|
- // 标记已尝试,避免死循环;乾坤下侧栏仍由门户管
|
|
|
- store.commit('user/setMenus', menus || []);
|
|
|
- dynamicRoutesReady = true;
|
|
|
+ // 与 EOM:无菜单时仍放行,避免卡死;侧栏由门户按 /page-hr 维护
|
|
|
+ next();
|
|
|
}
|
|
|
- next({ ...to, replace: true });
|
|
|
})
|
|
|
.catch((e) => {
|
|
|
console.error(e);
|
|
|
next();
|
|
|
});
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!dynamicRoutesReady && store.state.user.menus?.length) {
|
|
|
- registerMenuRoutes(
|
|
|
- store.state.user.menus,
|
|
|
- homePath,
|
|
|
- store.state.user.authoritiesRouter || [],
|
|
|
- );
|
|
|
- next({ ...to, replace: true });
|
|
|
- return;
|
|
|
+ } else {
|
|
|
+ // 热更新等导致动态路由丢失时补注册
|
|
|
+ if (routes.length >= router.getRoutes()?.length) {
|
|
|
+ router.addRoute(
|
|
|
+ getMenuRoutes(
|
|
|
+ [
|
|
|
+ ...store.state.user.menus,
|
|
|
+ ...(store.state.user.authoritiesRouter || []),
|
|
|
+ ],
|
|
|
+ HOME_PATH,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ next({ ...to });
|
|
|
+ } else {
|
|
|
+ next();
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+ } else if (WHITE_LIST.includes(to.path)) {
|
|
|
next();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (WHITE_LIST.includes(to.path)) {
|
|
|
- next();
|
|
|
- return;
|
|
|
+ } else {
|
|
|
+ next({
|
|
|
+ path: '/login',
|
|
|
+ query: to.path === LAYOUT_PATH ? {} : { from: to.path },
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
- next({
|
|
|
- path: '/login',
|
|
|
- query: to.path === LAYOUT_PATH ? {} : { from: to.path },
|
|
|
- });
|
|
|
});
|
|
|
|
|
|
router.afterEach((to) => {
|
|
|
@@ -151,12 +117,16 @@ router.afterEach((to) => {
|
|
|
|
|
|
router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
|
|
|
const currentUser = getCurrentUser();
|
|
|
- if (menus?.length) {
|
|
|
- resetRouter();
|
|
|
- registerMenuRoutes(menus, homePath, authoritiesRouter);
|
|
|
- const target = HOME_PATH || menus[0].redirect || menus[0].path;
|
|
|
- if (router.currentRoute.path !== target) {
|
|
|
- await router.replace({ path: target });
|
|
|
+ if (menus && menus.length > 0) {
|
|
|
+ router.addRoute(
|
|
|
+ getMenuRoutes([...menus, ...(authoritiesRouter || [])], homePath),
|
|
|
+ );
|
|
|
+ if (
|
|
|
+ router.currentRoute.path != (menus[0].redirect || menus[0].path)
|
|
|
+ ) {
|
|
|
+ await router.replace({
|
|
|
+ path: menus[0].redirect || menus[0].path,
|
|
|
+ });
|
|
|
}
|
|
|
const newToken = await changeRole({
|
|
|
groupId: currentUser.currentGroupId,
|