// 判空
export function isEmpty(val) {
    if (typeof val === 'undefined' || val == null || val === '') {
        return true;
    } else {
        return false;
    };
};

// 将base64转换为文件
export function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(',');
    var mime = arr[0].match(/:(.*?);/)[1];
    var bstr = atob(arr[1]);
    var n = bstr.length;
    var u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    };
    return new File([u8arr], filename, {type: mime});
};

// 证件号校验
export function validateCard(type, val) {
    if (type === 1) {
        let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/g;
        if (reg.test(val)) {
            return false;
        } else {
            return true;
        };
    } else if (type === 2) {
        
        let reg = /^([a-zA-z]|[0-9]){5,17}$/g;
        if (reg.test(val)) {
            return false;
        } else {
            return true;
        };
    };
};

// 手机号校验
export function validatePhone(val) {
    let reg = /^1(?:3[0-9]|4[5-9]|5[0-9]|6[12456]|7[0-8]|8[0-9]|9[0-9])[0-9]{8}$/g;
    if (reg.test(val)) {
        return false;
    } else {
        return true;
    };
};


// 判断ios还是安卓
export function isIOSWebKit() {
    const aa = window.navigator.userAgent;
    if (!!aa.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {// ios端
        return true;
    } else if (aa.indexOf('Android') !== -1 || aa.indexOf('Adr') !== -1) {// 安卓端
        return false;
    }
}

//当前方法传参数两种 字符串或者map
export function callMobile(handlerInterface, parameters) {
    let classStr = Object.prototype.toString.call(parameters);
    if (classStr === '[object String]' || classStr === '[object Object]') {
        let param = parameters;
        if (classStr === "[object Object]") { //判断传参是str ,还是object
            //handlerInterface由iOS addScriptMessageHandler与andorid addJavascriptInterface 代码注入而来。
            param = JSON.stringify(parameters);//参数必须转化成json格式
        }
        try {
            if (isIOSWebKit()) {//ios
                if (window.webkit !== undefined) {
                    if (param == '{}') {
                        window.webkit.messageHandlers[handlerInterface].postMessage(null);
                    } else {
                        window.webkit.messageHandlers[handlerInterface].postMessage(param);
                    }
                }
            } else if (isIOSWebKit() === false) {
                //安卓传输不了js json对象，只能传输string
                if (param == '{}') {
                    window.H5page[handlerInterface]();
                } else {
                    window.H5page[handlerInterface](param);
                }
            }
        } catch (e) {

        }
    }
}
export function  getDateStr(date) {
    let mouth = date.getMonth() + 1;
    let day = date.getDate();
    if (mouth < 10) {
        mouth = "0" + mouth;
    }
    if (day < 10) {
        day = "0" + day;
    }
    let resultDateStr = date.getFullYear() + "-" + mouth + "-" + day;//将日期转化为字符串格式
    return resultDateStr;
}