123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace crmeb\services;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyRequest;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\ContrastFaceVerifyResponse;
- use AlibabaCloud\Tea\Utils\Utils;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Rpc\Rpc\Config;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use think\Exception;
- class ContrastFaceVerifyService
- {
- /**
- * @param string $endpoint
- * @return Cloudauth
- */
- public static function createClient(string $endpoint): Cloudauth
- {
- $config = new Config([
- "accessKeyId" => sys_config('real_access_id'),
- "accessKeySecret" => sys_config('real_access_secret'),
- "endpoint" => $endpoint
- ]);
- return new Cloudauth($config);
- }
- /**
- * @return ContrastFaceVerifyResponse|null
- */
- public static function check($order_id, $real_name, $card_number, $image)
- {
- $request = new ContrastFaceVerifyRequest([
- // 请输入场景ID。
- "sceneId" => sys_config('real_scene_id'),
- "outerOrderNo" => $order_id,
- // 要接入的认证方案。
- "productCode" => "ID_MIN",
- // 要进行活体检测的类型。
- "model" => "NO_LIVENESS",
- "certType" => "IDENTITY_CARD",
- "certName" => $real_name,
- "certNo" => $card_number,
- "faceContrastPicture" => $image,
- ]);
- // 推荐,支持服务路由。
- $response = self::contrastFaceVerifyAutoRoute($request);
- return $response;
- }
- /**
- * @param ContrastFaceVerifyRequest $request
- * @return ContrastFaceVerifyResponse
- */
- public static function contrastFaceVerifyAutoRoute(ContrastFaceVerifyRequest $request)
- {
- $endpoints = [
- "cloudauth.cn-shanghai.aliyuncs.com",
- "cloudauth.cn-beijing.aliyuncs.com"
- ];
- foreach ($endpoints as $endpoint) {
- try {
- $response = self::contrastFaceVerify($endpoint, $request);
- if (Utils::equalString("500", $response->code)) {
- continue;
- }
- return $response;
- } catch (Exception $err) {
- if (!($err instanceof TeaError)) {
- $err = new TeaError([], $err->getMessage(), $err->getCode(), $err);
- }
- // 网络异常,切换到下个区域调用。
- if (Utils::equalString("ServiceUnavailable", $err->code)) {
- continue;
- }
- }
- }
- return null;
- }
- /**
- * @param string $endpoint
- * @param ContrastFaceVerifyRequest $request
- * @return ContrastFaceVerifyResponse
- */
- public static function contrastFaceVerify($endpoint, $request)
- {
- $client = self::createClient($endpoint);
- // 创建RuntimeObject实例并设置运行参数。
- $runtime = new RuntimeOptions([]);
- $runtime->readTimeout = 10000;
- $runtime->connectTimeout = 10000;
- return $client->contrastFaceVerify($request, $runtime);
- }
- }
|