AliAuthService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/2/28
  6. * @time: 11:33
  7. */
  8. namespace crmeb\services;
  9. use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
  10. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\DescribeFaceVerifyRequest;
  11. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\InitFaceVerifyRequest;
  12. use app\model\user\User;
  13. use \Exception;
  14. use AlibabaCloud\Tea\Exception\TeaError;
  15. use AlibabaCloud\Tea\Utils\Utils;
  16. use Darabonba\OpenApi\Models\Config;
  17. use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFaceVerifyRequest;
  18. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  19. use think\facade\Cache;
  20. class AliAuthService
  21. {
  22. private static $error_msg = "错误,请重试";
  23. private static $code_error = [
  24. '200' => '认证通过',
  25. '201' => '姓名和身份证不一致',
  26. '202' => '查询不到身份信息',
  27. '203' => '查询不到照片或照片不可用',
  28. '204' => '人脸比对不一致',
  29. '205' => '活体检测存在风险',
  30. '206' => '业务策略限制',
  31. '210' => '认证通过',
  32. ];
  33. public static function getCodeError($code)
  34. {
  35. return self::$code_error[$code] ?? '未知错误';
  36. }
  37. public static function createClient($accessKeyId, $accessKeySecret)
  38. {
  39. $config = new Config([
  40. // 必填,您的 AccessKey ID
  41. "accessKeyId" => $accessKeyId,
  42. // 必填,您的 AccessKey Secret
  43. "accessKeySecret" => $accessKeySecret
  44. ]);
  45. // Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
  46. $config->endpoint = "cloudauth.aliyuncs.com";
  47. return new Cloudauth($config);
  48. }
  49. public static function setError($msg)
  50. {
  51. self::$error_msg = $msg;
  52. return false;
  53. }
  54. public static function getError($default = '错误,请重试')
  55. {
  56. return self::$error_msg ?: $default;
  57. }
  58. public static function createOn($uid)
  59. {
  60. return time() . rand(100000, 999999) . $uid;
  61. }
  62. public static function check($uid, $metaInfo = '', $url = '')
  63. {
  64. // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  65. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html
  66. $client = self::createClient('LTAI5tFNPGNPbhWf9SWMyEV7', 'EQ5forf1F0iPV1rHQuJxXZsIATiYdf');
  67. $user = User::where('uid', $uid)->find();
  68. if (!$user['card_id'] || !$user['real_name']) {
  69. return self::setError('请先填写真实姓名与身份证号码');
  70. }
  71. $orderId = self::createOn($uid);
  72. $token = md5($orderId);
  73. $compareFaceVerifyRequest = new InitFaceVerifyRequest([
  74. "sceneId" => 1000009317,
  75. "outerOrderNo" => $orderId,
  76. "productCode" => "ID_PRO",
  77. "model" => "MULTI_ACTION",
  78. "certType" => "IDENTITY_CARD",
  79. "certName" => $user['real_name'],
  80. "certNo" => $user['card_id'],
  81. "returnUrl" => $url,
  82. "callbackUrl" => sys_config('site_url') . '/api/authNotify',
  83. "metaInfo" => $metaInfo,
  84. "callbackToken" => $token,
  85. ]);
  86. try {
  87. // 复制代码运行请自行打印 API 的返回值
  88. $res = $client->initFaceVerify($compareFaceVerifyRequest);
  89. if ($res->body->code !== '200') {
  90. return self::setError($res->body->message);
  91. } else {
  92. Cache::set($token, $uid, 1500);
  93. Cache::set($res->body->resultObject->certifyId, $token, 1500);
  94. return ['url' => $res->body->resultObject->certifyUrl, 'id' => $res->body->resultObject->certifyId];
  95. }
  96. } catch (Exception $error) {
  97. if (!($error instanceof TeaError)) {
  98. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  99. }
  100. return self::setError($error->message);
  101. }
  102. }
  103. public static function result($certifyId)
  104. {
  105. // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  106. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html
  107. $client = self::createClient('LTAI5tFNPGNPbhWf9SWMyEV7', 'EQ5forf1F0iPV1rHQuJxXZsIATiYdf');
  108. $compareFaceVerifyRequest = new DescribeFaceVerifyRequest([
  109. "sceneId" => 1000009317,
  110. "certifyId" => $certifyId,
  111. ]);
  112. try {
  113. // 复制代码运行请自行打印 API 的返回值
  114. $res = $client->describeFaceVerify($compareFaceVerifyRequest);
  115. if ($res->body->code !== '200') {
  116. return self::setError($res->body->message);
  117. } else {
  118. return ['passed' => $res->body->resultObject->passed, 'subCode' => $res->body->resultObject->subCode];
  119. }
  120. } catch (Exception $error) {
  121. if (!($error instanceof TeaError)) {
  122. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  123. }
  124. return self::setError($error->message);
  125. }
  126. }
  127. }