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

 更新时间:2021年06月04日 11:26:01   作者:Ferocious  
本文主要介绍了微信小程序官方人脸核身认证,感兴趣的同学,可以参考下,并且试验一下。

小程序收集了下用户个人信息上传被打回来说:

你好,小程序页面功能涉及:采集用户生物特征(人脸照片或视频)及其他敏感信息,用于身份认识或识别,

为保障用户敏感隐私身份信息,平台暂不支持此功能。请去除相关功能后重新提交。

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

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

以上就是详解微信小程序官方人脸核身认证的详细内容,更多关于微信小程序官方人脸核身认证的资料请关注脚本之家其它相关文章!

相关文章

  • 用js模拟JQuery的show与hide动画函数代码

    用js模拟JQuery的show与hide动画函数代码

    用javascript实现的模拟jquery下的显示与隐藏的动画效果,学习的朋友可以参考下。
    2010-09-09
  • javascript实现平滑无缝滚动

    javascript实现平滑无缝滚动

    这篇文章主要为大家详细介绍了javascript实现平滑无缝滚动的具体代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 用JavaScript调用WebService的示例

    用JavaScript调用WebService的示例

    JavaScript用htc实现WebService的调用
    2008-04-04
  • 详解jQuery插件开发方式

    详解jQuery插件开发方式

    本文介绍了jQuery扩展、私有域、定义插件的基本步骤等知识,有需要的朋友可以看下
    2016-11-11
  • 微信小程序实现左侧滑动导航栏

    微信小程序实现左侧滑动导航栏

    这篇文章主要为大家详细介绍了微信小程序实现左侧滑动导航栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • 在JS中最常看到切最容易迷惑的语法(转)

    在JS中最常看到切最容易迷惑的语法(转)

    发现一篇JS中比较容易迷惑的语法的解释,挺有用的,转载下,与大家分享
    2010-10-10
  • javascript中的作用域和闭包详解

    javascript中的作用域和闭包详解

    这篇文章主要向大家介绍了javascript中的作用域和闭包,以及利用js闭包实现循环绑定事件,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • webpack独立打包和缓存处理详解

    webpack独立打包和缓存处理详解

    这篇文章主要介绍了webpack独立打包和缓存处理的相关资料,文中介绍的非常详细,对大家学习webpack具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • 一次围绕setTimeout的前端面试经验分享

    一次围绕setTimeout的前端面试经验分享

    这篇文章主要跟大家分享了一次围绕setTimeout展开的前端面试经验,是一篇有意思的围绕setTimeout展开的前端开发考题,考察了for循环、定时器setTimeout()、JavaScript闭包、匿名函数和Promise等,一不小心你可能就会做错,快来看看你有没有掌握了上面的知识。
    2017-06-06
  • 小程序登录之支付宝授权的实现示例

    小程序登录之支付宝授权的实现示例

    这篇文章主要介绍了小程序登录之支付宝授权的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论