70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { commonFetch } from '#layers/utils/apiUtil'
|
|
import { usePageDataStore } from '#layers/stores/usePageDataStore'
|
|
import { useGetGameDomain } from '#layers/composables/useGetGameDomain'
|
|
import { usePathResolver } from '#layers/composables/usePathResolver'
|
|
import type { PageDataResponse } from '#layers/types/api/pageData'
|
|
import type {
|
|
GameDataValue,
|
|
} from '#layers/types/api/gameData'
|
|
|
|
export default defineNuxtRouteMiddleware(async (to, _from) => {
|
|
if (!import.meta.client) return
|
|
|
|
const config = useRuntimeConfig()
|
|
const stoveApiBaseUrl = config.public.stoveApiUrl
|
|
const apiUrl = `${stoveApiBaseUrl}/pub-comm/v2.0/template/page`
|
|
|
|
const store = usePageDataStore()
|
|
const gameDomain = useGetGameDomain()
|
|
const { getPathAfterLanguage } = usePathResolver()
|
|
const headers = useRequestHeaders()
|
|
const gameDataStore = useGameDataStore()
|
|
const gameData = gameDataStore.gameData as GameDataValue
|
|
|
|
const langCode = ssrGetFinalLocale(to.path, headers, gameData?.lang_codes, gameData?.default_lang_code)
|
|
|
|
try {
|
|
if (to.path.includes('inspection')) {
|
|
return
|
|
}
|
|
|
|
const pageUrl = getPathAfterLanguage(to.path)
|
|
|
|
// pageUrl이 빈값이거나 null이면 /brand로 리다이렉트
|
|
if (!pageUrl || pageUrl === '' || pageUrl === '/') {
|
|
return navigateTo(`/${langCode}/brand`, { replace: true })
|
|
}
|
|
|
|
const queryParams: Record<string, string> = {
|
|
game_domain: gameDomain,
|
|
lang_code: langCode,
|
|
page_url: pageUrl,
|
|
_t: Date.now().toString(), // 캐시 무효화를 위한 타임스탬프
|
|
}
|
|
// console.log('🚀 ~ queryParams:', queryParams)
|
|
|
|
const response = (await commonFetch('GET', apiUrl, {
|
|
query: queryParams,
|
|
loading: true,
|
|
})) as PageDataResponse | null
|
|
|
|
console.log('🚀 ~ response?.code:', response?.code)
|
|
// if(response?.code === 91003) {
|
|
// throw createError({
|
|
// statusCode: 404,
|
|
// statusMessage: 'Page not found',
|
|
// })
|
|
// }
|
|
|
|
if (response?.code === 0 && 'value' in response) {
|
|
store.setPageData(response.value)
|
|
console.log('🚀 ~ pageData:', response.value)
|
|
} else {
|
|
store.clearPageData()
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
store.clearPageData()
|
|
}
|
|
})
|