小程序人脸核身

一、先去腾讯云平台开通人脸核身功能,需要填写正确的小程序APPID

二、开通后、进入账号信息中->访问管理->访问秘钥,可得到调用API接口的参数:

三、进入微信小程序中申请类目:政府/明生 申请成功后、可在接口设置授权中使用。

四、在小程序设置中关联第三方平台,记住不要分配开发权限

五、下载小程序SDK ,下载位置见步骤1的图。


六、根据文档、在小程序代码中做相应的配置
官方文档:https://cloud.tencent.com/document/product/1007/31071

1、将小程序 SDK 文件夹放在小程序根目录下,使用 require 函数引入。

const Verify = require('/verify_mpsdk/main');

2、初始化慧眼实名核身 SDK。
在 App.js 的 onLaunch() 中加入相应代码,在 App.json 文件里添加活体验证页面verify_mpsdk/index/index

//app.js
App({
 onLaunch: function () {
     // 初始化慧眼实名核身组件
     const Verify = require('/verify_mpsdk/main');
     Verify.init();
 }
}) 
// app.json
{
 "pages":[
     "verify_mpsdk/index/index"
 ]
}

3、调用 SDK 功能函数 wx.startVerify()。
在需要实名认证的地方调用 wx.startVerify() 进入实名认证页面,认证完成会触发对应的回调函数。

// 单击某个按钮时,触发该函数
gotoVerify: function () {
 let BizToken = getBizToken();// 去客户后端调用DetectAuth接口获取BizToken
 // 调用实名核身功能
 wx.startVerify({
     data: {
         token: BizToken // BizToken
     },
     success: (res) => { // 验证成功后触发
         // res 包含验证成功的token, 这里需要加500ms延时,防止iOS下不执行后面的逻辑
         setTimeout(() => {
             // 验证成功后,拿到token后的逻辑处理,具体以客户自身逻辑为准
         }, 500);
     },
     fail: (err) => {  // 验证失败时触发
         // err 包含错误码,错误信息,弹窗提示错误
         setTimeout(() => {
             wx.showModal({
                 title: "提示",
                 content: err.ErrorMsg,
                 showCancel: false
             })
         }, 500);
     }
 });
}

4、上图的getBizToken(),是小程序的一个普通请求,主要作用是重自己的服务器后台去拿腾讯的token,详情代码请看第7步。

5、添加域名服务器白名单,登录小程序后台—>开发—>开发设置—>服务器域名
小程序前端接口请求有域名白名单限制,未添加白名单的域名只能在调试模式下运行。您需要在小程序上线前需要将以下两个域名添加至白名单:

// request 合法域名、uploadFile 合法域名、downloadFile 合法域名这三种都要添加
faceid.qq.com、btrace.qq.com


7、需要在自己的服务器中接入腾讯接口调用API 、去获取小程序所需的 BizToken

 1、在pom文件中引入:

 <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
            <!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询最新版本 -->
            <version>3.0.98</version>
</dependency>

2、java 代码访问腾讯实例:

package com.lansent.howjoy.controller.func.lookdoorminiapp;

import com.alibaba.fastjson.JSON;
import com.lansent.howjoy.client.common.validator.HJAssert;
import com.lansent.howjoy.client.exception.CustomException;
import com.lansent.howjoy.common.JsonResult;
import com.lansent.howjoy.controller.BaseController;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.faceid.v20180301.FaceidClient;

import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;

import java.util.HashMap;

/**
 * @author molei
 * @date 2019/10/25 14:56
 */
@Controller
@RequestMapping("/lookdoor/miniapp/checkFace")
public class MiniCheckFaceAction extends BaseController {
    //这两个参数如何获取,请看下面的图
    private final String secretId = "secretId";
    private final String secretKey = "secretKey";
    /**
     * 腾讯人脸核身 小程序获取 BizToken
     **/
    @RequestMapping("getBizToken.json")
    @ResponseBody
    public JsonResult getBizToken() {        
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);
            String params = "{\"RuleId\":\"0\"}";
            DetectAuthRequest req = DetectAuthRequest.fromJsonString(params, DetectAuthRequest.class);

            DetectAuthResponse resp = client.DetectAuth(req);

            return JsonResult.genJsonResultByOk(DetectAuthRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            throw new CustomException("获取人脸核身BizToken失败!");
        }
    }

    /**
     * 腾讯人脸核身 小程序获取 最终结果
     **/
    @RequestMapping("getInfo.json")
    @ResponseBody
    public JsonResult getBizToken(String bizToken) {
        HJAssert.hasText(bizToken,"人脸核身获取最终结果参数错误!");        
        HashMap<String,String> map = new HashMap<>(16);
        //bizToken 91F7A062-3357-4670-BAFE-34ECD382A101
        map.put("BizToken",bizToken);
        //业务ID
        map.put("RuleId","0");
        //0:全部;1:文本类;2:身份证正反面;3:视频最佳截图照片;4:视频
        map.put("InfoType","0");
        String params = JSON.toJSONString(map);
        try {
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);

            GetDetectInfoRequest req = GetDetectInfoRequest.fromJsonString(params, GetDetectInfoRequest.class);

            GetDetectInfoResponse resp = client.GetDetectInfo(req);

            return JsonResult.genJsonResultByOk(resp);
        } catch (TencentCloudSDKException e) {
            throw new CustomException("获取人脸核身最终结果失败!");
        }
    }
}

获取参数:secretKey,secretId 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值