人脸核身Button的实现

人脸核身Button的实现

HarmonyOS
2024-08-07 10:00:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
社恐的小美
// Face_authentication.ets

import { FunctionalButton, functionalButtonComponentManager } from '@kit.ScenarioFusionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import http from '@ohos.net.http';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';

interface Result {
  state?: number;
  realNameLevel?: number;
  verifyResult?: number;
  verifyToken?: string;
}

@Entry
@Component
struct SecondPage {
  obtainFaceVerifyDataInfo(authCode: string, sceneID: number, openID: string, realName: string, idNo: string,
    callback: AsyncCallback<Result>) {
    // 每一个httpRequest对应一个HTTP请求任务,不可复用
    let httpRequest = http.createHttp();
    // 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
    httpRequest.on('headersReceive', (header) => {
      hilog.info(0x0000, "testTag", "header: %{public}s", header as string);
    });
    httpRequest.request(
      // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
      "EXAMPLE_URL",
      {
        method: http.RequestMethod.POST,
        // 开发者根据自身业务需要添加header字段
        header: {
          'Content-Type': 'application/json'
        },
        // 当使用POST请求时此字段用于传递内容
        extraData: {
          "authCode": authCode,
          "sceneID": sceneID,
          "openID": openID,
          "realname": realName,
          "idNo": idNo,
        },
        expectDataType: http.HttpDataType.STRING,
        usingCache: true,
        priority: 1,
        connectTimeout: 60000,
        readTimeout: 60000,
        usingProtocol: http.HttpProtocol.HTTP1_1,
      }, (err, data) => {
      if (!err) {
        let res: Result = JSON.parse(data.result as string);
        callback(undefined, res);
      } else {
        let error: BusinessError = { code: err.code, message: err.message, name: '' }
        callback(error, undefined);
        // 取消订阅HTTP响应头事件
        httpRequest.off('headersReceive');
        // 当该请求使用完毕时,调用destroy方法主动销毁
        httpRequest.destroy();
      }
    })
  }

  build() {
    Row() {
      Column() {
        FunctionalButton({
          params: {
            // OpenType.FACE_AUTHENTICATION表示Button为人脸核身类型
            openType: functionalButtonComponentManager.OpenType.FACE_AUTHENTICATION,
            label: '人脸核身',
            // 调整Button样式
            styleOption: {
              styleConfig: new functionalButtonComponentManager.ButtonConfig()
                .fontSize(20)
                .fontColor(Color.Black)
            }
          },
          // OpenType为“FACE_AUTHENTICATION”时,回调必须选择“onFaceAuthentication”
          controller: new functionalButtonComponentManager.FunctionalButtonController()
            .onFaceAuthentication((err, data) => {
              if (err) {
                // 错误日志处理
                hilog.error(0x0000, "testTag", "error: %{public}d %{public}s", err.code, err.message);
                return;
              }
              // 成功日志处理
              hilog.info(0x0000, "testTag", "succeeded in authenticating");
              // 获取Authorization Code
              let authCode: string = data.authCode as string;
              let openID: string = data.openID as string;
              hilog.info(0x0000, "testTag", "succeeded in authCode");
              this.obtainFaceVerifyDataInfo(authCode, 2, openID, "", "", (err, data) => {
                if (err) {
                  hilog.error(0x0000, "testTag", "error: %{public}d %{public}s", err.code, err.message);
                  return;
                }
                let verifyToken: string = data.verifyToken as string;
                new functionalButtonComponentManager.FunctionalButtonController().onFaceVerification(verifyToken,
                  (error, data) => {
                    if (error) {
                      // 错误日志处理
                      hilog.error(0x0000, "testTag", "error: %{public}d %{public}s", error.code, error.message);
                      return;
                    }
                    let facialRecognitionVerificationToken = data.facialRecognitionVerificationToken;
                    let state = data.state;
                    hilog.info(0x0000, 'testTag', 'auth result success');
                  });
              })
            })
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
分享
微博
QQ
微信
回复
2024-08-07 14:50:06
相关问题
打开APP Button实现
941浏览 • 1回复 待解决
实名认证Button实现
1135浏览 • 1回复 待解决
地图选点Button实现
878浏览 • 1回复 待解决
选择头像Button实现
1330浏览 • 1回复 待解决
选择收货地址Button实现
1340浏览 • 1回复 待解决
armonyOS 如何固定程序运行CPU
993浏览 • 1回复 待解决
选择发票抬头Button实现
881浏览 • 1回复 待解决
打开授权设置页Button实现
1059浏览 • 1回复 待解决
实时验证手机号Button实现
1172浏览 • 1回复 待解决
快速验证手机号Button实现
1376浏览 • 1回复 待解决
HarmonyOS 人脸检测
842浏览 • 1回复 待解决
如何禁止Button点击事件?
1330浏览 • 1回复 待解决
HarmonyOS 相机人脸监听相关问题
920浏览 • 1回复 待解决
能修改Button默认样式吗?
1152浏览 • 1回复 待解决
如何释放人脸比对服务资源?
826浏览 • 0回复 待解决