import { createStore } from "vuex"; import createPersistedState from "vuex-persistedstate"; import { logOutProc } from "../../resources/api/logOut" let globalStore = null; // 전역 스토어 설정 함수 export function setGlobalStore(store) { globalStore = store; } // 전역 스토어 호출 함수 export function getGlobalStore() { return globalStore; } export default function createAppStore(ctx, strgMode, lgnMode) { const store = createStore({ plugins: [createPersistedState({ storage: strgMode === 'S' ? window.sessionStorage : window.localStorage, paths: [ 'loginMode', // J, S 'policyMode', // Y, N 'authorization', 'mbrId', 'mbrNm', 'roles', 'contextPath', 'pageAuth' ] })], state: { authorization: null, loginMode: lgnMode || 'J', // J, S policyMode: 'Y', // Y, N (기본값: 허용) userType: "portal", menu: null, path: null, roles: [{authority: "ROLE_NONE"}], pageAuth: null, contextPath: ctx || null, }, getters: { getAuthorization: function () {}, getMbrNm: function () {}, getRoles: function () {}, getLoginMode: state => state.loginMode, getPolicyMode: state => state.policyMode, // 로그인 상태 확인 isLoggedIn: (state) => { if (state.loginMode === 'J') { return !!state.authorization && !!state.mbrNm; } else if (state.loginMode === 'S') { return !!state.mbrNm; } return false; }, }, mutations: { setAuthorization(state, newValue) { state.authorization = newValue; }, setMbrNm(state, newValue) { state.mbrNm = newValue; }, setMbrId(state, newValue) { state.mbrId = newValue; }, setRoles(state, newValue) { state.roles = newValue; }, setUserType(state, newValue) { state.userType = newValue; }, setMenu(state, newValue) { state.menu = newValue; }, setPath(state, newValue) { state.path = newValue; }, setStoreReset(state) { state.authorization = null; state.loginMode = 'J'; state.policyMode = 'Y'; state.mbrNm = null; state.mbrId = null; state.roles = [{authority: "ROLE_NONE"}]; state.menu = null; state.pageAuth = null; state.contextPath = null; }, setPageAuth(state, newValue) { state.pageAuth = newValue; }, setMenuList(state, menuList) { state.menuList = menuList; // 메뉴트리 펼치기 const flattenMenus = (menus) => { const result = []; for (const menu of menus) { result.push(menu); if (menu.childList?.length) { result.push(...flattenMenus(menu.childList)); } } return result; } const flattenedMenuList = flattenMenus(menuList); state.flatMenuList = flattenedMenuList; }, setContextPath(state, ctx) { state.contextPath = ctx; }, setLoginMode(state, value) { state.loginMode = value; }, setPolicyMode(state, value) { state.policyMode = value; }, // 로그인 정보 일괄 설정 setLoginInfo(state, { mbrNm, mbrId, roles, authorization, loginMode, policyMode }) { state.mbrNm = mbrNm; state.mbrId = mbrId; state.roles = roles || [{authority: "ROLE_NONE"}]; if (loginMode) { state.loginMode = loginMode; } if (policyMode) { state.policyMode = policyMode; } // JWT 모드일 때만 authorization 저장 if (state.loginMode === 'J' && authorization) { state.authorization = authorization; } else if (state.loginMode === 'S') { state.authorization = null; // 세션 모드에서는 토큰 불필요 } }, }, actions: { async logout({ commit, state, dispatch }) { const ctx = state.contextPath; const admPath = state.path?.includes("/adm"); try { const res = await logOutProc(); if (res.status == 200) { commit("setStoreReset"); localStorage.clear(); sessionStorage.clear(); // 페이지 이동 const loginUrl = admPath ? (ctx || '') + "/cmslogin.page" : (ctx || '') + "/login.page"; window.location.href = loginUrl; } } catch(error) { commit("setStoreReset"); localStorage.clear(); sessionStorage.clear(); const loginUrl = admPath ? (ctx || '') + "/cmslogin.page" : (ctx || '') + "/login.page"; window.location.href = loginUrl; } }, setUserType({ commit }, userType) { commit("setUserType", userType); }, setMenu({ commit }, menu) { commit("setMenu", menu); }, setPath({ commit }, path) { commit("setPath", path); }, setPageAuth({ commit }, pageAuth) { commit("setPageAuth", pageAuth); }, setStoreReset({commit}) { commit("setStoreReset"); }, }, }); setGlobalStore(store); return store; }