Explorar o código

页面路径变了的问题

ZGC4846 hai 5 días
pai
achega
85ac990a23

+ 4 - 1
README.md

@@ -18,6 +18,7 @@ npm run dev
 | `publicPath`(静态资源目录 / entry) | `/eos` | `/hr` |
 | 独立运行 router `base` | `/eos/` | `/hr/` |
 | 乾坤 router `base` / activeRule | `/page-eos/` | `/page-hr/` |
+| WT 内示例地址 | `/page-eos/home/index` | `/page-hr/home/index` |
 
 ## qiankun 注册信息
 
@@ -33,4 +34,6 @@ npm run dev
 
 开发联调时 `entry` 可改为 `http://localhost:8091/hr/`。
 
-**重要**:构建产物需部署到网关的 `/hr/` 目录,不要部署到 `/page-hr/`。
+**重要**:
+- 构建产物部署到网关 **`/hr/`**(不要放到 `/page-hr/`)
+- 门户顶栏「人力资源系统」打开后,地址栏应为 **`/page-hr/xxxx`**(例如 `/page-hr/home/index`)

+ 12 - 0
src/App.vue

@@ -9,6 +9,18 @@ export default {
   name: 'App',
   created() {
     this.$store.dispatch('theme/recoverTheme');
+    // 独立运行打开根路径时清掉历史脏页签,避免白屏时还显示旧面包屑
+    if (!window.__POWERED_BY_QIANKUN__) {
+      const path = this.$route?.path;
+      if (
+        !path ||
+        path === '/' ||
+        path === '/home' ||
+        path === '/home/index'
+      ) {
+        this.$store.dispatch('theme/tabRemoveAll');
+      }
+    }
   },
 };
 </script>

+ 1 - 1
src/config/menus.js

