调用控件代码:
1 <object id="CertCtl" type="application/cert-reader" width="0" height="0"> 2 <p style="color:#FF0000;">未安装身份证阅读器驱动,或控件未启用。</p> 3 </object>
js代码:
读取身份证
1 //读取身份证号 2 function readCert() { 3 clearInfo(); //清除个人信息 4 connect(); //连接 5 var CertCtl = document.getElementById("CertCtl"); 6 7 try { 8 9 var startDt = new Date(); 10 var result = CertCtl.readCert(); 11 var endDt = new Date(); 12 13 document.getElementById("timeElapsed").value = (endDt.getTime() - startDt.getTime()) + "毫秒"; 14 15 var resultObj = toJson(result); 16 17 if (resultObj.resultFlag == 0) { 18 19 //document.getElementById("IDCard").value = resultObj.resultContent.certNumber; 20 21 GetInfo(resultObj.resultContent); 22 23 } else { 24 //$.messager.alert('提示', resultObj.errorMsg, 'info'); 25 } 26 } catch (e) { 27 $.messager.alert('发生错误', e, 'error'); 28 } 29 }
身份信息赋值
1 function GetInfo(content) { 2 $("#IDCard").val(content.certNumber); 3 $("#PersonName").val(content.partyName); 4 $("#IDCardAddr").val(content.certAddress); 5 var Sex = content.gender; 6 if(Sex=="男") $("input[name=Sex][value=true]").prop("checked", true); 7 else $("input[name=Sex][value=false]").prop("checked", true); 8 9 var ageStr = content.bornDay; //出生日期 10 var bornDt = ageStr.substring(0, 4); 11 var Age = new Date().getFullYear() - bornDt; //年龄 12 $("#Age").val(Age); 13 }
清空输入框信息
1 function clearInfo() { 2 $("#IDCard").val(""); 3 $("#PersonName").val(""); 4 $("#IDCardAddr").val(""); 5 $("input[name=Sex][value=true]").prop("checked", true); 6 $("#Age").val(""); 7 }
连接
1 //连接 2 function connect() { 3 4 var CertCtl = document.getElementById("CertCtl"); 5 try { 6 var result = CertCtl.connect(); 7 result = $.parseJSON(result); 8 //$.messager.alert('提示', result.errorMsg == "" ? "已连接" : result.errorMsg, 'info'); 9 } catch (e) { 10 11 if (IES != -1) { 12 $.messager.alert('发生错误', e, 'error'); 13 } 14 return; 15 } 16 }
断开
1 // 断开 2 function disconnect() { 3 4 clearInfo(); 5 6 var CertCtl = document.getElementById("CertCtl"); 7 try { 8 var result = CertCtl.disconnect(); 9 result = $.parseJSON(result); 10 // $('#SCardtype').html(result.resultFlag == 1 ? "已断开" : result.errorMsg); 11 $.messager.alert('提示', result.resultFlag == 1 ? "已断开" : result.errorMsg, 'info'); 12 13 } catch (e) { 14 if (IES != "-1") { 15 $.messager.alert('发生错误', e, 'error'); 16 } 17 } 18 }
1 //判断IE 2 function IEVersion() { 3 var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 4 var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器 5 var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器 6 var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1; 7 if (isIE) { 8 var reIE = new RegExp("MSIE (\\d+\\.\\d+);"); 9 reIE.test(userAgent); 10 var fIEVersion = parseFloat(RegExp["$1"]); 11 if (fIEVersion == 7) { 12 return 7; 13 } else if (fIEVersion == 8) { 14 return 8; 15 } else if (fIEVersion == 9) { 16 return 9; 17 } else if (fIEVersion == 10) { 18 return 10; 19 } else { 20 return 6;//IE版本<=7 21 } 22 } else if (isEdge) { 23 return 'edge';//edge 24 } else if (isIE11) { 25 return 11; //IE11 26 } else { 27 return -1;//不是ie浏览器 28 } 29 } 30 31 //数据 32 function toJson(str) { 33 return eval('(' + str + ')'); 34 }
最后展示一下返回的数据格式:
1 { 2 "errorMsg" : "", 3 "resultContent" : { 4 "bornDay" : "20201116", 5 "certAddress" : "河南省郑州市", 6 "certNumber" : "身份证号", 7 "certOrg" : "XXX公安局", 8 "certType" : "居民身份证", 9 "chineseName" : "", 10 "effDate" : "20150919", 11 "expDate" : "20250919", 12 "gender" : "男", 13 "identityPic" : "照片地址", 14 "nation" : "汉", 15 "partyName" : "姓名", 16 "passportNo" : "", 17 "signNum" : "" 18 }, 19 "resultFlag" : 0 20 }