284 lines
11 KiB
JavaScript
284 lines
11 KiB
JavaScript
var ajaxUtil = {
|
|
_msg400 : '잘못된 요청입니다.',
|
|
_msg404 : '',
|
|
_msg401 : '로그인에 실패했습니다.',
|
|
_msg403 : '로그인에 실패했습니다.',
|
|
_msg500 : '오류가 발생하여\n정상 처리되지 않았습니다.',
|
|
|
|
$400 : '400',
|
|
$401 : '401',
|
|
$403 : '403',
|
|
$404 : '404',
|
|
$500 : '500',
|
|
|
|
_csrfHeader : $('meta[name="_csrf_header"]').attr('content'),
|
|
_csrfToken : $('meta[name="_csrf"]').attr('content'),
|
|
loading : null,
|
|
loadingStatus:false,
|
|
|
|
_init : function(obj){ //에러메시지를 정의 해주어야 한다.
|
|
ajaxUtil._msg400 = obj._msg400;
|
|
ajaxUtil._msg404 = obj._msg404;
|
|
ajaxUtil._msg401 = obj._msg401;
|
|
ajaxUtil._msg403 = obj._msg403;
|
|
ajaxUtil._msg500 = obj._msg500;
|
|
},
|
|
|
|
_alertError : function (code) {
|
|
let message = ajaxUtil._msg500;
|
|
if (code === '400') {
|
|
message = ajaxUtil._msg400;
|
|
}
|
|
|
|
ajaxUtil._alert(message, '오류 안내');
|
|
},
|
|
_alert : function (alertMessage, alertTitle) {
|
|
console.warn(alertTitle)
|
|
|
|
alert(alertMessage);
|
|
},
|
|
|
|
|
|
/**
|
|
* 로딩바가 있는 동기식 방식 처리
|
|
*/
|
|
_ajaxSync : function(url, jsonObj, successCallback, errorCallback, dataType) {
|
|
ajaxUtil._showWaiting();
|
|
ajaxUtil._ajaxSyncMethod(url, ajaxUtil._jsonStringify(jsonObj), successCallback, errorCallback, 'post', dataType);
|
|
},
|
|
/**
|
|
* 로딩바가 없는 동기식 방식 처리
|
|
*/
|
|
_ajaxSyncNoWaiting : function(url, jsonObj, successCallback, errorCallback, dataType) {
|
|
ajaxUtil._ajaxSyncMethod(url, ajaxUtil._jsonStringify(jsonObj), successCallback, errorCallback, 'post', dataType);
|
|
},
|
|
/**
|
|
* 로딩바가 없는 동기식 방식 처리(MethodType를 지정할 수 있는)
|
|
*/
|
|
_ajaxSyncMethod : function(url, jsonObj, successCallback, errorCallback, method, dataType) {
|
|
ajaxUtil._ajaxMethod(url, jsonObj, successCallback, errorCallback, method, dataType, false);
|
|
},
|
|
/**
|
|
* 로딩바가 있는 비동기식 방식 처리
|
|
*/
|
|
_ajaxAsync : function (url, jsonObj, successCallback, errorCallback, dataType) {
|
|
ajaxUtil._showWaiting();
|
|
ajaxUtil._ajaxAsyncMethod(url, ajaxUtil._jsonStringify(jsonObj), successCallback, errorCallback, 'post', dataType);
|
|
},
|
|
/**
|
|
* 로딩바가 없는 비동기식 방식 처리
|
|
*/
|
|
_ajaxAsyncNoWaiting : function (url, jsonObj, successCallback, errorCallback, dataType) {
|
|
ajaxUtil._ajaxAsyncMethod(url, ajaxUtil._jsonStringify(jsonObj), successCallback, errorCallback, 'post', dataType);
|
|
},
|
|
/**
|
|
* 로딩바가 없는 비동기식 방식 처리(MethodType를 지정할 수 있는)
|
|
*/
|
|
_ajaxAsyncMethod : function (url, jsonObj, successCallback, errorCallback, method, dataType) {
|
|
ajaxUtil._ajaxMethod(url, jsonObj, successCallback, errorCallback, method, dataType, true);
|
|
},
|
|
/**
|
|
* ajax 공통 처리
|
|
*/
|
|
_ajaxMethod : function (url, jsonObj, successCallback, errorCallback, method, dataType, options = {}, async) {
|
|
if (typeof ENV != 'undefined' && typeof ENV.submitted == 'boolean') {
|
|
ENV.submitted = true;
|
|
}
|
|
|
|
$.ajax(
|
|
{
|
|
beforeSend : function(xhr) {
|
|
if (ajaxUtil._csrfHeader != undefined) {
|
|
xhr.setRequestHeader(ajaxUtil._csrfHeader, ajaxUtil._csrfToken);
|
|
}
|
|
},
|
|
beforeSubmit : '',
|
|
url : url,
|
|
type : method != undefined ? method : 'post', // get, post
|
|
dataType : dataType != undefined ? dataType : 'json', // xml, html, script, json
|
|
data : jsonObj,
|
|
contentType : 'application/json; charset=UTF-8',
|
|
async: async,
|
|
statusCode : {
|
|
400:function(rstData){
|
|
if (options.handle422Type === 'alert') {
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(400, rstData); // 호출한 쪽에서 alert 처리
|
|
}
|
|
return;
|
|
}
|
|
|
|
ajaxUtil._alertError(ajaxUtil.$400);
|
|
},
|
|
404:function(rstData){
|
|
ajaxUtil._hideWaiting();
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(ajaxUtil.$404, rstData);
|
|
} else {
|
|
console.error('statusCode: 404');
|
|
}
|
|
},
|
|
401:function(){
|
|
console.error('statusCode: 401');
|
|
},
|
|
403:function(rstData){
|
|
console.error('statusCode: 403');
|
|
},
|
|
422:function(rstData, status) {
|
|
console.log(rstData);
|
|
// options type : 1. alert, 2. field
|
|
if (options.handle422Type === 'alert') {
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(422, rstData); // 호출한 쪽에서 alert 처리
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (options.handle422Type === 'field' || !options.handle422Type) {
|
|
$(rstData.responseJSON).each((idx, oj) => {
|
|
let target = $('body').find('[name=' + oj.field + ']');
|
|
target.addClass('is-invalid');
|
|
|
|
target.parent().find('.invalid-feedback').text(oj.defaultMessage);
|
|
});
|
|
|
|
if ($('.invalid-feedback').length === 0) {
|
|
let fieldErrors = rstData.responseJSON != null ? rstData.responseJSON : null;
|
|
if (fieldErrors != null && fieldErrors.length > 0) {
|
|
ajaxUtil._alert(fieldErrors[0].defaultMessage, '유효성 체크');
|
|
}
|
|
|
|
}
|
|
}
|
|
},
|
|
500:function(rstData){
|
|
ajaxUtil._hideWaiting();
|
|
if (rstData.responseText != '') {
|
|
let resultJSON = ajaxUtil._jsonParse(rstData.responseText);
|
|
if (rstData.responseJSON != undefined) {
|
|
resultJSON = rstData.responseJSON;
|
|
}
|
|
|
|
if (resultJSON.code == 1064) {
|
|
ajaxUtil._alert(resultJSON.message, '확인');
|
|
} else if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(ajaxUtil.$500, rstData);
|
|
}
|
|
} else if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(ajaxUtil.$500, rstData);
|
|
}
|
|
}
|
|
},
|
|
success : function(rstData, status) {
|
|
ajaxUtil._hideWaiting();
|
|
console.log('succ', rstData)
|
|
if (typeof successCallback !== 'undefined') {
|
|
successCallback(rstData, status);
|
|
}
|
|
},
|
|
error : function(rstData) {
|
|
ajaxUtil._hideWaiting();
|
|
console.log('error')
|
|
if (!(rstData.status == 500 || rstData.status == 422 || rstData.status == 400)) {
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(rstData.status, rstData);
|
|
} else {
|
|
ajaxUtil._alertError(ajaxUtil._msg500);
|
|
}
|
|
}
|
|
},
|
|
complete : function(xhr, status, err) {
|
|
ajaxUtil._hideWaiting();
|
|
if (typeof ENV != 'undefined' && typeof ENV.submitted == 'boolean') {
|
|
ENV.submitted = false;
|
|
}
|
|
if (typeof commentENV != 'undefined' && typeof commentENV.submitted == 'boolean') {
|
|
commentENV.submitted = false;
|
|
}
|
|
},
|
|
});
|
|
},
|
|
/**
|
|
* 엑셀 다운로드 (로딩바 포함)
|
|
*/
|
|
_excelDownload: function(url, data, errorCallback) {
|
|
ajaxUtil._showWaiting();
|
|
|
|
$.ajax({
|
|
url: url,
|
|
method: 'GET',
|
|
data: data,
|
|
xhrFields: {
|
|
responseType: 'blob'
|
|
},
|
|
beforeSend: function(xhr) {
|
|
if (ajaxUtil._csrfHeader != undefined) {
|
|
xhr.setRequestHeader(ajaxUtil._csrfHeader, ajaxUtil._csrfToken);
|
|
}
|
|
},
|
|
success: function(blob, status, xhr) {
|
|
const disposition = xhr.getResponseHeader('Content-Disposition');
|
|
const filenameMatch = disposition?.match(/filename\*?=UTF-8''(.+)/);
|
|
const filename = filenameMatch ? decodeURIComponent(filenameMatch[1]) : 'download.xlsx';
|
|
|
|
const blobUrl = window.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = blobUrl;
|
|
a.download = filename;
|
|
a.click();
|
|
window.URL.revokeObjectURL(blobUrl);
|
|
},
|
|
error: function(rstData) {
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(rstData.status, rstData);
|
|
} else {
|
|
alert('엑셀 다운로드 실패');
|
|
}
|
|
},
|
|
complete: function() {
|
|
ajaxUtil._hideWaiting();
|
|
}
|
|
});
|
|
},
|
|
|
|
|
|
_jsonParse : function parse(jsonStr) {
|
|
try {
|
|
var jsonObj = JSON.parse(jsonStr);
|
|
return jsonObj;
|
|
} catch (error) {
|
|
console.error("Invalid JSON string:", error);
|
|
}
|
|
return null;
|
|
},
|
|
_jsonStringify : function stringify(obj) {
|
|
try {
|
|
var jsonStr = JSON.stringify(obj);
|
|
return jsonStr;
|
|
} catch (error) {
|
|
console.error("Error stringifying object:", error);
|
|
}
|
|
return null;
|
|
},
|
|
|
|
|
|
_goToMain:function() {
|
|
location.href = '/';
|
|
},
|
|
_hideWaiting:function(){
|
|
if (ajaxUtil.loadingStatus) {
|
|
ajaxUtil.loadingStatus = false;
|
|
$('#loading').hide();
|
|
}
|
|
},
|
|
_showWaiting:function(){
|
|
ajaxUtil.loadingStatus = true;
|
|
$('#loading').show();
|
|
},
|
|
};
|
|
|
|
const excelDownload = (url, data) => {
|
|
ajaxUtil._excelDownload(url, data, (status, error) => {
|
|
console.error('엑셀 다운로드 실패:', status, error);
|
|
});
|
|
}; |