您所在的位置首页>纯软件授权 > 微信小程序 >
微信小程序
微信小程序

中软高科·小程序身份证云解码插件集成方法

发布日期:2024-01-03 12:32:44 来源:XX 浏览次数

中软高科·小程序身份证云解码插件

1、引入插件

在小程序的app.json中,加入插件。

"plugins": {
    "readcard-plugin": {
      "version": "2.0.0",
      "provider": "wxa2583ebacdb87a6a"
    }
}

2、基础库

调试基础库  2.18.1及以上

微信版本:
iOS      暂不支持 
Android  8.0.6及以上版本

3、使用插件

在需要使用身份证读卡的页面中,使用插件。

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);

    // 设置APPID (开始读卡之前,必须设置)
    plugin.setAppId("appid联系我司商务获取");

    // 设置读卡SDK参数
    that.readSetting();

    // 初始化NFC读卡。
    // 初始化之前,请先自行判断设备是否支持NFC、系统NFC开关是否打开
    that.initRead();
  },

  // 设置读卡SDK参数
  readSetting(){
    // 日志保存目录
    // 默认在 Android/data/com.tencent.mm/MicroMsg/wxanewfiles目录下   搜索 zrgk_mini_log
    // 不需要日志时,可以不设置
    plugin.setShowLog(wx.getFileSystemManager(), wx.env.USER_DATA_PATH);

    // 读卡参数设置,根据需要自行修改参数值,参数名称及类型不可修改。
    var _Setting = {
      decodeImageType: "dn1", // dn0: 无照片   dn1: 平台解码照片
      saveLog: false, // 是否保存日志文件。设置true时,必须调用以上的 plugin.setShowLog(); 设置日志保存的目录
      openLocalCache: true, // 是否开启本地缓存
      // 解码服务器配置。
      // 默认第1个为主服务器,优先使用。
      // 其余皆为备用服务器,主服务器异常时自动切换启用
      ipPortArray: [{ 
        address: 'yfs3.sfzydq.com',
        port: 9999,
        canUse: true
      }, {
        address: "yjm2.sfzydq.com",
        port: 9999,
        canUse: true
      }]
    };
    // 将参数设置给插件
    plugin.readSetting(_Setting);
  },

  // 初始化 读卡示例代码
  initRead() {
    if (initSuccess == true) {
      wx.showToast({
        title: '已经初始化过了',
        icon: 'none'
      });
      return;
    }
    // 初始化并开始读卡
    plugin.startReadCard(function (code, msg, value, cardType) {
      that.setData({
        msg: "code:" + code + "\n" + "msg:" + msg,
      });
      switch (code) {
        case StatusCode.ININ_OK.code: // 初始化成功
          initSuccess = true;
          wx.showToast({
            title: '初始化成功',
            icon: 'success'
          });
          break;
        case StatusCode.ININ_FAILE.code: // 初始化失败
          initSuccess = false;
          if (msg.indexOf("13000") != -1) {
            wx.showModal({
              title: '温馨提示',
              content: '设备不支持NFC',
              complete: (res) => {
              }
            });
          } else if (msg.indexOf("13001") != -1) {
            wx.showModal({
              title: '温馨提示',
              content: '系统NFC开关未打开',
              complete: (res) => {
              }
            });
          } else {
            wx.showToast({
              title: msg,
              icon: 'error'
            })
          }
          break;
        case StatusCode.READ_NFC_TAG.code: // 识别到NFC标签
          wx.showLoading({
            title: '请勿移动卡片',
          });
          break;
        case StatusCode.READCARD_SUCCESS.code: // 解码成功
          wx.hideLoading();
          wx.showToast({
            title: '解码成功',
            icon: 'success'
          });
          switch (cardType) {
            case StatusCode.CARD_IC.code: // IC卡
              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);
              // 当设置平台解码身份证照片时,
              // idCardInfo.image字段,为base64编码的照片字符串,可直接用于显示
              // idCardInfo.type=1080 中国居民身份证
              // idCardInfo.type=1081 外国永久居住证
              // idCardInfo.type=1082 港澳台居住证
              break;
          }
          break;
        case StatusCode.READCARD_FAILE.code: // 解码失败
          wx.hideLoading();
          wx.showToast({
            title: '解码失败' + StatusCode.READCARD_FAILE.code,
            icon: 'error'
          });
          break;
      }
    });
  },

  /**
   * 生命周期
   */
  onShow() {
    console.log("onShow", "界面显示");
  },

  onHide() {
    console.log("onHide", "界面隐藏");
  },

  onUnload() {
    console.log("onUnload", "界面销毁,需要停止读卡");
    plugin.stopReadCard();
  }
})

4、配置TCP合法域名

必须将参数配置时,配置的 ipPortArray 参数中的所有服务器地址,加入到合法的TCP域名,
否则无法访问到服务器。
如配置:

tcp://yjm2.sfzydq.com

tcp://yfs3.sfzydq.com