詳解微信小程序官方人臉核身認(rèn)證
小程序收集了下用戶個(gè)人信息上傳被打回來說:
你好,小程序頁面功能涉及:采集用戶生物特征(人臉照片或視頻)及其他敏感信息,用于身份認(rèn)識(shí)或識(shí)別,
為保障用戶敏感隱私身份信息,平臺(tái)暫不支持此功能。請(qǐng)去除相關(guān)功能后重新提交。
然后就去找度娘搜了下需要申請(qǐng)
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
首先要給申請(qǐng)下來的接口發(fā)送倆個(gè)參數(shù):名字、身份證號(hào)碼
photo.js
data: { openid: '', custName: '姓名', custIdCard: '身份證號(hào)碼', verifyImg: '' }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ 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") { //調(diào)用人臉識(shí)別 _this.startface(_this.data.custName.replace(/(^\s*)|(\s*)$/g, ""), _this.data.custIdCard); //身份證名稱,身份證號(hào)碼 return; } wx.showToast('微信版本過低,暫時(shí)無法使用此功能,請(qǐng)升級(jí)微信最新版本') }, fail: function(res){ wx.showToast('微信版本過低,暫時(shí)無法使用此功能,請(qǐng)升級(jí)微信最新版本') } }) }, startface(name, idcard) { console.log('我進(jìn)來了?。?!'); var _this = this; wx.startFacialRecognitionVerify({ name: _this.data.custName, //身份證名稱 idCardNumber: _this.data.custIdCard, //身份證號(hào)碼 success: function (res) { var verifyResult = res.verifyResult; //認(rèn)證結(jié)果 //調(diào)用接口 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=恭喜您信息核驗(yàn)成功&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=人臉核身認(rèn)證成功' // }); }, checkAliveType: 2, //屏幕閃爍(人臉核驗(yàn)的交互方式,默認(rèn)0,讀數(shù)字) fail: err => { wx.showToast('請(qǐng)保持光線充足,面部正對(duì)手機(jī),且無遮擋') wx.navigateTo({ url: '../msg/msg?msg=請(qǐng)保持光線充足,面部正對(duì)手機(jī),且無遮擋,請(qǐng)退出重新操作' }); } }) }
主要坑的是這樣下來得申請(qǐng)好幾次接口到最后業(yè)務(wù)還需要unionid所以還得去微信開放平臺(tái)申請(qǐng)認(rèn)證
然后想要拉取核身結(jié)果的圖片的時(shí)候就需要黨上面的https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=access_token
返回的數(shù)據(jù)需要轉(zhuǎn)成base64碼然后顯示在image標(biāo)簽上我是直接傳給后臺(tái)的
下面上我msg的js代碼
msg.js
const app = getApp(); Page({ /** * 頁面的初始數(shù)據(jù) */ data: { msg:'', verifyImg:'', url:app.globalData.PostData }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ 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', //僅為示例,非真實(shí)的接口地址 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) } }) }
后臺(tái)上傳base64轉(zhuǎn)換的代碼
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,那么需要設(shè)置為text/html,否則會(huì)彈框下載 // 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("上傳失敗,數(shù)據(jù)太短或不存"); j.setSuccess(false); } else { // 去除開頭不合理的數(shù)據(jù) 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); }
以上就是詳解微信小程序官方人臉核身認(rèn)證的詳細(xì)內(nèi)容,更多關(guān)于微信小程序官方人臉核身認(rèn)證的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Javascript中3個(gè)需要注意的運(yùn)算符
這篇文章主要介紹了Javascript中3個(gè)需要注意的運(yùn)算符,這3個(gè)運(yùn)算符的使用有很多需要注意的地方和有意思的地方,需要的朋友可以參考下2015-04-04JavaScript實(shí)現(xiàn)數(shù)組元素增減的方法示例
數(shù)組元素的增刪是JavaScript的一個(gè)特點(diǎn),因?yàn)槠渌芏嗑幊陶Z言的數(shù)組是不允許增加或者刪除元素的,本文給大家介紹了JavaScript實(shí)現(xiàn)數(shù)組元素增減的方法示例,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-07-07js實(shí)現(xiàn)把圖片的絕對(duì)路徑轉(zhuǎn)為base64字符串、blob對(duì)象再上傳
本文主要介紹了JavaScript把項(xiàng)目本地的圖片或者圖片的絕對(duì)路徑轉(zhuǎn)為base64字符串、blob對(duì)象再上傳的方法,具有一定的參考價(jià)值,需要的朋友一起來看下吧2016-12-12JavaScript canvas實(shí)現(xiàn)動(dòng)態(tài)點(diǎn)線效果
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)動(dòng)態(tài)點(diǎn)線效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08javascript實(shí)現(xiàn)圖片預(yù)加載和懶加載
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)圖片預(yù)加載和懶加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03淺析Javascript中“==”與“===”的區(qū)別
這篇文章主要介紹了淺析Javascript中“==”與“===”的區(qū)別,非常的全面,這里推薦給小伙伴們2014-12-12