近期公司使用 精伦电子 iDR210、中国普天 CP IDMR02/TG、新中新 DKQ-A16D读卡器,对身份证读取。
于是便写了这个JS对身份证读卡器进行操作。
留意,在程序测试时,新中新的读卡器,貌似出问题了。奇怪的问题是,使用普天的驱动可以正常使用新中新的读卡器,而新中新的程序却不能识别自己的读卡器。
另外,普天读卡器,有时候拿不到身份证图片的 BASE64信息。建议禁用其他无关身份证读卡器加载项关闭IE重新打开,再试试。
另,已修复 IE8 console 报错问题。使用时,可以把 cardDebug 开关关闭。
需要注意的是,修复后的 console.log 并不是支持所有IE浏览器(IE8是显示了,IE11却不显示了),建议使用 Faux Console 来处理此问题。
在 HTML文件 HEAD部分加入:
idCard.js 源文件:
/*
执行流程
1、获取读卡器类型及报错信息
2、读取证件信息
3、post 数据
by un
*/
var cardDebug = false; // 调试开关
// 读取到的身份证信息
var persInfo = {txt_aac003 : '',txt_aac004 : '', txt_aac004n : '', txt_aac005 : '', txt_aac005n : '', txt_aac002 : '', txt_bab305 : '', baseimg : ''};
var jingLunInfo; // 精伦 读取到的身份证信息
var puTianInfo; // 普天 读取到的身份证信息
var xinZhongXinInfo; // 新中新 读取到的身份证信息
// 对 persInfo 置空
function initPersInfo() {
persInfo.txt_aac003 = '';// 姓名
persInfo.txt_aac004 = ''; // 性别编号
persInfo.txt_aac004n= ''; // 性别
persInfo.txt_aac005 = ''; // 民族编号
persInfo.txt_aac005n= ''; // 民族
persInfo.txt_aac002 = ''; // 身份证号码
persInfo.txt_bab305 = ''; // 身份证个人地址
persInfo.baseimg = ''; // 身份证图片 Base64编码
}
// 测试所有支持的读卡器
function testCard() {
initPersInfo();
var data = '';
if (getCookie('cardType') == '')
{
data = testJingLunCard(); // 精伦 读卡器测试
if (cardDebug)
{
console.log("精伦 读卡器测试");
console.log("type " + data.type);
console.log("status " + (data.status ? 'true' : 'false'));
console.log("info " + data.info);
}
if (data.status == false)
{
data = testPuTianCard(); // 普天 读卡器测试
if (cardDebug)
{
console.log("普天 读卡器测试");
console.log("type " + data.type);
console.log("status " + (data.status ? 'true' : 'false'));
console.log("info " + data.info);
}
if (data.status == false)
{
// 新中新 读卡器有问题 忽略处理 (BT的是,使用演示程序 普天 可以顺利的使用新中新的读卡器)
data = testXinZhongXinCard(); // 新中新 读卡器测试
if (cardDebug)
{
console.log("新中新 读卡器测试");
console.log("type " + data.type);
console.log("status " + (data.status ? 'true' : 'false'));
console.log("info " + data.info);
}
if (data.status == false)
{
alert(data.info);
return false;
}
}
}
} else {
if (cardDebug)
{
console.log("使用 Cookie 来操作读卡器 "+getCookie('cardType'));
}
switch (getCookie('cardType'))
{
case 'jinglun': // 精伦
data = testJingLunCard(); // 精伦 读卡器测试
break;
case 'putian': // 普天
data = testPuTianCard(); // 普天 读卡器测试
break;
case 'xinzhongxin':// 新中新
data = testXinZhongXinCard(); // 新中新 读卡器测试
break;
}
if (data.status == false)
{
alert(data.info);
return false;
}
}
setCookie('cardType',data.type, null);
getCardVal(data);
postInfo();
}
function getCardVal(data) {
// 数据置空
switch (data.type)
{
case 'jinglun': // 精伦
persInfo.txt_aac003 = JingLunCard.GetName();// 姓名
persInfo.txt_aac004 = JingLunCard.GetSexN(); // 性别编号
persInfo.txt_aac004n= JingLunCard.GetSex(); // 性别
persInfo.txt_aac005 = JingLunCard.GetFolkN(); // 民族编号
persInfo.txt_aac005n= JingLunCard.GetFolk(); // 民族
persInfo.txt_aac002 = JingLunCard.GetCode(); // 身份证号码
persInfo.txt_bab305 = JingLunCard.GetAddress(); // 身份证个人地址
persInfo.baseimg = JingLunCard.GetJPGPhotobuf(); // 身份证图片 Base64编码
break;
case 'putian':// 普天
try
{
persInfo.txt_aac003 = PuTianCard.NameL();// 姓名
}catch(e)
{
data.status = false;
data.info = "身份证信息读取失败 错误详情:"+e.name+":"+e.message;
return data;
}
persInfo.txt_aac004 = PuTianCard.Sex(); // 性别编号
persInfo.txt_aac004n= PuTianCard.sexL(); // 性别
persInfo.txt_aac005 = PuTianCard.Nation(); // 民族编号
persInfo.txt_aac005n= PuTianCard.NationL(); // 民族
persInfo.txt_aac002 = PuTianCard.CardNo(); // 身份证号码
persInfo.txt_bab305 = PuTianCard.Address(); // 身份证个人地址
persInfo.baseimg = PuTianCard.GetImage(); // 身份证图片 Base64编码
break;
case 'xinzhongxin':// 新中新
persInfo.txt_aac003 = XinZhongXinCard.NameA();// 姓名
persInfo.txt_aac004 = XinZhongXinCard.Sex(); // 性别编号
persInfo.txt_aac004n= persInfo.txt_aac004 == '1' ? '男' : '女'; // 性别
persInfo.txt_aac005n= XinZhongXinCard.Nation(); // 民族编号
persInfo.txt_aac005 = XinZhongXinCard.Nation(); // 民族
persInfo.txt_aac002 = XinZhongXinCard.CardNo(); // 身份证号码
persInfo.txt_bab305 = XinZhongXinCard.Address(); // 身份证个人地址
persInfo.baseimg = XinZhongXinCard.Base64Photo(); // 身份证图片 Base64编码
break; // 新中新读卡器有问题
default:
if (cardDebug)
{
console.log("当前读卡器不被支持或不存在身份证读卡器!");
}
return false; // 当前读卡器不被支持或不存在身份证读卡器
break;
}
if (cardDebug)
{
console.log("card type " + data.type);
console.log("status " + (data.status ? 'true' : 'false'));
console.log("info " + data.info);
console.log("姓名 " + persInfo.txt_aac003);
console.log("性别编号 " + persInfo.txt_aac004);
console.log("性别 " + persInfo.txt_aac004n);
console.log("民族编号 " + persInfo.txt_aac005);
console.log("民族 " + persInfo.txt_aac005n);
console.log("身份证号码 " + persInfo.txt_aac002);
console.log("身份证个人地址 " + persInfo.txt_bab305);
console.log("身份证图片 Base64编码 " + persInfo.baseimg);
}
}
// POST 数据
function postInfo() {
$.post("index.php?m=index&a=top",{
"txt_aac003":persInfo.txt_aac003,
"txt_aac002":persInfo.txt_aac002,
"txt_aac004":persInfo.txt_aac004,
"txt_aac005":persInfo.txt_aac005,
"txt_bab305":persInfo.txt_bab305,
"txt_aac004n":persInfo.txt_aac004n+'性',
"txt_aac005n":persInfo.txt_aac005n+'族',
"baseimg":persInfo.baseimg
},function(data){
window.top.topFrame.location.reload();
eval("var data='"+data+"'");
if(data.state == -1){
window.top.mainFrame.I2.location="index.php?m=personal&a=add&s=card";
window.top.mainFrame.I2.reload();
}
});
}
// 精伦 读卡器测试
function testJingLunCard() {
// 执行结果与信息提示和读卡器类型
var result = {status : true, info : '', type : 'jinglun'};
// 精伦 读卡器
try
{
jingLunInfo = JingLunCard.ReadCard("1001","d:\\test\\test.bmp");
}catch(e)
{
result.status = false;
result.info = "您未安装身份证读卡驱动!错误详情:"+e.name+":"+e.message;
return result;
}
if (result.status == true && jingLunInfo == 1)
{
result.status = true;
result.info = "操作成功!";
// 表明读取成功
} else {
if (cardDebug)
{
console.log("精伦 返回值 " + jingLunInfo);
}
switch (jingLunInfo)
{
case 1:// 读取成功 使用 jingLunInfo 获取身份证信息
result.status = true;
result.info = "操作成功!";
break;
case -1:
result.status = false;
result.info = "端口初始化失败!";
break;
case -2:
result.status = false;
result.info = "请重新将卡片放到读卡器上!";
break;
case -3:
result.status = false;
result.info = "读取数据失败!";
break;
case -4:
result.status = false;
result.info = "生成照片文件失败,请检查设定路径和磁盘空间!";
break;
}
}
return result;
}
var isInit = false;
function testPuTianCard() {
// 执行结果与信息提示和读卡器类型
var result = {status : true, info : '', type : 'putian'};
//设置端口号,1表示串口1,2表示串口2,依此类推;1001表示USB。0表示自动选择
if(false == isInit)
{
try
{
PuTianCard.setPortNum(0);
}catch(e)
{
result.status = false;
result.info = "您未安装身份证读卡驱动!错误详情:"+e.name+":"+e.message;
return result;
}
isInit=true;
}
//使用重复读卡功能
PuTianCard.Flag=0;
// 读卡
try
{
puTianInfo = PuTianCard.ReadCard();
}catch(e)
{
result.status = false;
result.info = "您未安装身份证读卡驱动!错误详情:"+e.name+":"+e.message;
return result;
}
if (cardDebug)
{
console.log("普天 返回值 " + puTianInfo);
}
switch (puTianInfo)
{
case 0x90: // 读取成功 使用 puTianInfo 获取身份证信息
result.status = true;
result.info = "操作成功!";
break;
case 0x01:
result.status = false;
result.info = "端口初始化失败!";
break;
case 0x02: // 超时
result.status = false;
result.info = "读取数据失败!";
break;
case 0x41:// 读卡失败
result.status = false;
result.info = "请重新将卡片放到读卡器上!";
break;
default:
result.status = false;
result.info = "未知错误!";
}
return result;
}
function testXinZhongXinCard() {
// 执行结果与信息提示和读卡器类型
var result = {status : true, info : '', type : 'xinzhongxin'};
try
{
var xinZhongXinStatus = XinZhongXinCard.FindReader();
}catch(e)
{
result.status = false;
result.info = "您未安装身份证读卡驱动!错误详情:"+e.name+":"+e.message;
return result;
}
if (cardDebug)
{
console.log("新中新 获取读卡器 " + tmpStatus);
}
if (xinZhongXinStatus > 0)
{
result.status = true;
result.info = "操作成功!";
XinZhongXinCard.SetPhotoPath(2, 'C:\\Windows\\temp\\');
var tmpStatus = XinZhongXinCard.ReadCardMsg();
if (cardDebug)
{
console.log("新中新 返回值 " + tmpStatus);
}
if (tmpStatus == 0)
{
result.status = true;
result.info = "操作成功!";
} else {
result.status = false;
result.info = "请重新将卡片放到读卡器上!";
}
} else {
result.status = false;
result.info = "读卡器初始化失败!";
}
return result;
}
/*
* 读会员卡
*/
function ICcard(){
var result;
//注意:参数为对应的设备端口,iDR210为8159,iDR200 USB型为1001,iDR200串口型为1至16
try{
result=JingLunCard.ReadICCard("8159");
}catch(e)
{
alert("您未安装读卡驱动!错误详情:"+e.name+":"+e.message);
return;
}
if (result==1){
var txt_cardmark = JingLunCard.GetICCardSN();
$.post("index.php?m=index&a=top",{ "txt_cardmark":txt_cardmark },
function(data){
var data = eval("data="+data);
if(data.state==0){
alert(data.msg);
}else{
window.top.topFrame.location.reload();
}
})
}else{
if (result==-1)
alert("端口初始化失败!");
if (result==-2)
alert("读IC卡失败");
}
}
function CardById(id)
{
return document.getElementById(id);
}
// 设置 Cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
// 读取 Cookie 失败 返回 空
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}