/**
 * 判断最大长度
 * targetValue:传入的数组或者字符串
 * targetLength:允许的长度
 */
export function maxLength(targetValue,targetLength){
    if(typeof targetValue == "string" || targetValue instanceof Array){
        if(targetValue.length > targetLength){
            return false
        }else{
            return true
        }
    }else{
        return "传入的数据并非数组或者字符串"
    }
}

/**
 * 正则匹配统一社会信用代码
 * code:十八位统一社会信用代码(字符串)
 */
 export function matchDepartCode(code){
    var reg = /^[0-9A-Z]{18}$/;
    if(code.length != 18){
        //统一社会信用代码长度必须为十八位
        return 0
    }else if(!reg.test(code)){
        //统一社会信用代码不能有汉字或者中间有空格
        return 1
    }else{
        var codeArr = code.split('');
        //计算基数与权重相乘后相加的值
        var sumNum = 0;
        //除数
        var number = 31;
        //获取统一社会信用代码最后一位
        var codeLastNum = code.substr(code.length - 1,1);
        for(var x = 0,len = codeArr.length - 1;x < len;x++){
            //统一社会信用代码不能含有以下五个字母
            if(codeArr[x] == 'I' || codeArr[x] == 'O' || codeArr[x] == 'Z' || codeArr[x] == 'S' || codeArr[x] == 'V'){
                return 2
            //其余数字转换
            }else if(codeArr[x] == 'A'){
                codeArr[x] = '10'
            }else if(codeArr[x] == 'B'){
                codeArr[x] = '11'
            }else if(codeArr[x] == 'C'){
                codeArr[x] = '12'
            }else if(codeArr[x] == 'D'){
                codeArr[x] = '13'
            }else if(codeArr[x] == 'E'){
                codeArr[x] = '14'
            }else if(codeArr[x] == 'F'){
                codeArr[x] = '15'
            }else if(codeArr[x] == 'G'){
                codeArr[x] = '16'
            }else if(codeArr[x] == 'H'){
                codeArr[x] = '17'
            }else if(codeArr[x] == 'J'){
                codeArr[x] = '18'
            }else if(codeArr[x] == 'K'){
                codeArr[x] = '19'
            }else if(codeArr[x] == 'L'){
                codeArr[x] = '20'
            }else if(codeArr[x] == 'M'){
                codeArr[x] = '21'
            }else if(codeArr[x] == 'N'){
                codeArr[x] = '22'
            }else if(codeArr[x] == 'P'){
                codeArr[x] = '23'
            }else if(codeArr[x] == 'Q'){
                codeArr[x] = '24'
            }else if(codeArr[x] == 'R'){
                codeArr[x] = '25'
            }else if(codeArr[x] == 'T'){
                codeArr[x] = '26'
            }else if(codeArr[x] == 'U'){
                codeArr[x] = '27'
            }else if(codeArr[x] == 'W'){
                codeArr[x] = '28'
            }else if(codeArr[x] == 'X'){
                codeArr[x] = '29'
            }else if(codeArr[x] == 'Y'){
                codeArr[x] = '30'
            }
        };
        //转换后的数字乘以每个数字所在位置的的权重
        for(var y = 0,len = codeArr.length - 1;y < len;y++){
            if(y == 0){
                sumNum += codeArr[0] * 1;
            }else if(y == 1){
                sumNum += codeArr[1] * 3;
            }else if(y == 2){
                sumNum += codeArr[2] * 9;
            }else if(y == 3){
                sumNum += codeArr[3] * 27;
            }else if(y == 4){
                sumNum += codeArr[4] * 19;
            }else if(y == 5){
                sumNum += codeArr[5] * 26;
            }else if(y == 6){
                sumNum += codeArr[6] * 16;
            }else if(y == 7){
                sumNum += codeArr[7] * 17;
            }else if(y == 8){
                sumNum += codeArr[8] * 20;
            }else if(y == 9){
                sumNum += codeArr[9] * 29;
            }else if(y == 10){
                sumNum += codeArr[10] * 25;
            }else if(y == 11){
                sumNum += codeArr[11] * 13;
            }else if(y == 12){
                sumNum += codeArr[12] * 8;
            }else if(y == 13){
                sumNum += codeArr[13] * 24;
            }else if(y == 14){
                sumNum += codeArr[14] * 10;
            }else if(y == 15){
                sumNum += codeArr[15] * 30;
            }else if(y == 16){
                sumNum += codeArr[16] * 28;
            }
        };
        //求余
        var remainderNum = sumNum % number;
        //最后一位数
        // var lastNum = number - remainderNum;
        //余数为0,则第十八位为0
        if(remainderNum == 0){
            var lastNum = 0;
        }else{
            //否则获取非0余数
            var lastNum = number - remainderNum;
        }
        //余数大于9则转换成第十八位的字母
        if(lastNum == '10'){
            lastNum = 'A'
        }else if(lastNum == '11'){
            lastNum = 'B'
        }else if(lastNum == '12'){
            lastNum = 'C'
        }else if(lastNum == '13'){
            lastNum = 'D'
        }else if(lastNum == '14'){
            lastNum = 'E'
        }else if(lastNum == '15'){
            lastNum = 'F'
        }else if(lastNum == '16'){
            lastNum = 'G'
        }else if(lastNum == '17'){
            lastNum = 'H'
        }else if(lastNum == '18'){
            lastNum = 'J'
        }else if(lastNum == '19'){
            lastNum = 'K'
        }else if(lastNum == '20'){
            lastNum = 'L'
        }else if(lastNum == '21'){
            lastNum = 'M'
        }else if(lastNum == '22'){
            lastNum = 'N'
        }else if(lastNum == '23'){
            lastNum = 'P'
        }else if(lastNum == '24'){
            lastNum = 'Q'
        }else if(lastNum == '25'){
            lastNum = 'R'
        }else if(lastNum == '26'){
            lastNum = 'T'
        }else if(lastNum == '27'){
            lastNum = 'U'
        }else if(lastNum == '28'){
            lastNum = 'W'
        }else if(lastNum == '29'){
            lastNum = 'X'
        }else if(lastNum == '30'){
            lastNum = 'Y'
        };
        //计算出来的值和最后一位不匹配
        if(lastNum != codeLastNum){
            return 3
        }else{
            //匹配成功
            return 4
        }
    }
}
/**
 * 
 * 身份证号校验
 * IDNumber:身份证号
 * 
 */
