調用控件代碼:
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 }
