haoutil
Version:
8 lines • 10.1 kB
JavaScript
/**
* JS常用静态方法类库
* 源码地址:https://github.com/muyao1987/haoutil
* 版本信息:v2.6.0
* 编译日期:2022-1-10 14:57:17
* 版权所有:Copyright by 火星科技 木遥 http://marsgis.cn
*/
var haoutil=haoutil||{};"object"==typeof exports?exports.haoutil=haoutil:window.haoutil=haoutil,haoutil.version="2.6.0",haoutil.update="2022-01-10",haoutil.name="木遥 通用常用JS方法类库",haoutil.author="木遥 , 微信: http://marsgis.cn/weixin.html",haoutil.website="https://github.com/muyao1987/haoutil",haoutil.msg=function(t){haoutil.isutil.isNull(t)?t="未知":"object"==typeof t&&(t=JSON.stringify(t)),window.toastr?toastr.info(t):window.layer?window.layer.msg(t):alert(t)},haoutil.tip=haoutil.msg,haoutil.oneMsg=function(t,e){haoutil.storage.get(e)||(haoutil.msg(t),haoutil.storage.add(e,!0))},haoutil.alert=function(t,e){haoutil.isutil.isNull(t)?t="未知":"object"==typeof t&&(t=JSON.stringify(t)),window.layer?window.layer.alert(t,{title:e||"提示",skin:"layui-layer-lan layer-mars-dialog",closeBtn:0,anim:0}):alert(t)},haoutil.loading={index:-1,show:function(t){this.close(),window.NProgress?((t=t||{}).color?t.template='<div class="bar '+(t.className||"")+'" style="background-color:'+t.color+';" role="bar"></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>':t.template='<div class="bar '+(t.className||"")+'" role="bar"></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div>',window.NProgress.configure(t),window.NProgress.start()):window.layer&&(this.index=window.layer.load(2,{shade:[.3,"#000000"]}))},hide:function(){this.close()},close:function(){window.NProgress?window.NProgress.done(!0):window.layer&&(-1!=this.index&&window.layer.close(this.index),this.index=-1)}},String.prototype.startsWith=String.prototype.startsWith||function(t){return this.slice(0,t.length)==t},String.prototype.endsWith=String.prototype.endsWith||function(t){return this.slice(-t.length)==t},String.prototype.replaceAll=String.prototype.replaceAll||function(t,e){return this.replace(new RegExp(t,"gm"),e)},Date.prototype.format=function(t){var e,n={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours()%12==0?12:this.getHours()%12,"H+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(e in/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),/(E+)/.test(t)&&(t=t.replace(RegExp.$1,(1<RegExp.$1.length?2<RegExp.$1.length?"星期":"周":"")+{0:"日",1:"一",2:"二",3:"三",4:"四",5:"五",6:"六"}[this.getDay()+""])),n)new RegExp("("+e+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?n[e]:("00"+n[e]).substr((""+n[e]).length)));return t},haoutil.array={indexOf:function(t,e){for(var n=0;n<t.length;n++)if(t[n]==e)return n;return-1},remove:function(t,e){for(var n=0;n<t.length;n++)if(t[n]==e){t.splice(n,1);break}},insert:function(t,e,n){t.splice(n=null==n?0:n,0,e)}},haoutil.color={random:function(){return"#"+function(t){return(t+="0123456789abcdef"[Math.floor(16*Math.random())])&&6==t.length?t:arguments.callee(t)}("")}},haoutil.cookie=function(){var i;return{isH5Mobile:function(t){i=t},add:function(t,e,n){0<n?(o=new Date).setTime(o.getTime+24*n*60*60*1e3):o=new Date(2147483647e3);var o=t+"="+escape(e)+";path=/;expires="+o.toGMTString();i&&null!=window.plus?window.plus.navigator.setCookie(t,o):document.cookie=o},get:function(t){var e;if(i&&null!=window.plus){if(null==(e=window.plus.navigator.getCookie(t)))return null}else e=document.cookie;for(var n=e.split("; "),o=0;o<n.length;o++){var r=n[o].split("=");if(r[0]==t)return unescape(r[1])}return null},del:function(t){var e;i&&null!=window.plus?window.plus.navigator.removeCookie(t):((e=new Date).setTime(e.getTime()-1e4),document.cookie=t+"=v; expires="+e.toGMTString())}}}(),haoutil.file=function(){function n(t,e){var n=document.createElement("a");n.download=t,n.href=URL.createObjectURL(e),document.body.appendChild(n),n.click(),document.body.removeChild(n)}function o(t){for(var e=t.split(";base64,"),t=e[0].split(":")[1],n=window.atob(e[1]),o=n.length,r=new Uint8Array(o),i=0;i<o;++i)r[i]=n.charCodeAt(i);return new Blob([r],{type:t})}return{download:n,downloadFile:function(t,e){n(t,new Blob([e]))},downloadImage:function(t,e){n(t+".png",o(e.toDataURL("image/png")))},downloadBase64Image:function(t,e){n(t+".png",o(e))},base64Img2Blob:o}}(),haoutil.isutil=function(){function e(t){return"string"==typeof t&&t.constructor==String}function n(t){return"number"==typeof t&&t.constructor==Number}function o(t){return null==t||(!(!e(t)||""!=t)||!(!n(t)||!isNaN(t)))}return{isNull:o,isNotNull:function(t){return!o(t)},isArray:function(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===Object.prototype.toString.call(t)},isString:e,isNumber:n,isDate:function(t){return"object"==typeof t&&t.constructor==Date},isFunction:function(t){return"function"==typeof t&&t.constructor==Function},isObject:function(t){return"[object Object]"===Object.prototype.toString.call(t)}}}(),haoutil.math=function(){function e(t,e){return Math.floor(Math.random()*(e-t+1)+t)}return{getArrayRandomOne:function(t){return t[e(0,t.length-1)]},random:e,padLeft0:function(t,e){let n=(t=String(t)).length;for(;n<e;)t="0"+t,n++;return t}}}(),haoutil.storage=function(){var n;return{add:function(t,e){null!=(n=window.localStorage)&&n.setItem(t,e)},get:function(t){if(null!=(n=window.localStorage))return n.getItem(t)},del:function(t){null!=(n=window.localStorage)&&n.removeItem(t)}}}(),haoutil.str=function(){var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");return{isChinese:function(t){return!!/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi.exec(t)},formatLength:function(t,e){if(null==t)return"";t=Number(t);var n="";switch(e=null==e||"auto"==e?t<1e3?"m":"km":e){default:case"m":n=t.toFixed(2)+"米";break;case"km":n=(.001*t).toFixed(2)+"公里";break;case"mile":n=(54e-5*t).toFixed(2)+"海里";break;case"zhang":n=(.3*t).toFixed(2)+"丈"}return n},formatArea:function(t,e){if(null==t)return"";t=Number(t);var n="";switch(e=null==e||"auto"==e?t<1e6?"m":"km":e){default:case"m":n=t.toFixed(2)+"平方米";break;case"km":n=(t/1e6).toFixed(2)+"平方公里";break;case"mu":n=(.0015*t).toFixed(2)+"亩";break;case"ha":n=(1e-4*t).toFixed(2)+"公顷"}return n},formatTime:function(t){if((t=Number(t)||0)<60)return t.toFixed(0)+"秒";if(60<=t&&t<3600){var e=Math.floor(t%60);return Math.floor(t/60)+"分钟"+(0!=e?e+"秒":"")}return t=Math.floor(t/60),Math.floor(t/60)+"小时"+Math.floor(t%60)+"分钟"},base64:function(t){return function(t){for(var e="",n=t.length%6,o=t.substr(0,t.length-n),t=t.substr(t.length-n,n),r=0;r<o.length;r+=6){var i=parseInt(o.substr(r,6),2);e+=a[i]}return t+=new Array(7-n).join("0"),n&&(e+=a[parseInt(t,2)],e+=new Array((6-n)/2+1).join("=")),e}(function(t){for(var e="",n=0;n<t.length;n++){var o=t.charCodeAt(n).toString(2);e+=new Array(9-o.length).join("0")+o}return e}(t))},decodeBase64:function(t){return function(t){for(var e="",n=0;n<t.length;n+=8)e+=String.fromCharCode(parseInt(t.substr(n,8),2));return e}(function(t){for(var e,n="",o=0,r=0;r<t.length;r++)"="!=t[r]?(e=a.indexOf(t[r]).toString(2),n+=new Array(7-e.length).join("0")+e):o++;return n.substr(0,n.length-2*o)}(t))}}}(),haoutil.system=function(){let o=document.head||document.getElementsByTagName("head")[0];function n(t,e){let n=document.createElement("link");n.rel="stylesheet",n.async=e,n.href=t,o.appendChild(n)}function r(t,e){let n=document.createElement("script");n.charset="utf-8",n.async=e,n.src=t,o.appendChild(n)}let i=new RegExp("\\.css");return{getRequest:function(t){let o=new Object;try{let n=(t=t||window).location.search;if(-1!=n.indexOf("?")){let t=n.substr(1),e=t.split("&");for(let t=0;t<e.length;t++)o[e[t].split("=")[0]]=decodeURI(e[t].split("=")[1])}}catch(t){}return o},getRequestByName:function(t,e,n){try{n=n||window;var o=new RegExp("(^|&)"+t+"=([^&]*)(&|$)","i"),r=n.location.search.substr(1).match(o);if(null!=r)return decodeURI(r[2])}catch(t){}return e},getExplorerInfo:function(){let t=window.navigator.userAgent.toLowerCase();return 0<=t.indexOf("msie")?{type:"IE",version:Number(t.match(/msie ([\d]+)/)[1])}:0<=t.indexOf("firefox")?{type:"Firefox",version:Number(t.match(/firefox\/([\d]+)/)[1])}:0<=t.indexOf("chrome")?{type:"Chrome",version:Number(t.match(/chrome\/([\d]+)/)[1])}:0<=t.indexOf("opera")?{type:"Opera",version:Number(t.match(/opera.([\d]+)/)[1])}:0<=t.indexOf("Safari")?{type:"Safari",version:Number(t.match(/version\/([\d]+)/)[1])}:{type:t,version:-1}},isPCBroswer:function(){let t=navigator.userAgent.toLowerCase();var e="ipad"==t.match(/ipad/i),n="iphone"==t.match(/iphone/i),o="midp"==t.match(/midp/i),r="rv:1.2.3.4"==t.match(/rv:1.2.3.4/i),i="ucweb"==t.match(/ucweb/i),a="android"==t.match(/android/i),l="windows ce"==t.match(/windows ce/i),u="windows mobile"==t.match(/windows mobile/i);return!(e||n||o||r||i||a||l||u)},clone:function o(r,i,a){if(null==a&&(a=9),null==i&&(i=["_layer"]),null==r||"object"!=typeof r)return r;if(haoutil.isutil.isDate(r)){let t=new Date;return t.setTime(r.getTime()),t}if(haoutil.isutil.isArray(r)&&0<=a){let n=[];for(let t=0,e=r.length;t<e;++t)n[t]=o(r[t],i,a-1);return n}if("object"==typeof r&&0<=a)try{let t={};for(var e in r)"function"!=typeof e&&-1==i.indexOf(e)&&r.hasOwnProperty(e)&&(t[e]=o(r[e],i,a-1));return t}catch(t){console.log(t)}return r},jsonp:function(t,e,n){window.$jsonp=function(t,e,n){var o,r="my_json_cb_"+Math.random().toString().replace(".","");window[r]=n;let i=-1==t.indexOf("?")?"?":"&";for(o in e)i+=o+"="+e[o]+"&";i+="callback="+r;let a=document.createElement("script");a.src=t+i,document.body.appendChild(a)}},getWindowSize:function(){return void 0!==window.innerWidth?{width:window.innerWidth,height:window.innerHeight}:{width:document.documentElement.clientWidth,height:document.documentElement.clientHeight}},getHtml:function(t,e){$.ajax({url:t,type:"GET",dataType:"html",timeout:0,success:function(t){e(t)}})},loadCss:n,loadJs:r,loadResource:function(t,e){(i.test(t)?n:r)(t,e)}}}();