From aaa03fa2fa8ead98ba761c1dab5a6026cffe2afe Mon Sep 17 00:00:00 2001 From: zhizous Date: Thu, 12 Dec 2019 02:14:04 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9AokFly(ajax=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E8=AF=B7=E6=B1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/navs.json | 13 +++ js/chartChina.js | 0 lib/layui/lay/okmodules/okFly.js | 47 +++++++++++ lib/layui/layui.js | 1 + pages/help/okFly.html | 132 +++++++++++++++++++++++++++++++ pages/help/test.html | 26 ++++-- 6 files changed, 214 insertions(+), 5 deletions(-) delete mode 100644 js/chartChina.js create mode 100644 lib/layui/lay/okmodules/okFly.js create mode 100644 pages/help/okFly.html diff --git a/data/navs.json b/data/navs.json index c9b13dc..3949057 100644 --- a/data/navs.json +++ b/data/navs.json @@ -7,6 +7,12 @@ "spread": true, "isCheck": true }, + { + "title": "测试", + "href": "pages/help/test.html", + "fontFamily": "ok-icon", + "icon": "" + }, { "title": "控制台示例", "href": "", @@ -67,7 +73,14 @@ "href": "pages/help/okLayer.html", "icon": "", "spread": false + }, + { + "title": "okFly", + "href": "pages/help/okFly.html", + "icon": "", + "spread": false } + ] }, { diff --git a/js/chartChina.js b/js/chartChina.js deleted file mode 100644 index e69de29..0000000 diff --git a/lib/layui/lay/okmodules/okFly.js b/lib/layui/lay/okmodules/okFly.js new file mode 100644 index 0000000..48e8ac1 --- /dev/null +++ b/lib/layui/lay/okmodules/okFly.js @@ -0,0 +1,47 @@ +/*flyio官方文档 https://wendux.github.io/dist/#/doc/flyio/readme*/ +!function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=5)}({1:function(e,t,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};e.exports={type:function(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()},isObject:function(e,t){return t?"object"===this.type(e):e&&"object"===(void 0===e?"undefined":r(e))},isFormData:function(e){return"undefined"!=typeof FormData&&e instanceof FormData},trim:function(e){return e.replace(/(^\s*)|(\s*$)/g,"")},encode:function(e){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")},formatParams:function(e){function t(e,i){var s=o.encode,a=o.type(e);if("array"==a)e.forEach(function(e,n){o.isObject(e)||(n=""),t(e,i+"%5B"+n+"%5D")});else if("object"==a)for(var c in e)i?t(e[c],i+"%5B"+s(c)+"%5D"):t(e[c],s(c));else r||(n+="&"),r=!1,n+=i+"="+s(e)}var n="",r=!0,o=this;return this.isObject(e)?(t(e,""),n):e},merge:function(e,t){for(var n in t)e.hasOwnProperty(n)?this.isObject(t[n],1)&&this.isObject(e[n],1)&&this.merge(e[n],t[n]):e[n]=t[n];return e}}},5:function(e,t,n){function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var n=0;n0&&(e+=(-1===e.indexOf("?")?"?":"&")+P.join("&")),o.open(n.method,e);try{o.withCredentials=!!n.withCredentials,o.timeout=n.timeout||0,"stream"!==w&&(o.responseType=w)}catch(e){}var E=n.headers[a]||n.headers[c],T="application/x-www-form-urlencoded";i.trim((E||"").toLowerCase())===T?t=i.formatParams(t):i.isFormData(t)||-1===["object","array"].indexOf(i.type(t))||(T="application/json;charset=utf-8",t=JSON.stringify(t)),E||O||(n.headers[a]=T);for(var C in n.headers)if(C===a&&i.isFormData(t))delete n.headers[C];else try{o.setRequestHeader(C,n.headers[C])}catch(e){}o.onload=function(){try{var e=o.response||o.responseText;e&&n.parseJson&&-1!==(o.getResponseHeader(a)||"").indexOf("json")&&!i.isObject(e)&&(e=JSON.parse(e));var t=o.responseHeaders;if(!t){t={};var s=(o.getAllResponseHeaders()||"").split("\r\n");s.pop(),s.forEach(function(e){if(e){var n=e.split(":")[0];t[n]=o.getResponseHeader(n)}})}var c=o.status,u=o.statusText,h={data:e,headers:t,status:c,statusText:u};if(i.merge(h,o._response),c>=200&&c<300||304===c)h.engine=o,h.request=n,r(l.handler,h,0);else{var d=new p(u,c);d.response=h,f(d)}}catch(d){f(new p(d.msg,o.status))}},o.onerror=function(e){f(new p(e.msg||"Network Error",0))},o.ontimeout=function(){f(new p("timeout [ "+o.timeout+"ms ]",1))},o._options=n,setTimeout(function(){o.send(O?null:t)},0)}i.isObject(e)&&(n=e,e=n.url),n=n||{},n.headers=n.headers||{},m(f.p,function(){i.merge(n,JSON.parse(JSON.stringify(r.config)));var o=n.headers;o[a]=o[a]||o[c]||"",delete o[c],n.body=t||n.body,e=i.trim(e||""),n.method=n.method.toUpperCase(),n.url=e;var s=n;p&&(s=p.call(f,n,Promise)||n),d(s)||(s=Promise.resolve(s)),s.then(function(e){e===n?y(e):u(e)},function(e){h(e)})})});return h.engine=o,h}},{key:"all",value:function(e){return Promise.all(e)}},{key:"spread",value:function(e){return function(t){return e.apply(null,t)}}}]),e}();a.default=a,["get","post","put","patch","head","delete"].forEach(function(e){a.prototype[e]=function(t,n,r){return this.request(t,n,i.merge({method:e},r))}}),["lock","unlock","clear"].forEach(function(e){a.prototype[e]=function(){this.interceptors.request[e]()}}),function(e,t){t()}(0,function(){window.fly=new a,window.Fly=a}),e.exports=a}}); + +/**配置fly*/ +function flyConfiguration(fly){ + fly.config.baseURL = "http://admin.zhizous.cn/"; /**配置请求域名*/ + fly.config.headers = { + 'Content-Type': 'application/x-www-form-urlencoded' + }; + + //添加请求拦截器 + fly.interceptors.request.use(function (request) { + //给所有请求添加自定义header + // request.headers["token"]="cd47dc88a1788ebf03580dd3a495408c"; + // request.headers["Authorization"] = 'Bearer ' + "cd47dc88a1788ebf03580dd3a495408c"; + //打印出请求体(发送的参数) + // console.log(request.body); + //终止请求 + //var err=new Error("xxx") + //err.request=request + //return Promise.reject(new Error("")) + + //可以显式返回request, 也可以不返回,没有返回值时拦截器中默认返回request + return request; + }); + + //添加响应拦截器,响应拦截器会在then/catch处理之前执行 + fly.interceptors.response.use( + function (response) { + //只将请求结果的data字段返回 + return response.data + }, + function (err) { + //发生网络错误后会走到这里 + return Promise.resolve(err); + } + ); + return fly; +} + + +layui.define(["jquery"], function (exprots) { + var $ = layui.jquery; + var okFly = flyConfiguration(fly); + + exprots("okFly", okFly); +}); diff --git a/lib/layui/layui.js b/lib/layui/layui.js index 2ca1c66..4bc22ed 100644 --- a/lib/layui/layui.js +++ b/lib/layui/layui.js @@ -29,6 +29,7 @@ if (!Object.assign) { "okTab": "okmodules/okTab", "okCountUp": "okmodules/okCountUp", "okUtils": "okmodules/okUtils", + "okFly": "okmodules/okFly", "okGVerify": "okmodules/okGVerify", "qrcode": "okmodules/qrcode", "okQrcode": "okmodules/okQrcode", diff --git a/pages/help/okFly.html b/pages/help/okFly.html new file mode 100644 index 0000000..8e109ce --- /dev/null +++ b/pages/help/okFly.html @@ -0,0 +1,132 @@ + + + + + okFly + + + + + +
+ +
+

okFly(网络请求)

+
+
+ okFly.get(url, params, options).then(function(res){ + console.log(res); + }); +
+
+/**get请求*/
+okFly.get("/test", {a:1,b:2});
+
+/**post请求*/
+okFly.post("/test", {a:1,b:2}).then(function(res){
+  console.log(res);
+}).catch(function(err) {
+  console.log(err);
+});
+        
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
参数类型描述
urlString请求地址
paramsJSON请求参数
optionsJSON配置项
+ +
+ options: 配置说明 +
+ + + + + + + + + + + + + + + + + + + + +
参数类型描述
headersJSON配置请求头
+
+ + + + diff --git a/pages/help/test.html b/pages/help/test.html index c71896c..1e6ecf5 100644 --- a/pages/help/test.html +++ b/pages/help/test.html @@ -4,8 +4,6 @@ Title - - @@ -14,8 +12,26 @@ dsafasdf