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

今天原本本身作的小程序收集了下用戶我的信息上傳被打回來講:html

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

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

而後就去找度娘搜了下須要申請json

 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_tokenapi

返回的數據須要轉成base64碼而後顯示在image標籤上我是直接傳給後臺的微信

下面上我msg的js代碼app

msg.jside

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

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

相關文章
相關標籤/搜索