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


今天本來自己做的小程序收集了下用戶個人信息上傳被打回來說:

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

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

然后就去找度娘搜了下需要申請

 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
  1 data: {
  2     openid: '',
  3     custName: '姓名',
  4     custIdCard: '身份證號碼',
  5     verifyImg: ''
  6   },
  7   /**
  8    * 生命周期函數--監聽頁面加載
  9    */
 10   onLoad: function (options) {
 11     this.setData({
 12       custName: options.custName,
 13       custIdCard: options.custIdCard
 14     });
 15     var _this = this;
 16     wx.checkIsSupportFacialRecognition({
 17       checkAliveType: 2,
 18       success: function (res) {
 19         if (res.errCode === 0 || res.errMsg === "checkIsSupportFacialRecognition:ok") {
 20           //調用人臉識別
 21           _this.startface(_this.data.custName.replace(/(^\s*)|(\s*)$/g, ""), _this.data.custIdCard); //身份證名稱,身份證號碼
 22           return;
 23         }
 24         wx.showToast('微信版本過低,暫時無法使用此功能,請升級微信最新版本')
 25       },
 26       fail: function(res){
 27         wx.showToast('微信版本過低,暫時無法使用此功能,請升級微信最新版本')
 28       }
 29 
 30     })
 31   },
 32   startface(name, idcard) {
 33     console.log('我進來了!!!');
 34     var _this = this;
 35     wx.startFacialRecognitionVerify({
 36       name: _this.data.custName, //身份證名稱
 37       idCardNumber: _this.data.custIdCard, //身份證號碼
 38       success: function (res) {
 39         var verifyResult = res.verifyResult; //認證結果
 40         //調用接口
 41 
 42 
 43         wx.request({
 44           url: 'https://api.weixin.qq.com/cgi-bin/token?appid=wx2cafec51ec4c2153&secret=8d3e68a5a2c702673340d72d1c7db4cc&grant_type=client_credential',
 45           data: {
 46 
 47           },
 48           method: 'POST',
 49           header: {
 50             'content-type': 'application/json;charset=utf-8'
 51           },
 52           success: function (res) {
 53             console.log(res.data);
 54             console.log(res.data.access_token)
 55             var token = res.data.access_token;
 56             wx.request({
 57               url: 'https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=' + res.data.access_token,
 58               data: {
 59                 verify_result: verifyResult
 60               },
 61               method: 'POST',
 62               header: {
 63                 'content-type': 'application/json;charset=utf-8'
 64               },
 65               success: function (res) {
 66                 console.log(res.data)
 67                 console.log('我終於成功了。。。。')
 68                 wx.request({
 69                   url: 'https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=' + token,
 70                   data: {
 71                     verify_result: verifyResult
 72                   },
 73                   method: 'POST',
 74                   responseType: 'arraybuffer',
 75                   header: {
 76                     'content-type': 'application/json;charset=utf-8',
 77                   },
 78                   success: (res) => {
 79                     // console.log('data:image/png;base64,'+wx.arrayBufferToBases64(res))
 80                   
 81                     console.log(res.data);
 82                     var base64 = wx.arrayBufferToBase64(res.data);
 83                     _this.setData({ verifyImg:'data:image/png;base64,'+ base64})
 84                     wx.navigateTo({
 85                       url: '../msg/msg?msg=恭喜您信息核驗成功&verifyImg=' + _this.data.verifyImg
 86                     });
 87                   },
 88                   fail: function (res) {
 89                     console.log('失敗', res.data)
 90                   }
 91                 })
 92 
 93               },
 94               fail: function (res) {
 95 
 96               }
 97             })
 98           },
 99           fail: function (res) {
100 
101           }
102         })
103 
104 
105 
106         console.log(verifyResult)
107         // wx.navigateTo({
108         //   url: '../msg/msg?msg=人臉核身認證成功'
109         // });
110       },
111       checkAliveType: 2, //屏幕閃爍(人臉核驗的交互方式,默認0,讀數字)
112       fail: err => {
113         wx.showToast('請保持光線充足,面部正對手機,且無遮擋')
114         wx.navigateTo({
115           url: '../msg/msg?msg=請保持光線充足,面部正對手機,且無遮擋,請退出重新操作'
116         });
117       }
118     })
119   }
View Code

 

主要坑的是這樣下來得申請好幾次接口到最后業務還需要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);
    }

 另外說一句一定要保持程序員優良的美德,啊啊啊程序源碼開源一定的小程序是真的坑。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM