123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/2/28
- * @time: 11:33
- */
- namespace crmeb\services;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Cloudauth;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\DescribeFaceVerifyRequest;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\InitFaceVerifyRequest;
- use app\model\user\User;
- use \Exception;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Utils\Utils;
- use Darabonba\OpenApi\Models\Config;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFaceVerifyRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use think\facade\Cache;
- class AliAuthService
- {
- private static $error_msg = "错误,请重试";
- private static $code_error = [
- '200' => '认证通过',
- '201' => '姓名和身份证不一致',
- '202' => '查询不到身份信息',
- '203' => '查询不到照片或照片不可用',
- '204' => '人脸比对不一致',
- '205' => '活体检测存在风险',
- '206' => '业务策略限制',
- '210' => '认证通过',
- ];
- public static function getCodeError($code)
- {
- return self::$code_error[$code] ?? '未知错误';
- }
- public static function createClient($accessKeyId, $accessKeySecret)
- {
- $config = new Config([
- // 必填,您的 AccessKey ID
- "accessKeyId" => $accessKeyId,
- // 必填,您的 AccessKey Secret
- "accessKeySecret" => $accessKeySecret
- ]);
- // Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
- $config->endpoint = "cloudauth.aliyuncs.com";
- return new Cloudauth($config);
- }
- public static function setError($msg)
- {
- self::$error_msg = $msg;
- return false;
- }
- public static function getError($default = '错误,请重试')
- {
- return self::$error_msg ?: $default;
- }
- public static function createOn($uid)
- {
- return time() . rand(100000, 999999) . $uid;
- }
- public static function check($uid, $metaInfo = '', $url = '')
- {
- // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
- // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html
- $client = self::createClient('LTAI5tFNPGNPbhWf9SWMyEV7', 'EQ5forf1F0iPV1rHQuJxXZsIATiYdf');
- $user = User::where('uid', $uid)->find();
- if (!$user['card_id'] || !$user['real_name']) {
- return self::setError('请先填写真实姓名与身份证号码');
- }
- $orderId = self::createOn($uid);
- $token = md5($orderId);
- $compareFaceVerifyRequest = new InitFaceVerifyRequest([
- "sceneId" => 1000009317,
- "outerOrderNo" => $orderId,
- "productCode" => "ID_PRO",
- "model" => "MULTI_ACTION",
- "certType" => "IDENTITY_CARD",
- "certName" => $user['real_name'],
- "certNo" => $user['card_id'],
- "returnUrl" => $url,
- "callbackUrl" => sys_config('site_url') . '/api/authNotify',
- "metaInfo" => $metaInfo,
- "callbackToken" => $token,
- ]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->initFaceVerify($compareFaceVerifyRequest);
- if ($res->body->code !== '200') {
- return self::setError($res->body->message);
- } else {
- Cache::set($token, $uid, 1500);
- Cache::set($res->body->resultObject->certifyId, $token, 1500);
- return ['url' => $res->body->resultObject->certifyUrl, 'id' => $res->body->resultObject->certifyId];
- }
- } catch (Exception $error) {
- if (!($error instanceof TeaError)) {
- $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- }
- return self::setError($error->message);
- }
- }
- public static function result($certifyId)
- {
- // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
- // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html
- $client = self::createClient('LTAI5tFNPGNPbhWf9SWMyEV7', 'EQ5forf1F0iPV1rHQuJxXZsIATiYdf');
- $compareFaceVerifyRequest = new DescribeFaceVerifyRequest([
- "sceneId" => 1000009317,
- "certifyId" => $certifyId,
- ]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->describeFaceVerify($compareFaceVerifyRequest);
- if ($res->body->code !== '200') {
- return self::setError($res->body->message);
- } else {
- return ['passed' => $res->body->resultObject->passed, 'subCode' => $res->body->resultObject->subCode];
- }
- } catch (Exception $error) {
- if (!($error instanceof TeaError)) {
- $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- }
- return self::setError($error->message);
- }
- }
- }
|