하석형 하석형 04-02
250402 하석형 Context Path 반영
@771ac70d1beab517924435c764b360d61b2af264
client/resources/api/index.js
--- client/resources/api/index.js
+++ client/resources/api/index.js
@@ -8,8 +8,18 @@
   }
 });
 
+const excludeCtxUrls = [
+  '/admin/cntxtPth/findLatestCntxtPth.json' // Context Path 정보 호출
+]
+
 apiClient.interceptors.request.use(
   config => {
+    const excludeCtxUrl = excludeCtxUrls.some(url => config.url.includes(url));
+    const contextPath = store.state.contextPath || '';
+    if(contextPath != '/' && !excludeCtxUrl) {
+      config.url = contextPath + config.url; // 요청 시 Context Path 추가
+    }
+
     config.headers.Authorization = store.state.authorization; // 요청 시 AccessToken 추가
     return config;
   },
client/resources/api/router.js
--- client/resources/api/router.js
+++ client/resources/api/router.js
@@ -1,5 +1,9 @@
 import apiClient from "./index";
 
+export const getCntxtPth = () => {
+    return apiClient.get(`/admin/cntxtPth/findLatestCntxtPth.json`);
+}
+
 export const findAll = () => {
     return apiClient.post(`/sys/contsType/findByContsAuthrtSys.json`);
 }
client/views/pages/AppRouter.js
--- client/views/pages/AppRouter.js
+++ client/views/pages/AppRouter.js
@@ -21,9 +21,26 @@
   },
 ];
 
-import { findAll, accessCheck } from "../../resources/api/router";
+import { getCntxtPth, findAll, accessCheck } from "../../resources/api/router";
 import { save } from "../../resources/api/cntnStats";
 import { mdiConsoleLine } from "@mdi/js";
+
+// Context Path 정보 호출
+async function getContextPath() {
+  try {
+    const res = await getCntxtPth();
+    if (res.status == 200) {
+      const ctx = res.data.data || ""; // Context Path 정보
+      store.commit("setContextPath", ctx); // Context Path 정보 저장
+      return ctx;
+    }else {
+      store.commit("setStoreReset");
+      window.location = '/'
+    }
+  } catch (error) {
+    return [];
+  }
+}
 
 // 라우터 정보 호출
 async function fetchRoutes() {
@@ -44,6 +61,20 @@
   } catch (error) {
     return [];
   }
+}
+
+// Context Path를 라우터에 추가하는 함수
+function addContextPathToRoutes(routes, contextPath) {
+  return routes.map(route => {
+    const newRoute = {
+      ...route,
+      path: contextPath + route.path
+    };
+    // if (route.children && route.children.length > 0) {
+    //   newRoute.children = addContextPathToRoutes(route.children, contextPath);
+    // }
+    return newRoute;
+  });
 }
 
 // 접근 제어 확인
@@ -113,8 +144,11 @@
 }
 
 export default async function createAppRouter() {
+  const ctx = await getContextPath(); // DB에 적재된 Context Path 정보 호출
   const dynamicRoutes = await fetchRoutes(); // DB에 적재된 라우터 정보 호출
-  const newRoutes = beforeRoutes.concat(dynamicRoutes); // 기존 라우터 정보와 합치기
+  const prefixedBeforeRoutes = addContextPathToRoutes(beforeRoutes, ctx); // 기존 라우터 정보에 Context Path 추가
+  const prefixedDynamicRoutes = addContextPathToRoutes(dynamicRoutes, ctx); // DB에 적재된 라우터 정보에 Context Path 추가
+  const newRoutes = prefixedBeforeRoutes.concat(prefixedDynamicRoutes); // 기존 라우터 정보와 합치기
   const AppRouter = createRouter({
     history: createWebHistory(),
     routes: newRoutes,
client/views/pages/AppStore.js
--- client/views/pages/AppStore.js
+++ client/views/pages/AppStore.js
@@ -11,7 +11,8 @@
     menu: null,
     path: null,
     roles: [{authority: "ROLE_NONE"}],
-    pageAuth: null
+    pageAuth: null,
+    contextPath: null,
   },
   getters: {
     getAuthorization: function () {},
@@ -52,6 +53,7 @@
       state.roles = [{authority: "ROLE_NONE"}];
       state.menu = null;
       state.pageAuth = null;
+      state.contextPath = null;
     },
     setPageAuth(state, newValue) {
       state.pageAuth = newValue;
@@ -59,6 +61,9 @@
     setMenuList(state, menuList) {
       state.menuList = menuList;
     },
+    setContextPath(state, ctx) {
+      state.contextPath = ctx;
+    }
   },
   actions: {
     async logout({ commit }) {
@@ -101,6 +106,6 @@
     },
     setStoreReset({commit}) {
       commit("setStoreReset");
-    }
+    },
   },
 });
Add a comment
List