Closed sailei1 closed 5 years ago
export function calcIDCardCode(isShenFenZheng,idCardBody){ if (idCardBody.length != 17) { return ''; } // 加权因子 var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 校验码对应值 var code = isShenFenZheng ? ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] : ['1', '0', '5', '9', '8', '7', '6', '5', '4', '3', '2']; var checksum = 0; for (var i = 0; i < idCardBody.length; i++) { checksum += idCardBody.substr(i, 1) * factor[i]; } return code[checksum % 11]; } /** * 18位身份证校验码有效性检查 * @param: isShenFenZheng true/ false (true 身份证); idCard: 身份证码 * @param: idCard Number */ export function check18IDCard(isShenFenZheng,idCard) { if(isShenFenZheng && idCard.length != 18){ return false; }else if(!isShenFenZheng && idCard.length != 14){ return false; } var idCardBody = idCard.substr(0, 17); var idCardCode = idCard.substr(17, 1).toUpperCase(); if (calcIDCardCode(isShenFenZheng,idCardBody) != idCardCode) { return false; } return true; } /** * 计算身份证性别 */ export function calculateSex (ID){ let sexno, sex, tempid if (ID.length == 18) { sexno = ID.substring(16, 17) } else if (ID.length == 15) { sexno = ID.substring(14, 15) } else { return false } tempid = sexno % 2; if (tempid == 0) { sex = '0'; }else{ sex = '1'; } return sex } /** * 计算身份证生日 */ export function calculateBirthDay(ID){ return ID.substring(6, 14) } /** * 计算周岁(按当天算) */ export function calculateTodayAge (birth) { let clientDate = new Date() let month = clientDate.getMonth() + 1 let day = clientDate.getDate() let age = clientDate.getFullYear() - birth.substring(0, 4) - 1 if (birth.substring(4, 6) < month || birth.substring(4, 6) == month && birth.substring(6, 8) <= day) { age++ } return age } /** * 计算周岁(按明天算) */ export function calculateTomorrowAge (birth) { let date = new Date(parseInt(birth.substring(0, 4)), parseInt(birth.substring(4, 6)) - 1, birth.substring(6, 8)) let y, m, d let clientDate, month, day, age date.setDate(date.getDate() - 2) y = date.getFullYear() m = date.getMonth() + 1 d = date.getDate() clientDate = new Date(); month = clientDate.getMonth() + 1 day = clientDate.getDate() age = clientDate.getFullYear() - y - 1 if (m < month || m == month && (d + 1) <= day) { age++ } return age } /** * 计算该身份证 在保险范围之内(按明天算) * YMD 生日 年月日 * effective 0 当天生效 1 明天生效 */ export function checkUseCanBuy(YMD,effective=0,minDay=30){ YMD = YMD.replace(/[^0-9]/ig, '') let date = new Date() let y, m, d let birthDate, clientDate, day; let check=false; date.setDate(date.getDate()) if(effective){ date.setDate(date.getDate() + 1) } date.setMonth(date.getMonth() + 1) y = date.getFullYear() m = date.getMonth() d = date.getDate() // 转换为12-18-2006格式 birthDate = new Date(YMD.substring(0, 4), YMD.substring(4, 6), YMD.substring(6, 8)) clientDate = new Date(y, m, d) // 把相差的毫秒数转换为天数 day = parseInt(Math.abs(birthDate - clientDate) / 1000 / 60 / 60 / 24) if(day>minDay){ check=true; } return check; }