js 工具类方法记录

/**
 * 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值