/**
* Created by zyn on 2019/3/22.
*/
const Utils = {
getTime(dateTimeStr) {
dateTimeStr = dateTimeStr.substring(0, 19);
dateTimeStr = dateTimeStr.replace(/-/g, '/');
return new Date(dateTimeStr).getTime();
},
// js 元素绑定事件兼容写法
addEvent(element, eType, handle, bol = false) {
if (element.addEventListener) {
element.addEventListener(eType, handle, bol);
} else if (element.attachEvent) {
element.attachEvent('on' + eType, handle);
} else {
element['on' + eType] = handle;
}
},
// 获取当前时间
getCurrentDate() {
const date = new Date();
const seperator1 = '-';
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = '0' + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate;
}
return date.getFullYear() + seperator1 + month + seperator1 + strDate;
},
// 获取js样式的句柄
getComputedStyle(el) {
let computedStyle = null;
if (window.getComputedStyle) {
computedStyle = getComputedStyle(el, null);
} else {
computedStyle = el.currentStyle;
}
return computedStyle;
},
// 检查对象中是否存在 指定key值
hasOwn(obj, key) {
return hasOwnProperty.call(obj, key);
},
extend(to, _from) {
for (let key in _from) {
to[key] = _from[key];
}
return to;
},
toObject(arr) {
let res = {};
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
extend(res, arr[i]);
}
}
return res;
},
// js 检查地址栏参数
getQueryString() {
const { href } = location;
let param = href.indexOf('?') !== -1 ? href.split('?')[1] : {};
let _obj = {};
Object.keys(param).length &&
param.split('&').forEach(item => {
const [key, value] = item.split('=');
return (_obj[key] = value);
});
return _obj;
},
// 检查数组中是否存在指定的参数
checkInjectInfo(inject = []) {
const list = ['appId', 'systemSource'].concat(inject);
const parma = this.getQueryString();
return (
list.filter(item => {
if (parma[item]) return parma[item];
}).length === list.length
);
}
};
export default Utils;
js 工具类方法记录
最新推荐文章于 2024-05-14 14:30:37 发布