329 lines
13 KiB
JavaScript
329 lines
13 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) {
|
|
var message = ajaxUtil._msg500;
|
|
if (code == '400') {
|
|
message = ajaxUtil._msg400;
|
|
}
|
|
// ajaxUtil._alert(message, '오류 안내');
|
|
ajaxUtil._alert(message, null);
|
|
},
|
|
_alert : function (alertMessage, alertTitle, afterEvent) {
|
|
if(alertTitle){
|
|
etUI.dialog.alert({
|
|
title: alertTitle,
|
|
message: alertMessage,
|
|
type: "alert",
|
|
callback: function() {
|
|
if (typeof afterEvent === 'function') {
|
|
afterEvent();
|
|
}
|
|
},
|
|
});
|
|
} else {
|
|
etUI.dialog.alert({
|
|
message: alertMessage,
|
|
type: "alert",
|
|
callback: function() {
|
|
if (typeof afterEvent === 'function') {
|
|
afterEvent();
|
|
}
|
|
},
|
|
});
|
|
}
|
|
},
|
|
_alertId : function (alertId, afterEvent) {
|
|
ajaxUtil._alertMethod({
|
|
id: alertId,
|
|
title: null,
|
|
message: null,
|
|
after: afterEvent !== undefined ? (afterEvent ? 'Y' : 'N') : 'N',
|
|
});
|
|
},
|
|
_alertMethod : function (jsonObj) {
|
|
ajaxUtil._ajaxSyncNoWaiting(
|
|
'/m/common/alert',
|
|
jsonObj,
|
|
function(rstData) {
|
|
var $wrap = $('#bindWrap');
|
|
if ($wrap.find('.ui-popup-common-alert').length != 0) {
|
|
$wrap.find('.ui-popup-common-alert').remove();
|
|
}
|
|
|
|
$wrap.append(rstData);
|
|
uxgFn.popup.bind('.ui-popup-common-alert').open();
|
|
},
|
|
function(code, rstData) {
|
|
kri.logger.error('Do nothing. code: '+ code, rstData);
|
|
},
|
|
'html');
|
|
},
|
|
|
|
|
|
/**
|
|
* 로딩바가 있는 동기식 방식 처리
|
|
*/
|
|
_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, 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 : {
|
|
404:function(rstData){
|
|
ajaxUtil._hideWaiting();
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(ajaxUtil.$404, rstData);
|
|
} else {
|
|
// kri.menu.main();
|
|
kri.logger.error('statusCode: 404');
|
|
}
|
|
},
|
|
401:function(){
|
|
// kri.menu.main();
|
|
kri.logger.error('statusCode: 401');
|
|
},
|
|
403:function(rstData){
|
|
// kri.menu.main();
|
|
kri.logger.error('statusCode: 403');
|
|
},
|
|
422:function(rstData, status) {
|
|
console.log('422', rstData)
|
|
|
|
let isResetDone = false;
|
|
|
|
$(rstData.responseJSON.fieldErrors).each((idx, oj) => {
|
|
const target = $('[name="' + oj.field + '"]');
|
|
let infoBox = null;
|
|
if (!isResetDone) {
|
|
const formElement = target.closest('form');
|
|
if (formElement.length > 0) {
|
|
$(formElement).find('.component-input').removeClass('input-invalid');
|
|
$(formElement).find('.input-flex-group').removeClass('invalid');
|
|
$(formElement).find('.select-group').removeClass('select-invalid');
|
|
$(formElement).find('.input-info').each(function () {
|
|
$(this).remove();
|
|
});
|
|
}
|
|
isResetDone = true;
|
|
}
|
|
|
|
if (target.length > 0) {
|
|
|
|
const component = target.closest('.component-input');
|
|
if(component){
|
|
component.addClass('input-invalid');
|
|
//연결된 구조 일 경우 상위에 변경
|
|
if(target.closest('.input-flex-group').length > 0){
|
|
infoBox = target.closest('.input-flex-group').find('.input-info');
|
|
} else {
|
|
infoBox = component.find('.input-info');
|
|
}
|
|
|
|
if (infoBox.length === 0) {
|
|
if(target.closest('.input-flex-group').length > 0){
|
|
infoBox = $('<ul class="input-info"></ul>').appendTo(target.closest('.input-flex-group'));
|
|
target.closest('.input-flex-group').addClass('invalid');
|
|
} else {
|
|
infoBox = $('<ul class="input-info"></ul>').appendTo(component);
|
|
}
|
|
}
|
|
infoBox.html('<li>' + oj.message + '</li>');
|
|
}
|
|
}
|
|
|
|
if (target.data('type') === 'select') {
|
|
|
|
const component = target.parent().parent().closest('.select-group');
|
|
if (component) {
|
|
component.addClass('select-invalid');
|
|
|
|
infoBox = component.find('.input-info');
|
|
if (infoBox.length === 0) {
|
|
infoBox = $('<ul class="input-info"></ul>').appendTo(component);
|
|
}
|
|
|
|
infoBox.html('<li>' + oj.message + '</li>');
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
if ($('.input-info').length === 0) {
|
|
let fieldErrors = rstData.responseJSON != null ? rstData.responseJSON : null;
|
|
if (fieldErrors != null && fieldErrors.length > 0) {
|
|
ajaxUtil._alert(fieldErrors[0].defaultMessage, '유효성 체크');
|
|
}
|
|
}
|
|
|
|
// 포커스 이동
|
|
if ($('.input-invalid').length != 0) {
|
|
$('.input-invalid').eq(0).find('input').focus();
|
|
}
|
|
if ($('.select-invalid').length != 0) {
|
|
$('.select-invalid').eq(0).find('button').focus();
|
|
}
|
|
|
|
if (typeof errorCallback !== 'undefined') {
|
|
errorCallback(rstData.status, rstData);
|
|
}
|
|
},
|
|
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();
|
|
if (typeof successCallback !== 'undefined') {
|
|
successCallback(rstData, status);
|
|
}
|
|
},
|
|
error : function(rstData) {
|
|
ajaxUtil._hideWaiting();
|
|
|
|
if (!(rstData.status == 500 || rstData.status == 422)) {
|
|
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;
|
|
}
|
|
},
|
|
});
|
|
},
|
|
|
|
|
|
_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;
|
|
// 로딩바 숨김
|
|
document.querySelector('#loadingBar').style.display = 'none';
|
|
}
|
|
},
|
|
_showWaiting:function(){
|
|
ajaxUtil.loadingStatus = true;
|
|
// 로딩바 활성화
|
|
document.querySelector('#loadingBar').style.display = 'block';
|
|
},
|
|
}; |