export function IDNumberMatch(IDNumber){
    let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if(!reg.test(IDNumber)){
        return 0
    }else{
        var codeArr = IDNumber.split('');
        //计算基数与权重相乘后相加的值
        var sumNum = 0;
        //除数
        var number = 11;
        //获取统一社会信用代码最后一位
        var codeLastNum = IDNumber.substr(IDNumber.length - 1,1);
        
        //计算出的最后一位数字
        var lastNum = 0;
        for(var x = 0,len = codeArr.length - 1;x < len;x++){
            if(x == 0){
                sumNum += codeArr[0] * 7;
            }else if(x == 1){
                sumNum += codeArr[1] * 9;
            }else if(x == 2){
                sumNum += codeArr[2] * 10;
            }else if(x == 3){
                sumNum += codeArr[3] * 5;
            }else if(x == 4){
                sumNum += codeArr[4] * 8;
            }else if(x == 5){
                sumNum += codeArr[5] * 4;
            }else if(x == 6){
                sumNum += codeArr[6] * 2;
            }else if(x == 7){
                sumNum += codeArr[7] * 1;
            }else if(x == 8){
                sumNum += codeArr[8] * 6;
            }else if(x == 9){
                sumNum += codeArr[9] * 3;
            }else if(x == 10){
                sumNum += codeArr[10] * 7;
            }else if(x == 11){
                sumNum += codeArr[11] * 9;
            }else if(x == 12){
                sumNum += codeArr[12] * 10;
            }else if(x == 13){
                sumNum += codeArr[13] * 5; 
            }else if(x == 14){
                sumNum += codeArr[14] * 8;
            }else if(x == 15){
                sumNum += codeArr[15] * 4;
            }else if(x == 16){
                sumNum += codeArr[16] * 2;
            }
        };
        // 获取余数
        var remainderNum = sumNum % number;
        if(remainderNum == 0){
            lastNum = 1;
        }else if(remainderNum == 1){
            lastNum = 0
        }else if(remainderNum == 2){
            lastNum = 'X'
        }else if(remainderNum == 3){
            lastNum = 9;
        }else if(remainderNum == 4){
            lastNum = 8;
        }else if(remainderNum == 5){
            lastNum = 7;
        }else if(remainderNum == 6){
            lastNum = 6;
        }else if(remainderNum == 7){
            lastNum = 5
        }else if(remainderNum == 8){
            lastNum = 4;
        }else if(remainderNum == 9){
            lastNum = 3;
        }else if(remainderNum == 10){
            lastNum = 2;
        };
        if(codeLastNum != lastNum){
            return 0;
        }else {
            return 1;
        }
    }
}
/**
 * 生成年月日时分秒字符串
 */
export function createDateStr(){
    let date = new Date();
    return String(date.getFullYear()) + String((date.getMonth() + 1)).padStart('2','0') + String(date.getDate()).padStart('2','0') + String(date.getHours()).padStart('2','0') + String(date.getMinutes()).padStart('2','0') + String(date.getSeconds()).padStart('2','0');
}
/**
 * 随机生成字符串
 * length:生成字符串长度，最长36位
 */
export function randomStr(length){
    let str = Math.random().toString(36).substr(2);
    if(str.length >= length){
        return str.substr(0,length);
    };
    str += randomStr(length-str.length);
    return str;
}

export function currentDate (){
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if(month >= 1 && month <= 9){
        month = '0' + month;
    };
    if(day >= 1 && day <= 9){
        day = '0' + day
    };
    return year + '-' + month + '-' + day
}

export function currentTime (){
    let str = "";
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hours = date.getHours();
    let minutes = date.getMinutes();
    let seconds = date.getSeconds();
    if(month >= 1 && month <= 9){
        month = '0' + month;
    };
    if(day >= 1 && day <= 9){
        day = '0' + day
    };
    if(hours >= 0 && hours <= 9){
        hours = '0' + hours
    };
    if(minutes >= 0 && minutes <= 9){
        minutes = '0' + minutes
    };
    if(seconds >= 0 && seconds <= 9){
        seconds = '0' + seconds
    };
    str = year + "-" + month + "-"+ day + " " + hours+ ":" + minutes +":"+ seconds;
	
	return str;
}
