ContrastFaceVerifyService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace crmeb\services;
  3. use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
  4. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyRequest;
  5. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyResponse;
  6. use AlibabaCloud\Tea\Utils\Utils;
  7. use AlibabaCloud\Tea\Exception\TeaError;
  8. use AlibabaCloud\Tea\Rpc\Rpc\Config;
  9. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  10. use think\Exception;
  11. class ContrastFaceVerifyService
  12. {
  13. /**
  14. * @param string $endpoint
  15. * @return Cloudauth
  16. */
  17. public static function createClient(string $endpoint): Cloudauth
  18. {
  19. $config = new Config([
  20. "accessKeyId" => sys_config('real_access_id'),
  21. "accessKeySecret" => sys_config('real_access_secret'),
  22. "endpoint" => $endpoint
  23. ]);
  24. return new Cloudauth($config);
  25. }
  26. /**
  27. * @return ContrastFaceVerifyResponse|null
  28. */
  29. public static function check($order_id, $real_name, $card_number, $image)
  30. {
  31. $request = new ContrastFaceVerifyRequest([
  32. // 请输入场景ID。
  33. "sceneId" => sys_config('real_scene_id'),
  34. "outerOrderNo" => $order_id,
  35. // 要接入的认证方案。
  36. "productCode" => "ID_MIN",
  37. // 要进行活体检测的类型。
  38. "model" => "NO_LIVENESS",
  39. "certType" => "IDENTITY_CARD",
  40. "certName" => $real_name,
  41. "certNo" => $card_number,
  42. "faceContrastPicture" => $image,
  43. ]);
  44. // 推荐,支持服务路由。
  45. $response = self::contrastFaceVerifyAutoRoute($request);
  46. return $response;
  47. }
  48. /**
  49. * @param ContrastFaceVerifyRequest $request
  50. * @return ContrastFaceVerifyResponse
  51. */
  52. public static function contrastFaceVerifyAutoRoute(ContrastFaceVerifyRequest $request)
  53. {
  54. $endpoints = [
  55. "cloudauth.cn-shanghai.aliyuncs.com",
  56. "cloudauth.cn-beijing.aliyuncs.com"
  57. ];
  58. foreach ($endpoints as $endpoint) {
  59. try {
  60. $response = self::contrastFaceVerify($endpoint, $request);
  61. if (Utils::equalString("500", $response->code)) {
  62. continue;
  63. }
  64. return $response;
  65. } catch (Exception $err) {
  66. if (!($err instanceof TeaError)) {
  67. $err = new TeaError([], $err->getMessage(), $err->getCode(), $err);
  68. }
  69. // 网络异常,切换到下个区域调用。
  70. if (Utils::equalString("ServiceUnavailable", $err->code)) {
  71. continue;
  72. }
  73. }
  74. }
  75. return null;
  76. }
  77. /**
  78. * @param string $endpoint
  79. * @param ContrastFaceVerifyRequest $request
  80. * @return ContrastFaceVerifyResponse
  81. */
  82. public static function contrastFaceVerify($endpoint, $request)
  83. {
  84. $client = self::createClient($endpoint);
  85. // 创建RuntimeObject实例并设置运行参数。
  86. $runtime = new RuntimeOptions([]);
  87. $runtime->readTimeout = 10000;
  88. $runtime->connectTimeout = 10000;
  89. return $client->contrastFaceVerify($request, $runtime);
  90. }
  91. }