1、引入插件
在小程序的app.json中,加入插件。
"plugins": {
"readcard-plugin": {
"version": "*",
"provider": "2021004143649912"
}
}
2、使用插件
在需要使用身份证读卡的页面中,使用插件。
const plugin = requirePlugin('readcard-plugin');
var StatusCode = null;
var that = this;
var initSuccess = false;
Page({
data: {
idCardInfo: null,
},
onLoad() {
that = this;
StatusCode = plugin.StatusCodeEnum();
console.log("对照状态码:", StatusCode);
plugin.setAppId("appid联系我司商务获取");
that.readSetting();
that.initRead();
},
readSetting(){
plugin.setShowLog(my.getFileSystemManager(), my.env.USER_DATA_PATH);
var _Setting = {
decodeImageType: "dn1",
readCardType: 7,
saveLog: false,
openLocalCache: true,
ipPortArray: [{
address: 'ws.mtjsoft.cn',
port: 443,
canUse: true
}]
};
plugin.readSetting(_Setting);
},
initRead() {
if (initSuccess == true) {
my.showToast({
content: '已经初始化过了',
type: 'none'
});
return;
}
plugin.startReadCard(function (code, msg, value, cardType) {
let code_msg = "code:" + code + "\n" + "msg:" + msg;
that.setData({
msg: code_msg,
});
switch (code) {
case StatusCode.ININ_ING.code:
my.showLoading({
content: '初始化中...'
});
break;
case StatusCode.ININ_OK.code:
my.hideLoading();
initSuccess = true;
my.showToast({
content: '初始化成功',
type: 'success'
})
break;
case StatusCode.ININ_FAILE.code:
my.hideLoading();
initSuccess = false;
if (msg.indexOf("61000") != -1) {
my.showModal({
title: '温馨提示',
content: '设备不支持NFC',
complete: (res) => {}
});
} else if (msg.indexOf("61001") != -1) {
my.showModal({
title: '温馨提示',
content: '系统NFC开关未打开',
complete: (res) => {}
});
} else {
my.showToast({
content: msg,
type: 'fail'
})
}
break;
case StatusCode.FIND_CARD_START.code:
my.showToast({
content: '开始寻卡',
type: 'success'
})
break;
case StatusCode.FIND_CARD_SUCCESS.code:
my.showToast({
content: '寻卡成功',
type: 'success'
})
break;
case StatusCode.READCARD_START.code:
my.showLoading({
content: '请勿移动卡片',
})
break;
case StatusCode.READCARD_SUCCESS.code:
my.hideLoading();
my.showToast({
content: '解码成功',
type: 'success'
})
switch (cardType) {
case StatusCode.CARD_IC.code:
console.log("IC卡:" , value);
break;
case StatusCode.CARD_LCT_STUDENT.code:
console.log("绿城通学生卡:" , value);
break;
case StatusCode.CARD_LCT_NORMAL.code:
console.log("绿城通普通卡:" , value);
break;
case StatusCode.CARD_LCT_OLD.code:
console.log("绿城通老年卡:" , value);
break;
case StatusCode.CARD_IDCARD.code:
var idCardInfo = JSON.parse(value);
console.log("身份证信息:" , idCardInfo);
break;
}
break;
case StatusCode.READCARD_FAILE.code:
my.hideLoading();
my.showToast({
content: '解码失败' + msg,
type: 'fail'
})
break;
}
});
},
onShow() {
console.log("onShow", "界面显示");
},
onHide() {
console.log("onHide", "界面隐藏");
},
onUnload() {
console.log("onUnload", "界面销毁,需要停止读卡");
plugin.stopReadCard();
}
})
3、附录
身份证详细信息 IdCardData
| 字段 | 类型 | 注释 |
|---|
| type | int | 1080:中国居民身份证 |
| address | string | 家庭住址 |
| birthday | string | 出生日期 |
| dn | string | dn码 |
| endDate | string | 身份证结束时间 |
| startDate | string | 身份证生效时间 |
| id | string | 身份证号码 |
| image | String | base64照片 |
| issue | string | 签发机关 |
| name | string | 姓名 |
| nation | string | 民族 |
| sex | string | 性别 |
| uuid | string | uuid |
外国人永久居留身份证 IdCardData
| 字段 | 类型 | 注释 |
|---|
| type | int | 1081:新版外国人永久居住证 1083:旧版外国人永久居住证 |
| birthday | string | 出生日期 |
| dn | string | dn码 |
| endDate | string | 身份证结束时间 |
| startDate | string | 身份证生效时间 |
| id | string | 身份证号码 |
| issue | string | 签发机关 |
| sex | string | 性别 |
| uuid | string | uuid |
| image | String | base64照片 |
| nameChinese | string | 中文姓名 |
| nameEnglish | string | 英文姓名 |
| nameEnglishAdd | string | 英文姓名备用 |
| nationlity | string | 国籍 |
| cardVersion | string | 卡版本号 |
| cardType | string | 卡类型 |
| historicalNumber | string | 既往版本证件号码关联项 |
| renewalNumber | string | 换证次数 |
港澳台居民居住证详细信息 IdCardData
| 字段 | 类型 | 注释 |
|---|
| type | int | 1082:港澳台居住证 |
| address | string | 家庭住址 |
| birthday | string | 出生日期 |
| dn | string | dn码 |
| endDate | string | 身份证结束时间 |
| startDate | string | 身份证生效时间 |
| id | string | 身份证号码 |
| image | String | base64照片 |
| issue | string | 签发机关 |
| name | string | 姓名 |
| nation | string | 民族 |
| sex | string | 性别 |
| uuid | string | uuid |
| passCheckId | string | 通行证号码 |
| issuesNumber | string | 签发次数 |