From bc09b61a36f7e7ddc769ba32079b14b6dd4f0813 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Fri, 1 Jan 2021 21:54:41 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=89=8D=E7=AB=AF=E3=80=91=E9=87=8D?= =?UTF-8?q?=E6=9E=84http=E7=9A=84=E8=AF=B7=E6=B1=82=E5=B0=81=E8=A3=85?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E7=99=BB=E9=99=86=E5=92=8C=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=96=B0=E7=9A=84http=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/webapp/assets/common/js/common.js | 4 +- .../expand/module/HttpRequest/HttpRequest.js | 153 ++++++++++++++++++ src/main/webapp/assets/expand/module/ax/ax.js | 77 --------- src/main/webapp/pages/index.html | 47 +----- src/main/webapp/pages/login.html | 60 ++----- 5 files changed, 170 insertions(+), 171 deletions(-) create mode 100644 src/main/webapp/assets/expand/module/HttpRequest/HttpRequest.js delete mode 100644 src/main/webapp/assets/expand/module/ax/ax.js diff --git a/src/main/webapp/assets/common/js/common.js b/src/main/webapp/assets/common/js/common.js index 75126196..fd71b2c3 100644 --- a/src/main/webapp/assets/common/js/common.js +++ b/src/main/webapp/assets/common/js/common.js @@ -120,7 +120,7 @@ layui.config({ selectPlus: '../../expand/module/selectPlus/selectPlus', iconPicker: '../../expand/module/iconPicker/iconPicker', ztree: '../../expand/module/ztree/ztree-object', - ax: '../../expand/module/ax/ax', + HttpRequest: '../../expand/module/HttpRequest/HttpRequest', func: '../../expand/module/func/func' }).use(['layer', 'admin'], function () { var $ = layui.jquery; @@ -136,12 +136,10 @@ layui.config({ $.ajaxSetup({ contentType: "application/x-www-form-urlencoded;charset=utf-8", complete: function (XMLHttpRequest, textStatus) { - //如果超时就处理 ,指定要跳转的页面 if (XMLHttpRequest.getResponseHeader("Guns-Session-Timeout") === "true") { window.location = Feng.ctxPath + "/global/sessionError"; } - } }); diff --git a/src/main/webapp/assets/expand/module/HttpRequest/HttpRequest.js b/src/main/webapp/assets/expand/module/HttpRequest/HttpRequest.js new file mode 100644 index 00000000..4704dabb --- /dev/null +++ b/src/main/webapp/assets/expand/module/HttpRequest/HttpRequest.js @@ -0,0 +1,153 @@ +/** + * http请求对ajax封装,减少一些重复业务逻辑的编写 + * @author fengshuonan + */ +layui.define(['jquery'], function (exports) { + var $ = layui.$; + + /** + * 创建一个ajax的请求类 + * + * @param url 请求后端的url + * @param method http请求方法,写get或者post + * @param successCallback 请求成功的回调函数 + * @param errorCallback 请求失败的回调函数,后端返回http状态码500 + * + * 使用方法: + * + * var request = new HttpRequest('/user/list', 'get', function(data){ + * // ... + * }, function(){ + * // ... + * }); + * request.start(); + */ + var HttpRequest = function (url, method, successCallback, errorCallback) { + + // 请求的url,一般传参时候需要带上contextPath + this.url = url; + + // http请求的方法,默认不传为post,一般传 'get' 或 'post' + if (method === "" || method == null) { + this.method = "post"; + } else { + this.method = method; + } + + // 请求成功的回调 + this.successCallback = successCallback; + + // 请求失败的回调 + this.errorCallback = errorCallback; + + // 请求的数据 + this.dataObject = {}; + + // 请求的content-type,默认 "application/json" + this.contentType = "application/json"; + + // 预期服务器返回的数据类型,默认都为 json + this.dataType = "json"; + + // 默认请求都是同步,不开启异步 + this.async = false; + }; + + HttpRequest.prototype = { + + /** + * 执行http请求 + * + * @param parseJsonFlag 是否在请求时,参数转化为json,如果传 true 就是 + */ + start: function (parseJsonFlag) { + var me = this; + var result = ""; + + // 如果请求需要转化为json则将data转为json + if (parseJsonFlag === true) { + me.dataObject = JSON.stringify(me.dataObject); + } + + // 防止http请求缓存 + if (this.url.indexOf("?") === -1) { + this.url = this.url + "?jstime=" + new Date().getTime(); + } else { + this.url = this.url + "&jstime=" + new Date().getTime(); + } + + // 初始化ajax + $.ajax({ + type: me.method, + url: me.url, + contentType: me.contentType, + dataType: me.dataType, + async: me.async, + data: me.dataObject, + beforeSend: function (data) { + }, + success: function (data) { + result = data; + if (me.successCallback !== undefined) { + me.successCallback(data); + } + }, + error: function (xhr) { + if (me.errorCallback !== undefined) { + me.errorCallback(xhr.responseJSON); + } + } + }); + + return result; + }, + + /** + * 设置 key-value 形式的参数 + * + * 此参数组装的是param方式传参的参数,如需传递json请用 setJsonData(data) + * + * @param key 参数的key + * @param value 参数值 + */ + set: function (key, value) { + this.dataObject[key] = (typeof value === "undefined") ? $("#" + key).val() : value; + return this; + }, + + /** + * 清空传递参数 + */ + clear: function () { + this.dataObject = {}; + return this; + }, + + /** + * 设置请求的content-type + */ + setContentType: function (contentType) { + this.contentType = contentType; + return this; + }, + + /** + * 设置预期服务器返回的数据类型,默认都为 json + */ + setDataType: function (dataType) { + this.dataType = dataType; + return this; + }, + + /** + * 设置请求同步还是异步,true-异步,false-同步 + */ + setAsync: function (async) { + this.async = async; + return this; + } + + }; + + exports('HttpRequest', HttpRequest); +}); \ No newline at end of file diff --git a/src/main/webapp/assets/expand/module/ax/ax.js b/src/main/webapp/assets/expand/module/ax/ax.js deleted file mode 100644 index 3272ac9a..00000000 --- a/src/main/webapp/assets/expand/module/ax/ax.js +++ /dev/null @@ -1,77 +0,0 @@ -layui.define(['jquery'], function (exports) { - var $ = layui.$; - - var $ax = function (url, success, error) { - this.url = url; - this.type = "post"; - this.data = {}; - this.contentType = "application/json"; - this.dataType = "json"; - this.async = false; - this.success = success; - this.error = error; - }; - - $ax.prototype = { - start: function () { - var me = this; - var result = ""; - - if (this.url.indexOf("?") === -1) { - this.url = this.url + "?jstime=" + new Date().getTime(); - } else { - this.url = this.url + "&jstime=" + new Date().getTime(); - } - - $.ajax({ - type: me.type, - url: me.url, - contentType: me.contentType, - dataType: me.dataType, - async: me.async, - data: JSON.stringify(me.data), - beforeSend: function (data) { - - }, - success: function (data) { - result = data; - if (me.success !== undefined) { - me.success(data); - } - }, - error: function (data) { - if (me.error !== undefined) { - me.error(data); - } - } - }); - - return result; - }, - - set: function (key, value) { - if (typeof key === "object") { - for (var i in key) { - if (typeof i === "function") - continue; - this.data[i] = key[i]; - } - } else { - this.data[key] = (typeof value === "undefined") ? $("#" + key).val() : value; - } - return this; - }, - - setData: function (data) { - this.data = data; - return this; - }, - - clear: function () { - this.data = {}; - return this; - } - }; - - exports('ax', $ax); -}); \ No newline at end of file diff --git a/src/main/webapp/pages/index.html b/src/main/webapp/pages/index.html index 2f1c8464..0f34acf5 100644 --- a/src/main/webapp/pages/index.html +++ b/src/main/webapp/pages/index.html @@ -43,48 +43,12 @@ diff --git a/src/main/webapp/pages/login.html b/src/main/webapp/pages/login.html index 2875f42a..56304974 100644 --- a/src/main/webapp/pages/login.html +++ b/src/main/webapp/pages/login.html @@ -163,14 +163,6 @@

用户登录

- @if(constants.getTenantOpen()){ -
- - -
- @}
@@ -179,20 +171,13 @@
- @if(constants.getCaptchaOpen()){ - - @}
@@ -211,57 +196,34 @@