@@ -4,7 +4,7 @@
  */
 export const DEFAULT_MENUS = [
   {
-    path: '/home',
+    path: '/home/index',
     component: 'homeView/index',
     meta: { title: '首页', icon: 'el-icon-house' },
   },

+ 7 - 3
src/config/setting.js

@@ -1,7 +1,11 @@
 /** 接口基础地址 */
 export const API_BASE_URL = process.env.VUE_APP_API_BASE_URL;
 
-/** 应用及系统标识(与门户 activeRule `/page-hr`、资源目录 `/hr` 对齐,同 EOM 的 eos) */
+/**
+ * 应用及系统标识(对照 EOM):
+ * - 静态资源 / entry:`/hr`(同 EOM 的 `/eos`)
+ * - WT 内浏览器路径:`/page-hr/xxxx`(同 EOM 的 `/page-eos/home/index`)
+ */
 export const PROJECT_NAME = process.env.VUE_APP_NAME || 'HR';
 export const SYSTEM_NAME = 'hr';
 
@@ -32,9 +36,9 @@ export const KEEP_ALIVE_EXCLUDES = [];
  */
 export const USER_MENUS = undefined;
 
-/** 首页名称 / 路径 */
+/** 首页名称 / 路径(与 EOM 菜单一致:WT 内为 /page-hr/home/index) */
 export const HOME_TITLE = '首页';
-export const HOME_PATH = '/home';
+export const HOME_PATH = '/home/index';
 
 /** 接口/旧菜单可能用的首页 path,统一归一到 HOME_PATH */
 export const HOME_PATH_ALIASES = [

+ 28 - 14
src/router/index.js

@@ -17,9 +17,9 @@ import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
 
 Vue.use(VueRouter);
 
-// 与 EOM/WT 一致:
-// - 乾坤嵌入:base = /page-{SYSTEM_NAME}/(对应主应用 activeRule
-// - 独立运行:base = /{SYSTEM_NAME}/(对应 publicPath /hr
+// 与 EOM 一致:
+// - WT 内浏览器路径:/page-hr/xxxx(同 page-eos/home/index
+// - 独立运行:/hr/xxxx(同 /eos/,对应 publicPath
 const routerOptions = {
   base: window.__POWERED_BY_QIANKUN__
     ? `/page-${SYSTEM_NAME}/`
@@ -56,7 +56,7 @@ function registerMenuRoutes(menus, homePath, authoritiesRouter = []) {
   router.addRoute(
     getMenuRoutes(
       [...nextMenus, ...nextAuth],
-      homePath || HOME_PATH || '/home',
+      homePath || HOME_PATH || '/home/index',
     ),
   );
   dynamicRoutesReady = true;
@@ -72,17 +72,19 @@ router.beforeEach((to, from, next) => {
     NProgress.start();
   }
 
+  const homePath = HOME_PATH || '/home/index';
+
   if (getToken()) {
     // 已登录访问登录页:直接进首页,避免停在空白态
     if (to.path === '/login') {
-      next({ path: HOME_PATH || '/home', replace: true });
+      next({ path: homePath, replace: true });
       return;
     }
 
-    // 旧首页别名统一跳到 /home,避免再开「首页」可关闭页签
-    if (isHomeAliasPath(to.path) && to.path !== (HOME_PATH || '/home')) {
+    // 旧首页别名统一跳到 /home/index,避免再开「首页」可关闭页签
+    if (isHomeAliasPath(to.path) && to.path !== homePath) {
       next({
-        path: HOME_PATH || '/home',
+        path: homePath,
         query: to.query,
         hash: to.hash,
         replace: true,
@@ -93,14 +95,18 @@ router.beforeEach((to, from, next) => {
     if (store.state.user.menus == null) {
       store
         .dispatch('user/fetchUserInfo')
-        .then(({ menus, homePath, authoritiesRouter }) => {
-          // 与 EOM 一致:有菜单则注册;无菜单也要挂布局+首页,并标记已加载避免死循环
+        .then(({ menus, homePath: menuHome, authoritiesRouter }) => {
           registerMenuRoutes(
             menus || [],
-            homePath || HOME_PATH || '/home',
+            menuHome || homePath,
             authoritiesRouter || [],
           );
-          next({ ...to, replace: true });
+          // 根路径必须落到首页,否则只有布局、子路由空白
+          const targetPath =
+            to.path === LAYOUT_PATH || to.path === '/'
+              ? homePath
+              : to.fullPath;
+          next({ path: targetPath, replace: true });
         })
         .catch((e) => {
           console.error(e);
@@ -117,10 +123,18 @@ router.beforeEach((to, from, next) => {
     if (!dynamicRoutesReady) {
       registerMenuRoutes(
         store.state.user.menus,
-        HOME_PATH || '/home',
+        homePath,
         store.state.user.authoritiesRouter || [],
       );
-      next({ ...to, replace: true });
+      const targetPath =
+        to.path === LAYOUT_PATH || to.path === '/' ? homePath : to.fullPath;
+      next({ path: targetPath, replace: true });
+      return;
+    }
+
+    // 动态路由已就绪时,访问 / 仍强制进首页
+    if (to.path === LAYOUT_PATH || to.path === '/') {
+      next({ path: homePath, replace: true });
       return;
     }
 

+ 13 - 4
src/store/modules/theme.js

@@ -16,7 +16,7 @@ import {
   isHomeAliasPath,
 } from '@/config/setting';
 
-const HOME_ROUTE = HOME_PATH || '/home';
+const HOME_ROUTE = HOME_PATH || '/home/index';
 
 function isHomeTabKey(key) {
   if (!key) return false;
@@ -94,9 +94,13 @@ export default {
     const state = { ...DEFAULT_STATE };
     const cache = getCacheSetting();
     Object.keys(state).forEach((key) => {
+      // 不从本地恢复页签:刷新后容易残留旧路径面包屑(如资质证照/新增)
+      if (key === 'tabs' || key === 'homeComponents' || key === 'routeReload') {
+        return;
+      }
       if (typeof cache[key] !== 'undefined') state[key] = cache[key];
     });
-    state.tabs = withoutHomeTabs(state.tabs || []);
+    state.tabs = [];
     return state;
   })(),
   getters: {
@@ -221,6 +225,7 @@ export default {
     },
     tabRemoveAll({ commit }) {
       commit('SET', { key: 'tabs', value: [] });
+      cacheSetting('tabs', []);
       return Promise.resolve({ home: true });
     },
     tabSetItem({ state, commit }, data) {
@@ -257,8 +262,12 @@ export default {
       commit('SET', { key: 'styleResponsive', value });
       cacheSetting('styleResponsive', value);
     },
-    recoverTheme() {
-      // 已在 state 初始化时从缓存恢复
+    recoverTheme({ commit, state }) {
+      // 清理与当前地址无关的脏页签,避免面包屑残留「资质证照/新增」等旧路径
+      const cache = getCacheSetting();
+      const tabs = withoutHomeTabs(cache.tabs || state.tabs || []);
+      commit('SET', { key: 'tabs', value: tabs });
+      cacheSetting('tabs', tabs);
     },
   },
 };

+ 2 - 2
src/store/modules/user.js

@@ -126,7 +126,7 @@ export default {
         commit('setAuthoritiesRouter', nextAuth);
         return {
           menus: nextMenus,
-          homePath: HOME_PATH || homePath || '/home',
+          homePath: HOME_PATH || homePath || '/home/index',
           authoritiesRouter: nextAuth,
         };
       }
@@ -140,7 +140,7 @@ export default {
         commit('setAuthoritiesRouter', []);
         return {
           menus: nextMenus,
-          homePath: HOME_PATH || local.homePath || '/home',
+          homePath: HOME_PATH || local.homePath || '/home/index',
           authoritiesRouter: [],
         };
       }

+ 1 - 1
src/utils/menu-rewrite.js

@@ -68,7 +68,7 @@ function isHomeMenu(menu) {
 
 /**
  * 递归改写菜单树中的旧模块
- * - 首页 path 统一为 /home,侧栏仍显示「首页」
+ * - 首页 path 统一为 /home/index(WT 内完整地址 /page-hr/home/index),侧栏仍显示「首页」
  * - 顶部只走小房子,不另开可关闭页签
  */
 export function rewriteLegacyHrMenus(menus = []) {

+ 2 - 1
src/utils/request.js

@@ -44,7 +44,8 @@ function markParentHandledToast(notified) {
 
 /** 跳转登录页,独立运行和 qiankun 嵌入运行分别处理。 */
 function redirectToLogin(from) {
-  const isHome = !from || from === '/' || from === '/home';
+  const isHome =
+    !from || from === '/' || from === '/home' || from === '/home/index';
   removeToken();
   try {
     const store = require('@/store').default;

+ 5 - 2
src/views/login/index.vue

@@ -114,10 +114,13 @@
         ) {
           return from;
         }
-        return HOME_PATH || '/home';
+        return HOME_PATH || '/home/index';
       },
       goAfterLogin(path) {
-        const base = String(this.$router.options.base || '/hr/').replace(
+        const base = String(
+          this.$router.options.base ||
+            (window.__POWERED_BY_QIANKUN__ ? '/page-hr/' : '/hr/'),
+        ).replace(
           /\/$/,
           '',
         );