详解微信小程序官方人脸核身认证
小程序收集了下用户个人信息上传被打回来说:
你好,小程序页面功能涉及:采集用户生物特征(人脸照片或视频)及其他敏感信息,用于身份认识或识别,
为保障用户敏感隐私身份信息,平台暂不支持此功能。请去除相关功能后重新提交。
然后就去找度娘搜了下需要申请
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); }
以上就是详解微信小程序官方人脸核身认证的详细内容,更多关于微信小程序官方人脸核身认证的资料请关注我们其它相关文章!
赞 (0)