lvl-common
Version:
通用逻辑功能模块。包含AjaxService、clone、dataFormat、queryString、clipboard
2 lines • 8.47 kB
JavaScript
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("axios"),require("lvl-common-base"));else if("function"==typeof define&&define.amd)define(["axios","lvl-common-base"],t);else{var n="object"==typeof exports?t(require("axios"),require("lvl-common-base")):t(e.axios,e["lvl-common-base"]);for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}("undefined"!=typeof window?window:this,(function(e,t){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=5)}([function(t,n){t.exports=e},function(e,n){e.exports=t},function(e,t,n){"use strict";n.r(t),t.default=function(e,t){if("function"!=typeof document.execCommand)return!1;switch(t){case"cut":case"copy":break;default:t="copy"}var n=document.createElement("input");n.setAttribute("readonly","readonly"),n.setAttribute("value",e),n.style.position="absolute",n.style.top="-10000px",document.body.appendChild(n),n.focus(),n.setSelectionRange(0,e.length);var r=!1;try{r=document.execCommand(t)}catch(e){}return document.body.removeChild(n),r}},function(e,t,n){"use strict";function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}n.r(t);var o=function(){function e(){var t=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.search=this.getLocationSearch();var n=this.parse();this.getQuery=function(){var e=t.getLocationSearch();return t.search!==e&&(t.search=e,n=t.parse()),n}}var t,n,o;return t=e,(n=[{key:"getLocationSearch",value:function(){return"undefined"==typeof window?"":window.location.search}},{key:"parse",value:function(e){"string"!=typeof e&&(e=this.getLocationSearch().slice(1));var t=e.split("&"),n={};return t.forEach((function(e){var t=e.split("=");n[t[0]]=t[1]})),n}},{key:"stringify",value:function(e){var t=[];return Object.keys(e).forEach((function(n){t.push("".concat(n,"=").concat(e[n]))})),t.join("&")}}])&&r(t.prototype,n),o&&r(t,o),e}();t.default=new o},function(e,t,n){"use strict";n.r(t);var r=n(0),o=n.n(r);function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var s=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.interceptors={request:{},response:{}},this.defaults={headers:{common:{"Content-Type":"application/json"}}},this.initInterceptors(),this.initRequestMethods()}var t,n,r;return t=e,(n=[{key:"initInterceptors",value:function(){var e=function(e,t){this._hooks.push({success:e,fail:t})};this.interceptors.request={_hooks:[],use:e},this.interceptors.response={_hooks:[],use:e}}},{key:"initRequestMethods",value:function(){var e=this;["post","delete","put","get"].forEach((function(t){e[t]=function(n,r){var o={url:n,header:e.defaults.headers.common,data:r,method:t.toUpperCase()};return e.interceptors.request._hooks.forEach((function(e){o=e.success(o)})),new Promise((function(t,n){o.success=function(n){e.interceptors.response._hooks.forEach((function(e){n=e.success(n)})),t(n)},o.fail=function(t){e.interceptors.response._hooks.forEach((function(e){t=e.fail(t)})),n(t)},wx.request(o)}))}}))}}])&&i(t.prototype,n),r&&i(t,r),e}();function u(e){return(u="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)}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var a=function(){function e(t){var n=this;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.contentType=t,this.request=null,this.cancelFun=function(){},this.cancelToken="",this.isMiniApp="object"===("undefined"==typeof wx?"undefined":u(wx))&&"function"==typeof wx.canIUse&&wx.canIUse("request")&&"function"==typeof wx.request,this.isMiniApp)this.request=new s;else{var r=new o.a.CancelToken((function(e){n.cancelFun=e}));this.request=o.a.create({cancelToken:r}),this.request.interceptors.request.use((function(e){var t=e.data;if("object"!==u(t)||!t)return e;switch(t=JSON.parse(JSON.stringify(t)),n.contentType){case"json":break;case"form":default:var r=[];Object.keys(t).forEach((function(e){var n=t[e];r.push("".concat(e,"=").concat(n))})),t=r.join("&")}return e.data=t,e}));var i=this.request.get;this.request.get=function(e,t){return i.call(n.request,e,{params:t})};var c=this.request.delete;this.request.delete=function(e,t){return c.call(n.request,e,{params:t})}}}var t,n,r;return t=e,r=[{key:"isCancel",value:function(e){return!this.isMiniApp&&o.a.isCancel(e)}}],(n=null)&&c(t.prototype,n),r&&c(t,r),e}();function f(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function l(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var p="string"==typeof __HOST__?__HOST__:"",h=function(){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"form",r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:p;f(this,e),this.requestInstance=null,this.cancel=function(){},this.originalUrl=t||r+"{/api}{/version}{/module}{/action}{/id}",this.sections=[],this.init(n)}var t,n,r;return t=e,(n=[{key:"init",value:function(e){this.requestInstance=new a(e).request,this.cancel=this.requestInstance.cancelFun,this.setHeader("Content-Type","json"===e?"application/json":"application/x-www-form-urlencoded"),this.injectResponseHook((function(e){return e.data}),(function(e){return a.isCancel(e)?{type:"cancel",body:e}:e.request?{type:"request",body:e.request}:e.response?{type:"response",body:e.response}:{type:"other",body:e}}));for(var t=/\{\/([^}]+)\}/g;t.lastIndex<this.originalUrl.length;){var n=t.exec(this.originalUrl);if(!n)break;this.sections.push(n)}this.initHTTPMethod()}},{key:"setHeader",value:function(e,t,n){e&&t&&(n=n||"common",this.requestInstance.defaults.headers[n][e]=t)}},{key:"injectRequestHook",value:function(e){"function"==typeof e&&this.requestInstance.interceptors.request.use((function(t){return e(t)}))}},{key:"injectResponseHook",value:function(e,t){this.requestInstance.interceptors.response.use((function(t){if("function"!=typeof e)return t;var n=e(t);return void 0!==n?n:void 0}),(function(e){if("function"!=typeof t)return Promise.reject(e);var n=t(e);return void 0!==n?n instanceof Promise?n:Promise.reject(n):void 0}))}},{key:"initHTTPMethod",value:function(){var e=this;["post","delete","put","patch","get"].forEach((function(t){"function"==typeof e.requestInstance[t]&&(e[t]=function(n,r){return e.requestInstance[t](e.urlMatch(n),r)})}))}},{key:"urlMatch",value:function(e){var t=this,n=this.originalUrl;return e.hasOwnProperty("host")&&(n=n.replace(/http[s]?:\/\/[^{]+/,e.host)),this.sections.forEach((function(r){var o=r[1],i=e.hasOwnProperty(o)?e[o]:t[o];i=i?"/"+i:"";var s=new RegExp(r[0]);n=n.replace(s,i)})),n}}])&&l(t.prototype,n),r&&l(t,r),e}();t.default=h},function(e,t,n){"use strict";n.r(t);var r=n(1);n.d(t,"clone",(function(){return r.clone})),n.d(t,"dataFormat",(function(){return r.dataFormat}));var o=n(4);n.d(t,"AjaxService",(function(){return o.default}));var i=n(2);n.d(t,"clipboard",(function(){return i.default}));var s=n(3);n.d(t,"queryString",(function(){return s.default}))}])}));
//# sourceMappingURL=index.js.map