From f58233aa8ab939c1590f3a2f9d03596406ff0a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A3=BC=EC=88=98=ED=98=84?= Date: Fri, 13 Jun 2025 17:36:36 +0900 Subject: [PATCH] =?UTF-8?q?.=EC=9D=BC=EB=8B=A8=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/static/scripts/ajaxUtils.js | 248 ++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 admin/src/main/resources/static/scripts/ajaxUtils.js diff --git a/admin/src/main/resources/static/scripts/ajaxUtils.js b/admin/src/main/resources/static/scripts/ajaxUtils.js new file mode 100644 index 00000000..87fca12d --- /dev/null +++ b/admin/src/main/resources/static/scripts/ajaxUtils.js @@ -0,0 +1,248 @@ +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, '오류 안내'); + }, + _alert : function (alertMessage, alertTitle, afterEvent) { + etUI.dialog.alert({ + title: alertTitle, + message: alertMessage, + type: "alert", + callback: function() { + console.log("alert callback"); + }, + }); + }, + _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(rstData) + $(rstData.responseJSON).each((idx, oj) => { + // TODO : form id? or body + 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(); + 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; + // TODO : 로딩바 숨김 + } + }, + _showWaiting:function(){ + ajaxUtil.loadingStatus = true; + // TODO : 로딩바 활성화 + }, +}; \ No newline at end of file