209 lines
6.0 KiB
JavaScript
209 lines
6.0 KiB
JavaScript
import axios from "axios";
|
|
|
|
import Spotlight from "@enact/spotlight";
|
|
|
|
import { types } from "../actions/actionTypes";
|
|
import {
|
|
changeAppStatus,
|
|
changeLocalSettings,
|
|
setShowPopup,
|
|
setSystemNotice,
|
|
setSystemTermination,
|
|
} from "../actions/commonActions";
|
|
import {
|
|
getAuthenticationCode,
|
|
getReAuthenticationCode,
|
|
} from "../actions/deviceActions";
|
|
import { ACTIVE_POPUP } from "../utils/Config";
|
|
import * as HelperMethods from "../utils/helperMethods";
|
|
import { getUrl, URLS } from "./apiConfig";
|
|
import { pushPanel } from "../actions/panelActions";
|
|
import * as Config from "../utils/Config";
|
|
|
|
let tokenRefreshing = false;
|
|
const axiosQueue = [];
|
|
|
|
export const setTokenRefreshing = (value) => {
|
|
console.log("TAxios setTokenRefreshing ", value);
|
|
tokenRefreshing = value;
|
|
};
|
|
export const runDelayedAction = (dispatch, getState) => {
|
|
console.log("runDelayedAction axiosQueue size", axiosQueue.length);
|
|
while (axiosQueue.length > 0) {
|
|
const requestConfig = axiosQueue.pop(); // queue에서 요청을 하나씩 pop
|
|
TAxios(
|
|
dispatch,
|
|
getState,
|
|
requestConfig.type,
|
|
requestConfig.baseUrl,
|
|
requestConfig.urlParams,
|
|
requestConfig.params,
|
|
requestConfig.onSuccess,
|
|
requestConfig.onFail
|
|
);
|
|
}
|
|
};
|
|
export const TAxios = (
|
|
dispatch,
|
|
getState,
|
|
type,
|
|
baseUrl,
|
|
urlParams = {},
|
|
params = {},
|
|
onSuccess,
|
|
onFail,
|
|
noTokenRefresh = false
|
|
) => {
|
|
const pushQueue = () => {
|
|
if (!noTokenRefresh) {
|
|
axiosQueue.push({ type, baseUrl, urlParams, params, onSuccess, onFail });
|
|
}
|
|
};
|
|
const executeRequest = (accessToken) => {
|
|
const httpHeader = getState().common.httpHeader;
|
|
const { mbr_no, deviceId } = getState().common.appStatus;
|
|
const refreshToken = getState().localSettings.refreshToken;
|
|
const AUTHORIZATION = { headers: { ...httpHeader } };
|
|
|
|
if (accessToken) {
|
|
AUTHORIZATION.headers["lgsp_auth"] = accessToken;
|
|
}
|
|
AUTHORIZATION.headers["dvc_id"] = deviceId;
|
|
AUTHORIZATION.headers["refresh-token"] = refreshToken;
|
|
|
|
if (typeof window === "object") {
|
|
let url = Array.isArray(baseUrl)
|
|
? getUrl(getState, baseUrl[0])
|
|
: getUrl(getState, baseUrl);
|
|
|
|
if (!url) {
|
|
//todo error page
|
|
return;
|
|
}
|
|
if (type === "get") {
|
|
const _urlparams = HelperMethods.createQueryString(urlParams);
|
|
url += _urlparams ? `?${_urlparams}` : "";
|
|
}
|
|
|
|
let axiosInstance;
|
|
switch (type) {
|
|
case "get":
|
|
axiosInstance = axios.get(url, AUTHORIZATION);
|
|
break;
|
|
case "post":
|
|
axiosInstance = axios.post(url, params, AUTHORIZATION);
|
|
break;
|
|
// TODO: 다른 HTTP 메소드 있다면 처리 (chw)
|
|
}
|
|
|
|
if (axiosInstance) {
|
|
axiosInstance
|
|
.then((res) => {
|
|
console.log("TAxios response", url, res);
|
|
|
|
const apiSysStatus = res.headers["api-sys-status"];
|
|
const { systemNotice, systemTermination, appStatus } =
|
|
getState().common;
|
|
const isInitialLoad = !appStatus.loadingComplete;
|
|
|
|
if (apiSysStatus === "800" && !systemNotice) {
|
|
dispatch(setSystemNotice());
|
|
} else if (apiSysStatus === "900" && !systemTermination) {
|
|
dispatch(setSystemTermination(isInitialLoad));
|
|
}
|
|
|
|
if (baseUrl === URLS.GET_AUTHENTICATION_CODE) {
|
|
if (res?.data?.retCode !== 0) {
|
|
console.error("accessToken failed", res.data.retCode);
|
|
dispatch(
|
|
setShowPopup(ACTIVE_POPUP.networkErrorPopup, {
|
|
data: res.data.retCode,
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// if(res?.data?.retCode === 501){
|
|
// //약관 비동의
|
|
// dispatch(changeLocalSettings({accessToken: null}));
|
|
// dispatch({type: types.GET_TERMS_AGREE_YN, payload: {}});
|
|
// return;
|
|
// }
|
|
|
|
// AccessToken 만료
|
|
if (res?.data?.retCode === 401) {
|
|
if (!tokenRefreshing) {
|
|
dispatch(getReAuthenticationCode());
|
|
}
|
|
pushQueue();
|
|
return;
|
|
}
|
|
|
|
//RefreshToken 만료
|
|
if (res?.data?.retCode === 402 || res?.data?.retCode === 501) {
|
|
if (baseUrl === URLS.GET_RE_AUTHENTICATION_CODE) {
|
|
dispatch(getAuthenticationCode());
|
|
} else {
|
|
if (!tokenRefreshing) {
|
|
dispatch(getAuthenticationCode());
|
|
}
|
|
pushQueue();
|
|
}
|
|
return;
|
|
}
|
|
// 602 요청 국가 불일치
|
|
if (res?.data?.retCode === 602) {
|
|
dispatch(
|
|
setShowPopup(ACTIVE_POPUP.changeCountyPopup, {
|
|
data: res.data.retCode,
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
// 603 서비스 국가 아님
|
|
if (res?.data?.retCode === 603) {
|
|
dispatch(
|
|
setShowPopup(ACTIVE_POPUP.unSupportedCountryPopup, {
|
|
data: res.data.retCode,
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (res?.data?.retCode === 604) {
|
|
//todo "NotServiceLanguage"
|
|
return;
|
|
}
|
|
|
|
// 장애 발생
|
|
if (res?.data?.retCode === 900) {
|
|
dispatch(
|
|
pushPanel({
|
|
name: Config.panel_names.ERROR_PANEL,
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (onSuccess) onSuccess(res);
|
|
})
|
|
.catch((error) => {
|
|
console.error("TAxios ", url, error);
|
|
if (onFail) onFail(error);
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
const accessToken = getState().localSettings.accessToken;
|
|
if (noTokenRefresh || (!tokenRefreshing && accessToken)) {
|
|
executeRequest(accessToken);
|
|
} else {
|
|
if (!accessToken && !tokenRefreshing) {
|
|
dispatch(getAuthenticationCode());
|
|
}
|
|
pushQueue();
|
|
}
|
|
};
|