微信小程序官方人脸核身认证

今天原本本身作的小程序收集了下用户我的信息上传被打回来讲: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); }

 另外说一句必定要保持程序员优良的美德,啊啊啊程序源码开源必定的小程序是真的坑。

相关文章
相关标签/搜索