/**
 * @description 工具类 每个项目丢可以
 * */
export default class Utils {

    /*获取数据类型*/
    static getDataType(value){
        let types = {
            '[object Number]': 'number',
            '[object String]': 'string',
            '[object Boolean]': 'boolean',
            '[object Undefined]': 'undefined',
            '[object Object]': 'object',
            '[object Array]': 'array',
            '[object Function]': 'function',
        }
        let type = Object.prototype.toString.call(value)
        return types[type];
    }
    /**
     * 对象转字符串 对字符串进行sort 排序
     * @param data {object} 需要拼接的对象
     * @param connector {string} 连接符
     * */

    static obj2str(data = {}, blank = '=', connector = '&') {
        let str = '';
        let keys = Object.keys(data);
        if (keys.length) {
            let sort_key_arr = keys.sort();
            sort_key_arr.forEach(v=>{
                str += connector + v + blank + data[v];
            });
            str = str.slice(1);
        }
        return str;
    }
    static objClone(obj) {
       let str = JSON.stringify(obj);
        return JSON.parse(str);
    }

    /*字符串转对象*/
    static str2obj(str, blank = '=', connector = '&') {
        let obj = {};
        if (str) {
            let arr = str.split(connector);

            arr.forEach(v => {
                let item = v.split(blank);
                obj[item[0]] = item[1] || '';
            });
        }
        return obj;
    }

    /*字符串切割成指定长度的字符串数组*/
    static strSlice2arr(str,count = 1) {
        str = String(str);
        let len = str. length;

        let str_arr = [];
        let s = 0;
        let times = Math.ceil(len / count);

        for (let i = 0; i < times; i++) {
            str_arr.push(str.slice(s,s + count));
            s = s + count;
        }

        return str_arr;
    }
    /*获取对象的值 可以通过a.b.c*/
    static getObjKey(obj, key) {
        let re = obj;
        if (key) {
            let is_point = key.includes('.');
            if (is_point) {
                let arr = key.split('.');
                arr.forEach(v => {
                    re = re[v];
                });
            } else {
                re = obj[key];
            }
        }
        return re;
    }
    /*设置获取对象的值 可以通过a.b.c*/
    static setObjKey(obj, key ,value) {
        let re = obj;
        if (key) {
            let is_point = key.includes('.');
            if (is_point) {
                let arr = key.split('.');
                arr.forEach((v) => {
                    if( this.getDataType( re[v]) === 'object' ){
                        re = re[v]
                    }else {
                        re[v] = value
                    }
                });
            } else {
                obj[key] = value;
            }
        }

    }

    /*校验格式*/
    static checkParams(val = '', type = 'pwd') {
        let re = {
            is_check: false,//是否通过校验
            msg: ''//提示

        };
        let r = '';

        switch (type) {
            case 'tel'://手机号类型检测
                if (typeof val !== 'string') {
                    re.msg = '数据类型错误';
                }
                if (val.length !== 11) {
                    re.msg = '手机号长度错误';
                } else {
                    if (((/^1[3456789]\d{9}$/).test(val))) {
                        re.is_check = true;
                        re.msg = '';
                    }else {
                        re.msg = '手机号格式错误';
                    }


                    // if(val[0] ==='1'){
                    //     re.is_check=true;
                    //     re.msg='';
                    // }
                }


                break;

            case 'pwd':
                r = (/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,20}$/).test(val);
                if (r) {
                    re.is_check = true;
                    re.msg = '';
                } else {
                    re.is_check = false;
                    re.msg = '密码必须是6-20位字符,必须英文,数字,特殊字符两种及以上的混合';
                }


                break;
        }


