Lae/app/Support/RealNameSupport.php

161 lines
4.1 KiB
PHP
Raw Normal View History

2023-01-14 21:37:25 +00:00
<?php
namespace App\Support;
2023-02-06 11:26:16 +00:00
use App\Exceptions\CommonException;
2023-01-14 21:37:25 +00:00
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
2023-01-15 13:24:16 +00:00
use Illuminate\Support\Facades\Log;
2023-01-14 21:37:25 +00:00
use Illuminate\Support\Str;
/**
* 实名认证支持
*/
class RealNameSupport
{
private string $url = 'https://faceidh5.market.alicloudapi.com';
2023-01-30 16:14:07 +00:00
2023-01-14 21:37:25 +00:00
private string $app_code;
private PendingRequest $http;
public function __construct()
{
$this->app_code = config('settings.supports.real_name.code');
$this->http = Http::withHeaders([
2023-02-07 09:04:11 +00:00
'Authorization' => 'APPCODE '.$this->app_code,
2023-01-14 21:37:25 +00:00
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
'Accept' => 'application/json',
])->baseUrl($this->url);
}
/**
* 创建实名认证请求
*
* @param $user_id
* @param $name
* @param $id_card
* @return string
2023-02-07 09:04:11 +00:00
*
2023-02-06 11:26:16 +00:00
* @throws CommonException
2023-01-14 21:37:25 +00:00
*/
public function create($user_id, $name, $id_card): string
{
$id = Str::random(32);
2023-02-07 09:04:11 +00:00
Cache::remember('real_name:'.$id, 600, function () use ($user_id, $name, $id_card) {
2023-01-14 21:37:25 +00:00
return [
'user_id' => $user_id,
'name' => $name,
'id_card' => $id_card,
];
});
return $this->submit($id);
}
/** 实名认证服务 发送请求
*
2023-02-07 09:04:11 +00:00
* @param string $id
2023-01-14 21:37:25 +00:00
* @return string
2023-02-07 09:04:11 +00:00
*
2023-02-06 11:26:16 +00:00
* @throws CommonException
2023-01-14 21:37:25 +00:00
*/
private function submit(string $id): string
{
2023-02-07 09:04:11 +00:00
$real_name = Cache::get('real_name:'.$id);
2023-01-14 21:37:25 +00:00
2023-02-07 09:04:11 +00:00
if (! $real_name) {
2023-01-14 21:37:25 +00:00
abort(404, '找不到实名认证请求');
}
$data = [
'bizNo' => $id,
'idNumber' => $real_name['id_card'],
'idName' => $real_name['name'],
2023-02-07 09:04:11 +00:00
'pageTitle' => config('app.display_name').' 实名认证',
2023-01-14 21:37:25 +00:00
'notifyUrl' => route('public.real-name.notify'),
'procedureType' => 'video',
'txtBgColor' => '#cccccc',
'ocrIncIdBack' => 'false',
'ocrOnly' => 'false',
'pageBgColor' => 'false',
'retIdImg' => 'false',
'returnImg' => 'false',
'returnUrl' => route('public.real-name.process'),
];
$resp = $this->http->asForm()->post('/edis_ctid_id_name_video_ocr_h5', $data)->json();
2023-02-07 09:04:11 +00:00
if (! $resp || $resp['code'] !== '0000') {
2023-02-06 11:26:16 +00:00
throw new CommonException('调用远程服务器时出现了问题,请检查身份证号码是否正确。');
2023-01-14 21:37:25 +00:00
}
return $resp['verifyUrl'];
}
2023-01-14 23:25:05 +00:00
/**
* 验证实名认证请求
*
2023-02-07 09:04:11 +00:00
* @param array $request
2023-01-14 23:25:05 +00:00
* @return array|bool
*/
public function verify(array $request): array|bool
{
$data = json_decode($request['data'], true);
2023-01-22 05:08:30 +00:00
Log::debug('实名认证回调', $request);
2023-01-14 23:25:05 +00:00
$verify = $this->verifyIfSuccess($request['data'], $request['sign']);
2023-02-07 09:04:11 +00:00
if (! $verify) {
2023-01-15 13:24:16 +00:00
Log::debug('实名认证签名验证失败', $request);
2023-01-30 16:14:07 +00:00
2023-01-14 23:25:05 +00:00
return false;
}
if ($data['code'] !== 'PASS') {
return false;
}
2023-02-07 09:04:11 +00:00
$return = Cache::get('real_name:'.$data['bizNo'], false);
2023-01-15 12:59:20 +00:00
2023-02-07 09:04:11 +00:00
Cache::forget('real_name:'.$data['bizNo']);
2023-01-15 12:59:20 +00:00
return $return;
2023-01-14 23:25:05 +00:00
}
2023-01-14 21:37:25 +00:00
private function verifyIfSuccess(string $request, string $sign): bool
{
2023-01-30 16:14:07 +00:00
$public_key = <<<'EOF'
2023-01-14 21:37:25 +00:00
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWKKJoLwh6XEBkTeCfVbKSB3zkkycbIdd8SBabj2jpWynXx0pBZvdFpbb9AEiyrnM8bImhpz8YOXc2yUuN1ui/w==
-----END PUBLIC KEY-----
EOF;
$sign = base64_decode($sign);
$public_key = openssl_pkey_get_public($public_key);
2023-02-07 09:04:11 +00:00
if (! $public_key) {
2023-01-14 21:37:25 +00:00
abort(500, '公钥错误');
}
$flag = openssl_verify($request, $sign, $public_key, OPENSSL_ALGO_SHA256);
return $flag === 1;
}
2023-01-15 01:25:34 +00:00
public function getBirthday(string $id_card): string
{
$year = substr($id_card, 6, 4);
$month = substr($id_card, 10, 2);
$day = substr($id_card, 12, 2);
2023-02-07 09:04:11 +00:00
return $year.'-'.$month.'-'.$day;
2023-01-15 01:25:34 +00:00
}
2023-01-14 21:37:25 +00:00
}