137 lines
4.4 KiB
TypeScript
137 lines
4.4 KiB
TypeScript
import { usePageDataStore } from '#layers/stores/usePageDataStore'
|
|
import { useLoadingStore } from '#layers/stores/useLoadingStore'
|
|
import { commonFetch } from '#layers/utils/apiUtil'
|
|
import { getGameDomain, getPathAfterLanguage } from '#layers/utils/urlUtil'
|
|
import type { PageDataResponse } from '#layers/types/api/pageData'
|
|
|
|
export default defineNuxtRouteMiddleware(async (to, _from) => {
|
|
const runtimeConfig = useRuntimeConfig()
|
|
|
|
// server에서는 실행X -----
|
|
if (import.meta.server) return
|
|
|
|
// error 페이지는 실행X -----
|
|
if (to.path.includes('/error')) return
|
|
|
|
// inspection 페이지는 실행X -----
|
|
if (to.path.includes('/inspection')) return
|
|
|
|
const gameDataStore = useGameDataStore()
|
|
const pageDataStore = usePageDataStore()
|
|
const loadingStore = useLoadingStore()
|
|
|
|
const { langCodes } = storeToRefs(gameDataStore)
|
|
|
|
const stoveApiBaseUrl = runtimeConfig.public.stoveApiUrl
|
|
const accessToken = csrGetAccessToken()
|
|
const gameDomain = getGameDomain()
|
|
const pathWithoutLocale = getPathAfterLanguage(to.path)
|
|
const langCode = csrGetFinalLocale(to.path, langCodes.value) || 'ko'
|
|
|
|
let pageDataResponse: PageDataResponse | null = null
|
|
|
|
try {
|
|
// 페이지 이동 시 로딩 상태 시작
|
|
loadingStore.startFullLoading()
|
|
|
|
const headers = {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
}
|
|
|
|
let queryParams: Record<string, string>
|
|
let apiUrl: string
|
|
|
|
if (pathWithoutLocale === '/preview') {
|
|
// 미리보기 쿼리스트링에서 파라미터 값 추출
|
|
// preview?page_seq=1&page_ver=1&lang_code=ko
|
|
const queryString = to.fullPath.includes('?')
|
|
? to.fullPath.split('?')[1]
|
|
: ''
|
|
const urlParams = new URLSearchParams(queryString)
|
|
const pageSeq = urlParams.get('page_seq') || ''
|
|
const pageVer = urlParams.get('page_ver') || ''
|
|
const queryLangCode = urlParams.get('lang_code') || langCode
|
|
|
|
apiUrl = `${stoveApiBaseUrl}/pub-comm/v1.0/template/page/preview`
|
|
queryParams = {
|
|
lang_code: queryLangCode,
|
|
page_seq: pageSeq,
|
|
page_ver: pageVer,
|
|
_t: Date.now().toString(), // 캐시 무효화를 위한 타임스탬프
|
|
}
|
|
} else {
|
|
apiUrl = `${stoveApiBaseUrl}/pub-comm/v2.0/template/page`
|
|
queryParams = {
|
|
game_domain: gameDomain,
|
|
lang_code: langCode,
|
|
page_url: pathWithoutLocale,
|
|
_t: Date.now().toString(), // 캐시 무효화를 위한 타임스탬프
|
|
}
|
|
}
|
|
|
|
pageDataResponse = (await commonFetch('GET', apiUrl, {
|
|
headers,
|
|
query: queryParams,
|
|
})) as PageDataResponse | null
|
|
|
|
console.log('🚀 ~ pageData.global response:', pageDataResponse)
|
|
} catch (error) {
|
|
pageDataStore.clearPageData()
|
|
console.error(error)
|
|
return showError(
|
|
createError({
|
|
statusCode: error.statusCode,
|
|
statusMessage: error.message,
|
|
fatal: false, // 즉시 에러 페이지로
|
|
data: { reason: 'post-not-found' },
|
|
})
|
|
)
|
|
}
|
|
|
|
if (pageDataResponse?.code === 0 && 'value' in pageDataResponse) {
|
|
pageDataStore.setPageData(pageDataResponse.value)
|
|
} else {
|
|
pageDataStore.clearPageData()
|
|
// 90001 (API Respond 4xx status): API 응답 4xx 에러
|
|
// 91001 (Invalid GameCode): 게임 코드 없음
|
|
// 91003 (Invalid PageUrl): 페이지 주소 없음
|
|
if (
|
|
pageDataResponse?.code === 90001 ||
|
|
pageDataResponse?.code === 91001 ||
|
|
pageDataResponse?.code === 91003
|
|
) {
|
|
return showError(
|
|
createError({
|
|
statusCode: 404,
|
|
statusMessage: pageDataResponse?.message,
|
|
fatal: false, // 즉시 에러 페이지로
|
|
data: {
|
|
reason: pageDataResponse?.message,
|
|
},
|
|
})
|
|
)
|
|
}
|
|
// 91002 (Invalid LangCode): 미지원 언어로 접근
|
|
if (pageDataResponse?.code === 91002) {
|
|
// 이미 /home 경로에 있으면 무한 리다이렉트 방지
|
|
if (pathWithoutLocale === '/home') {
|
|
return createError({
|
|
statusCode: 404,
|
|
statusMessage: pageDataResponse?.message,
|
|
fatal: false, // 즉시 에러 페이지로
|
|
data: {
|
|
reason: pageDataResponse?.message,
|
|
},
|
|
})
|
|
}
|
|
return navigateTo(`/${langCode}/home`)
|
|
}
|
|
|
|
// [TODO]
|
|
// 90004 (Not found user): 사용자 없음
|
|
// if (pageDataResponse?.code === 90043) {
|
|
// return navigateTo(`/${langCode}/home`)
|
|
// }
|
|
}
|
|
})
|