        return re;
    }

    /**
     * @description 检测对象是否有包含关系
     * @return boolean
     * */
    static objInner(big_obj, inner_obj) {
        for (let i in inner_obj) {
            if (inner_obj[i] !== big_obj[i]) {
                return false;
            }
        }
        return true;
    }

    /*给数组对象的某个子对象 的某个属性赋值*/
    static editObjField(arr_obj, obj, new_obj) {
        for (let i = 0, len = arr_obj.length; i < len; i++) {
            let item = arr_obj[i];

            // 对象检测是否存在要修改的列

            let is_inner = this.objInner(arr_obj[i], obj);

            if (is_inner) {
                Object.assign(item, new_obj);
            }
        }
        // console.log(arr_obj)
    }


    /*值转化为数字*/
    static val2Number(val, fix) {
        // console.log(val,fix)
        let num = '';

        // if(fix){
        //
        //     num = parseFloat(val);
        //     if(this.isNunPoint(num)){
        //         num=num.toFixed(fix)
        //     }
        // }else {
        num = parseInt(val);
        // }


        // console.log(num)
        if (isNaN(num)) {
            num = '';
        }
        return num;
    }

    /*拼接地址*/
    static joinUrl(url, data = {}) {
        /*检测url 是否有参数 对原始参数不影响*/

        let is_w = url.includes('?');//地址中是否包含问号 http://asda.com?a=1 http://asda.com
        let url_split = url.split('?'),
            base_url = url_split[0],
            search_str = url_split[1] || '';


        let obj = this.str2obj(search_str, '=', '&');

        let new_obj = {...obj, ...data};

        return base_url + '?' + this.obj2str(new_obj, '=', '&');
    }

    /*获取时间格式
    * alert(new Date().format("yyyy年MM月dd日"));
alert(new Date().format("MM/dd/yyyy"));
alert(new Date().format("yyyyMMdd"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
    * */
    static getDateTime(fmt,time) {

        if(this.getDataType(time) === 'string'){
            time = time.replace(/\-/g, '/')
        }

        let date = new Date(time || Date.now());
        var o = {
            'M+': date.getMonth() + 1, //月份
            'd+': date.getDate(), //日
            'h+': date.getHours(), //小时
            'm+': date.getMinutes(), //分
            's+': date.getSeconds(), //秒
            'q+': Math.floor((date.getMonth() + 3) / 3), //季度
            'S': date.getMilliseconds() //毫秒
        };

        if ((/(y+)/).test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(
                    RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
            }
        }
        return fmt;
    }

    /**
     * @description 获取倒计时格式时间
     * @param{number} val 时长 秒
     * @param{string} symbol 连接符
     * */
    static getTimeLong(val, symbol = ' : ') {
        /*获取秒*/
        let seconds = val % 60;
        // 获取分钟
        let minutes = parseInt(val / 60)%60;
        //获取小时
        let hours = parseInt(val / 3600);


        seconds = seconds < 10 ? '0' + seconds : seconds;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        hours = hours < 10 ? '0' + hours : hours;
        console.log(111,hours,seconds,seconds)
        let str = '';

        if (hours/1) {
            str = hours + symbol

        }

        str = str+ minutes + symbol + seconds;
        console.log(str)
        return str;


    }
    /*计算两个时间相差的时间戳
    * data1 结束时间 开始时间
    * */
    static getReduceTime(data1,data2 = new Date().toString()){
        data1 = data1.replace(/-/g, '/');
        data2 = data2.replace(/-/g, '/');

        data1 = new Date(data1).getTime();
        data2 = new Date(data2).getTime();

        return data1 - data2

    }

    /*获取年月的天数*/
    static getMonthDays(year, month) {
        var next = new Date(year, month, 1, 0, 0, 0) //获取想要获取的那个月的后一个月第一天的时间戳
        var now = new Date(next-1000) //往回波一秒
        return now.getDate();// 波一秒之后就是想要获取的那个月的时间戳
    }

    /*获取地址参数*/
    static getUrlQuery(url){
        let url_search = url.split('?')[1];
        let re = {};
        if(!url_search){
            return re;
        }
        let url_search_arr = url_search.split('&');
        url_search_arr.forEach(v=>{
            let val_arr = v.split('=');
            re[val_arr[0]] = val_arr[1];
        });
        return  re;





    }


    /*创建两个数之间的数组
    * */
    static getArrRange(min,max){
        let re = [];
        for(var i = min;i<=max;i++){
            re.push(i);
        }
        return re;

    }
}