Commit 1386ff55 by 王昆

gsb

parent 83805b92
const system=require("../system"); const system = require("../system");
const uuidv4 = require('uuid/v4'); const uuidv4 = require('uuid/v4');
class IdcardClient { class IdcardClient {
constructor() { constructor() {
// 加权因子 // 加权因子
this.weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]; this.weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
// 校验码 // 校验码
this.check_code = ['1', '0', 'X' , '9', '8', '7', '6', '5', '4', '3', '2']; this.check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
} }
async checkIDCard(idcard) { async checkIDCard(idcard) {
if(!idcard) { if (!idcard) {
return false; return false;
} }
var code = idcard.toString().toUpperCase(); var code = idcard.toString().toUpperCase();
var last = idcard[17];//最后一个 var last = idcard[17];//最后一个
var seventeen = code.substring(0,17); var seventeen = code.substring(0, 17);
// ISO 7064:1983.MOD 11-2 // ISO 7064:1983.MOD 11-2
// 判断最后一位校验码是否正确 // 判断最后一位校验码是否正确
var arr = seventeen.split(""); var arr = seventeen.split("");
var len = arr.length; var len = arr.length;
var num = 0; var num = 0;
for(var i = 0; i < len; i++){ for (var i = 0; i < len; i++) {
num = num + arr[i] * this.weight_factor[i]; num = num + arr[i] * this.weight_factor[i];
} }
// 获取余数 // 获取余数
var resisue = num%11; var resisue = num % 11;
var last_no = this.check_code[resisue]; var last_no = this.check_code[resisue];
// 格式的正则 // 格式的正则
...@@ -51,13 +51,44 @@ class IdcardClient { ...@@ -51,13 +51,44 @@ class IdcardClient {
return last === last_no && format ? true : false; return last === last_no && format ? true : false;
} }
/**
*
* @param card
* @returns {
* birth 生日
* sex 性别
*
* }
*/
async cardInfo(card) {
let info = {};
//获取出生日期
info.birth = card.substring(6, 10) + "-" + card.substring(10, 12) + "-" + card.substring(12, 14);
//获取性别
if (parseInt(card.substr(16, 1)) % 2 == 1) {
info.sex = "male";
} else {
info.sex = "female";
}
//获取年龄
let myDate = new Date();
let month = myDate.getMonth() + 1;
let day = myDate.getDate();
let age = Number(myDate.getFullYear()) - Number(card.substring(6, 10)) - 1;
if (card.substring(10, 12) < month || card.substring(10, 12) == month && card.substring(12, 14) <= day) {
age++;
}
info.age = Number(age);
return info;
}
} }
module.exports = IdcardClient; module.exports = IdcardClient;
// 函数参数必须是字符串,因为二代身份证号码是十八位,而在javascript中,十八位的数值会超出计算范围,造成不精确的结果,导致最后两位和计算的值不一致,从而该函数出现错误。 // 函数参数必须是字符串,因为二代身份证号码是十八位,而在javascript中,十八位的数值会超出计算范围,造成不精确的结果,导致最后两位和计算的值不一致,从而该函数出现错误。
// 详情查看javascript的数值范围 // 详情查看javascript的数值范围
function checkIDCard(idcode){ function checkIDCard(idcode) {
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment