当前位置:   article > 正文

vue+springboot+腾讯云人脸核身_开发通过微信扫描二维码进行人脸识别认证

开发通过微信扫描二维码进行人脸识别认证

目录

业务场景

腾讯云

后台部分

前台部分


业务场景

业务场景,VUE做PC端,然后要用户输入姓名和身份证号点击生成二维码,微信扫描进行人脸识别,最后识别后,页面显示认真成功失败。

腾讯云

开始先登录自己注册的腾讯云,云产品——人脸核身

 创建业务流程

 这里自己根据自己需求改,根据官方文档说明,普通H5不需要绑定公众号,浮层需要公众号,如果单纯想实现输入姓名和身份证啥的认证,我这里用的普通H5。看个人需求

 普通的话微信公众号名称输入的无所谓了,随便输

 这个业务名称就是微信认证页面的显示你的业务名

选择权威库,既然做人脸识别了,当然用权威的了

 这里会弹一大堆有的没的。。选择不校验,直接输入姓名和身份证认证即可

 也就是我们前台传姓名和身份证参数,传给自己的后台,我们后台再发给腾讯

活体选动作,这个没啥说的。。 

 这个看自己心情,我心情好写鎏金哇卡雅酷咧也没人说啥!

然后提交审核

 审核成功后,记住你的RuleId这个业务id我们对接需要这个参数

接下来,我们要弄访问秘钥,官方文档说可以不需要访问秘钥,容易有风险,我没管,我直接生成的外部访问秘钥

云产品——访问秘钥

 新建秘钥启用就行,秘钥的id和密文记下来,后台需要。。

这里资源包购买

如果按我的配的微信普通H5的,注册时候选权威库的,就买基础人脸核身权威库的,1K次应该是930块钱那个

资源包一定看好再买啊,别买错了怨我,我这里给提个醒

有免费的100次可以试用

后台部分

maven依赖

  1. <!-- 腾讯云sdk -->
  2. <dependency>
  3. <groupId>com.tencentcloudapi</groupId>
  4. <artifactId>tencentcloud-sdk-java</artifactId>
  5. <version>3.1.97</version>
  6. </dependency>

后台接口

先在application加配置(看你个人)

秘钥id,秘钥密文,业务id,回调地址PC路径和手机路径(个人业务),后面内容自己配自己的

  1. tencentcloud:
  2. face:
  3. SecretId:
  4. SecretKey:
  5. RuleId: 2
  6. RedirectUrl:
  7. RedirectWxUrl:

然后引

  1. @Value("${tencentcloud.face.SecretId}")
  2. private String SecretId;
  3. @Value("${tencentcloud.face.SecretKey}")
  4. private String SecretKey;
  5. @Value("${tencentcloud.face.RuleId}")
  6. private String RuleId;
  7. @Value("${tencentcloud.face.RedirectUrl}")
  8. private String RedirectUrl;
  9. @Value("${tencentcloud.face.RedirectWxUrl}")
  10. private String RedirectWxUrl;

下面这个是前台传姓名和身份证号的,然后我们把前台传的信息,按照腾讯云提供的写法发送给腾讯云,我们需要的参数

必填项:

RuleId 业务id,就是我上面写的注册的时候让你记得id

idCard 身份证号

name 姓名

非必填项:

Extra: 获取验证结果时获取的,这个看你个人愿意传就传,不传就不传无所谓

RedirectUrl:用户识别之后,在微信浏览器上显示的回调页面,这个可以配自己画的页面,这里说下,虽然说是非必填,我个人认为最好自己配回调页面,要不然用户识别结束之后,如果你没配会返回人脸核身的首页,这样用户又该净事叨叨叨的了。

  1. // 腾讯微信h5 人脸核身
  2. @RequestMapping(value = "detectAuth", method = RequestMethod.POST)
  3. @ResponseBody
  4. public Result detectAuth(@RequestBody String json) throws Exception {
  5. Result res = new Result();
  6. try {
  7. // 参数
  8. JSONObject jsonObject = JSONObject.parseObject(json);
  9. String token = jsonObject.getString("token");
  10. String type = jsonObject.getString("type");
  11. Map<String, Object> sign = redisCacheStorage.get(token, new HashMap<>());
  12. if (sign == null) {
  13. res.setCode(ResultCode.TOKENEXCEPTION);
  14. res.setMessage("未登陆!");
  15. return res;
  16. }
  17. // 身份证
  18. String idCard = jsonObject.getString("idCard");
  19. // 姓名
  20. String name = jsonObject.getString("name");
  21. // 企业id
  22. String uuid = sign.get(SessionKey.ENTERPRISE_ID.key()).toString();
  23. if (StringUtils.isNotBlank(uuid)) {
  24. // 腾讯云部分======start
  25. // 访问秘钥
  26. Credential cred = new Credential(SecretId, SecretKey);
  27. HttpProfile httpProfile = new HttpProfile();
  28. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  29. ClientProfile clientProfile = new ClientProfile();
  30. clientProfile.setHttpProfile(httpProfile);
  31. FaceidClient client = new FaceidClient(cred, "ap-beijing",
  32. clientProfile);
  33. DetectAuthRequest req = new DetectAuthRequest();
  34. // 业务id
  35. req.setRuleId(RuleId);
  36. // 身份证
  37. req.setIdCard(idCard);
  38. // 姓名
  39. req.setName(name);
  40. // 这个参数 透传字段,在获取验证结果时返回
  41. req.setExtra(uuid);
  42. // 根据业务 回调地址
  43. if("SJ".equals(type)){
  44. req.setRedirectUrl(RedirectWxUrl);
  45. }else if("PC".equals(type)){
  46. req.setRedirectUrl(RedirectUrl);
  47. }
  48. DetectAuthResponse resp = client.DetectAuth(req);
  49. // 腾讯云=========end
  50. // String result = DetectAuthResponse.toJsonString(resp);
  51. // log.info("detectAuth接口结果:" + result);
  52. return ResultGenerator.genSuccessResult(resp);
  53. }
  54. return res;
  55. } catch (Exception e) {
  56. return ResultGenerator.genFailResult(e.getMessage());
  57. }
  58. }

上面接口我们前台传完后,会返回给前台biztoken和url

 这个时候我们前台用url生成二维码,代码一会再发,用户这个时候就可以用微信扫码自动进入微信浏览器的人脸识别系统,根据步骤提示进行人脸识别(这个腾讯的系统页面不截图了,自己扫就看到了)

我们前台要把返回的biztoken再传给后台获取认证结果

这次我们需要给腾讯

biztoken 刚才返给前台的不用说了吧,再传回来就完事了

RuleId 业务id,同上面

InfoType 后台写死传1

  1. // 腾讯微信h5 人脸核身
  2. @RequestMapping(value = "getDetectInfo", method = RequestMethod.POST)
  3. @ResponseBody
  4. public Result getDetectInfo(@RequestBody String json1) {
  5. JSONObject jsonObject1 = JSONObject.parseObject(json1);
  6. String token = jsonObject1.getString("token");
  7. Map<String, Object> sign = redisCacheStorage.get(token, new HashMap<>());
  8. if (sign == null) {
  9. Result res = new Result();
  10. res.setCode(ResultCode.TOKENEXCEPTION);
  11. res.setMessage("未登陆!");
  12. return res;
  13. }
  14. // bizToken,个人当前次人脸识别的token码,用来发送获取成功结果
  15. String bizToken = jsonObject1.getString("bizToken");
  16. JSONObject json = new JSONObject();
  17. json.put("code", -1);
  18. try {
  19. //访问秘钥
  20. Credential cred = new Credential(SecretId, SecretKey);
  21. HttpProfile httpProfile = new HttpProfile();
  22. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  23. ClientProfile clientProfile = new ClientProfile();
  24. clientProfile.setHttpProfile(httpProfile);
  25. FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);
  26. GetDetectInfoRequest req = new GetDetectInfoRequest();
  27. req.setBizToken(bizToken);
  28. req.setRuleId(RuleId);
  29. req.setInfoType("1");// 0:全部;1:文本类;2:身份证正反面;3:视频最佳截图照片;4:视频。如
  30. // 134表示拉取文本类、视频最佳截图照片、视频
  31. GetDetectInfoResponse resp = client.GetDetectInfo(req);
  32. String result = DetectAuthResponse.toJsonString(resp);
  33. // log.info("getDetectInfo接口结果:" + result);
  34. JSONObject resultJson = JSONObject.parseObject(result);
  35. System.out.println("获取认证结果==========" + resultJson);
  36. Integer resultCode = resultJson.getJSONObject("DetectInfo")
  37. .getJSONObject("Text").getInteger("ErrCode");
  38. if (resultCode != null) {
  39. if (resultCode == 0) {
  40. json.put("code", 0);
  41. json.put("msg", "成功");
  42. sign.put(SessionKey.ENTERPRISE_FACE.key(), 0);
  43. redisCacheStorage.set(token, sign);
  44. } else {
  45. json.put("code", 1);
  46. json.put(
  47. "msg",
  48. "失败:"
  49. + resultJson.getJSONObject("DetectInfo")
  50. .getJSONObject("Text")
  51. .getString("ErrMsg"));
  52. }
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return ResultGenerator.genSuccessResult(json);
  58. }

 然后ResultCode就是返回结果了

前台部分

vue部分代码

我没全粘方法,因为都是我自己业务,没必要

biztoken我们要传给后台获取结果的

requestId我自己业务的

qrCode是我们把返回的url转成二维码给用户扫描进行识别用的,下面的timeout就是每5秒我们都进行一次获取认证结果,好能即时取到认证结果

  1. this.$http.post('detectAuth', param).then((response) => {
  2. if (response.error_code === 0) {
  3. document.getElementById('qrcodeId').innerHTML = ''
  4. this.bizToken = response.data.bizToken
  5. this.requestId = response.data.requestId
  6. this.qrCodeShow = true
  7. var qrcode = new QRCode(this.$refs.qrCodeUrl, {
  8. text: response.data.url,
  9. width: 210,
  10. height: 210,
  11. colorDark: '#000000',
  12. colorLight: '#ffffff',
  13. correctLevel: QRCode.CorrectLevel.H
  14. })
  15. this.t_show = setTimeout(function() {
  16. that.getDetectInfo(type)
  17. }, 5000)
  18. }
  19. })

 获取结果

  1. getDetectInfo(type) {
  2. const that = this
  3. const param = {
  4. 'bizToken': this.bizToken
  5. }
  6. this.$http.post('getDetectInfo', param).then((response) => {
  7. if (response.data.code === 0) {
  8. this.$message.success('人脸识别通过!')
  9. if (type === 1) { // 法人
  10. this.qrCodeImgShow = true
  11. this.qrCodeShow = false
  12. this.butShow = false
  13. } else { // 经办人
  14. this.qrCodeImgShow1 = true
  15. this.qrCodeShow1 = false
  16. this.butShow1 = false
  17. }
  18. } else {
  19. this.t_info = setTimeout(function() {
  20. that.getDetectInfo(type)
  21. }, 5000)
  22. }
  23. })
  24. }

成功了就把图片渲染上去就行

这里讲下回调页面,就是后台发给腾讯配置的路径,那个页面是自己画的,但是路径配置是在识别前生成二维码的时候发给腾讯的,也就是说那个路径只能配一个,不能动态配,有些小伙伴需求可能要腾讯认证成功和失败显示俩页面。。这时候我们做的时候可能在页面上动态获取状态,但是那个回调页面是在微信浏览器里打开的,所以获取不到项目状态机里的状态,这里提一嘴,其实biztoken会返回给回调页面的

根据打印路径自己截取BizToken然后发给后台获取结果,在动态更新页面成功失败就可以了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/938190?site
推荐阅读
相关标签
  

闽ICP备14008679号