詳解微信小程序官方人臉核身認證

小程序收集瞭下用戶個人信息上傳被打回來說:

你好,小程序頁面功能涉及:采集用戶生物特征(人臉照片或視頻)及其他敏感信息,用於身份認識或識別,

為保障用戶敏感隱私身份信息,平臺暫不支持此功能。請去除相關功能後重新提交。

然後就去找度娘搜瞭下需要申請

wx.startFacialRecognitionVerify({})

https://api.weixin.qq.com/cgi-bin/token?appid=appid&secret=secret&grant_type=client_credential

https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=access_token

https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=access_token

首先要給申請下來的接口發送倆個參數:名字、身份證號碼

photo.js

data: {
    openid: '',
    custName: '姓名',
    custIdCard: '身份證號碼',
    verifyImg: ''
  },
  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    this.setData({
      custName: options.custName,
      custIdCard: options.custIdCard
    });
    var _this = this;
    wx.checkIsSupportFacialRecognition({
      checkAliveType: 2,
      success: function (res) {
        if (res.errCode === 0 || res.errMsg === "checkIsSupportFacialRecognition:ok") {
          //調用人臉識別
          _this.startface(_this.data.custName.replace(/(^\s*)|(\s*)$/g, ""), _this.data.custIdCard); //身份證名稱,身份證號碼
          return;
        }
        wx.showToast('微信版本過低,暫時無法使用此功能,請升級微信最新版本')
      },
      fail: function(res){
        wx.showToast('微信版本過低,暫時無法使用此功能,請升級微信最新版本')
      }

    })
  },
  startface(name, idcard) {
    console.log('我進來瞭!!!');
    var _this = this;
    wx.startFacialRecognitionVerify({
      name: _this.data.custName, //身份證名稱
      idCardNumber: _this.data.custIdCard, //身份證號碼
      success: function (res) {
        var verifyResult = res.verifyResult; //認證結果
        //調用接口


        wx.request({
          url: 'https://api.weixin.qq.com/cgi-bin/token?appid=wx2cafec51ec4c2153&secret=8d3e68a5a2c702673340d72d1c7db4cc&grant_type=client_credential',
          data: {

          },
          method: 'POST',
          header: {
            'content-type': 'application/json;charset=utf-8'
          },
          success: function (res) {
            console.log(res.data);
            console.log(res.data.access_token)
            var token = res.data.access_token;
            wx.request({
              url: 'https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=' + res.data.access_token,
              data: {
                verify_result: verifyResult
              },
              method: 'POST',
              header: {
                'content-type': 'application/json;charset=utf-8'
              },
              success: function (res) {
                console.log(res.data)
                console.log('我終於成功瞭。。。。')
                wx.request({
                  url: 'https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=' + token,
                  data: {
                    verify_result: verifyResult
                  },
                  method: 'POST',
                  responseType: 'arraybuffer',
                  header: {
                    'content-type': 'application/json;charset=utf-8',
                  },
                  success: (res) => {
                    // console.log('data:image/png;base64,'+wx.arrayBufferToBases64(res))

                    console.log(res.data);
                    var base64 = wx.arrayBufferToBase64(res.data);
                    _this.setData({ verifyImg:'data:image/png;base64,'+ base64})
                    wx.navigateTo({
                      url: '../msg/msg?msg=恭喜您信息核驗成功&verifyImg=' + _this.data.verifyImg
                    });
                  },
                  fail: function (res) {
                    console.log('失敗', res.data)
                  }
                })

              },
              fail: function (res) {

              }
            })
          },
          fail: function (res) {

          }
        })



        console.log(verifyResult)
        // wx.navigateTo({
        //   url: '../msg/msg?msg=人臉核身認證成功'
        // });
      },
      checkAliveType: 2, //屏幕閃爍(人臉核驗的交互方式,默認0,讀數字)
      fail: err => {
        wx.showToast('請保持光線充足,面部正對手機,且無遮擋')
        wx.navigateTo({
          url: '../msg/msg?msg=請保持光線充足,面部正對手機,且無遮擋,請退出重新操作'
        });
      }
    })
  }

主要坑的是這樣下來得申請好幾次接口到最後業務還需要unionid所以還得去微信開放平臺申請認證

然後想要拉取核身結果的圖片的時候就需要黨上面的https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=access_token

返回的數據需要轉成base64碼然後顯示在image標簽上我是直接傳給後臺的

下面上我msg的js代碼

msg.js

const app = getApp();
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    msg:'',
    verifyImg:'',
    url:app.globalData.PostData
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp/1000
    console.log(options)
    var that = this;
    that.setData({
      msg:options.msg,
      verifyImg:options.verifyImg
    });
    console.log(that.data.url)
    console.log(that.data.verifyImg)
    
      wx.request({
        url: that.data.url+'fileUpload!upBase64.do', //僅為示例,非真實的接口地址
        data: {
          file:that.data.verifyImg,
          filename:timestamp,
          filedata:that.data.verifyImg
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        success:function (res){
          const data = res.data
          console.log('成功',data);
          //do something
        },
       fail:function(res){
         console.log('失敗',res)
       }
        
      })
  }

後臺上傳base64轉換的代碼

public void upBase64() {
         System.out.println("======開始上傳圖片====");
         System.out.println(file);
        Json j = new Json();
        String FilePath = ServletActionContext.getServletContext().getRealPath(Constants.IMGPATH+"/"+Constants.PHOTOPATH);  
        File PathFile = new File(FilePath);
        try {
            // 如果是IE,那麼需要設置為text/html,否則會彈框下載
            // response.setContentType("text/html;charset=UTF-8");
            response.setContentType("application/json;charset=UTF-8");

            String FileName = request.getParameter("filename");
            String FileData = request.getParameter("filedata");
            System.out.println(FileName+"**************"+FileData);
            if (null == FileData || FileData.length() < 50) {
                j.setMsg("上傳失敗,數據太短或不存");
                j.setSuccess(false);
            } else {
                // 去除開頭不合理的數據
                FileData = FileData.substring(30);
                FileData = URLDecoder.decode(FileData, "UTF-8");
                System.out.println("FileData="+FileData);
                byte[] data = FileUtil.decode(FileData);
                /*if (null == FileName || FileName.length() < 1) {
                    FileName = System.currentTimeMillis() + ".jpg";
                }*/
                // 寫入到文件 
                FileOutputStream outputStream = new FileOutputStream(new File(PathFile,FileName)); 
                outputStream.write(data); 
                outputStream.flush(); 
                outputStream.close(); 
                System.out.println(FileName+"**************"+FileData);
                j.setMsg("上傳成功");
                j.setSuccess(true);
            }
        } catch (Exception err) {
            j.setMsg("上傳失敗");
            j.setSuccess(false);
            err.printStackTrace();
        }
        writeJson(j);
    }

以上就是詳解微信小程序官方人臉核身認證的詳細內容,更多關於微信小程序官方人臉核身認證的資料請關註WalkonNet其它相關文章!

推薦閱讀: