class Utils {
static formmatData(formmat
='Y-M-D h:m:s', timestamp
= Date
.now()){
let date
= new Date(timestamp
)
let deteInfo
= {
Y: date
.getFullYear(),
M: date
.getMonth()+1,
D: date
.getDate(),
h
: date
.getHours(),
m
: date
.getHours(),
s
: date
.getSeconds()
}
let formatNumber = (n
) => n
> 10 ? n
: '0' + n
;
let res
= formmat
.replace('Y', deteInfo
.Y)
.replace('M', formatNumber(deteInfo
.M))
.replace('D', formatNumber(deteInfo
.D))
.replace('h', formatNumber(deteInfo
.h
))
.replace('m', formatNumber(deteInfo
.m
))
.replace('s', formatNumber(deteInfo
.s
))
return res
;
}
static xncs(){
setTimeout(()=>{
let t
= performance
.timing
,
m
= performance
.memory
console
.table({
'DNS查询耗时': (t
.domainLookupEnd
- t
.domainLookupStart
).toFixed(0),
'TCP链接耗时': (t
.connectEnd
- t
.connectStart
).toFixed(0),
'request请求耗时': (t
.responseEnd
- t
.responseStart
).toFixed(0),
'解析dom树耗时': (t
.domComplete
- t
.domInteractive
).toFixed(0),
'白屏时间': (t
.responseStart
- t
.navigationStart
).toFixed(0),
'domready时间': (t
.domContentLoadedEventEnd
- t
.navigationStart
).toFixed(0),
'onload时间': (t
.loadEventEnd
- t
.navigationStart
).toFixed(0),
'js内存使用占比': m
? (m
.usedJSHeapSize
/ m
.totalJSHeapSize
* 100).toFixed(2) + '%' : undefined
})
})
}
static debounce(func
, wait
=300, immediate
){
let timer
, ctx
;
let later = (arg
) => setTimeout(() => {
func
.apply(ctx
, arg
)
timer
= ctx
= null
}, wait
);
return function(...arg
){
if(!timer
){
timer
= later(arg
)
ctx
= this
if(immediate
){
func
.apply(ctx
,arg
)
}
}else{
clearTimeout(timer
)
timer
= later(arg
)
}
}
}
static throttle(func
, delay
){
let timer
= null
return function(...arg
){
if(!timer
){
timer
= setTimeout(() => {
func
.apply(this, arg
)
timer
= null
}, delay
)
}
}
}
static downloadFile(filename
, data
){
let downloadLink
= document
.createElement('a');
if ( downloadLink
){
document
.body
.appendChild(downloadLink
);
downloadLink
.style
= 'display: none';
downloadLink
.download
= filename
;
downloadLink
.href
= data
;
if ( document
.createEvent
){
let downloadEvt
= document
.createEvent('MouseEvents');
downloadEvt
.initEvent('click', true, false);
downloadLink
.dispatchEvent(downloadEvt
);
} else if ( document
.createEventObject
) {
downloadLink
.fireEvent('onclick');
} else if (typeof downloadLink
.onclick
== 'function' ) {
downloadLink
.onclick();
}
document
.body
.removeChild(downloadLink
);
}
}
static isPCBroswer() {
let e
= window
.navigator
.userAgent
.toLowerCase()
, t
= "ipad" == e
.match(/ipad/i)
, i
= "iphone" == e
.match(/iphone/i)
, r
= "midp" == e
.match(/midp/i)
, n
= "rv:1.2.3.4" == e
.match(/rv:1.2.3.4/i)
, a
= "ucweb" == e
.match(/ucweb/i)
, o
= "android" == e
.match(/android/i)
, s
= "windows ce" == e
.match(/windows ce/i)
, l
= "windows mobile" == e
.match(/windows mobile/i);
return !(t
|| i
|| r
|| n
|| a
|| o
|| s
|| l
)
}
static getPlatformInfo(){
let inBrowser
= typeof window
!== 'undefined';
let inWeex
= typeof WXEnvironment
!== 'undefined' && !!WXEnvironment
.platform
;
let weexPlatform
= inWeex
&& WXEnvironment
.platform
.toLowerCase();
let UA = inBrowser
&& window
.navigator
.userAgent
.toLowerCase();
if(UA){
let platforms
= {
IE: /msie|trident/.test(UA),
IE9: UA.indexOf('msie 9.0') > 0,
Edge
: UA.indexOf('edge/') > 0,
Android
: UA.indexOf('android') > 0 || (weexPlatform
=== 'android'),
IOS: /iphone|ipad|ipod|ios/.test(UA) || (weexPlatform
=== 'ios'),
Chrome
: /chrome\/\d+/.test(UA) && !(UA.indexOf('edge/') > 0),
}
for (const key
in platforms
) {
if (platforms
.hasOwnProperty(key
)) {
if(platforms
[key
]) return key
}
}
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-7618.html