初始化提交
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Extend\VerifyDun;
|
||||
|
||||
use App\Utils\HttpRequest;
|
||||
use GuzzleHttp\Client;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* 基础类
|
||||
*/
|
||||
class Base
|
||||
{
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected mixed $httpRuest;
|
||||
protected array $config;// 系统配置
|
||||
|
||||
protected string $api_url = "https://verify.dun.163.com/";
|
||||
|
||||
public string $version = "v1";
|
||||
|
||||
protected array $options;
|
||||
|
||||
protected array $params;// 请求参数
|
||||
|
||||
public function __construct(){
|
||||
$this->config = config("verify_dun");
|
||||
|
||||
$this->options['headers'] = [
|
||||
"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"
|
||||
];
|
||||
|
||||
$this->params['secretId'] = $this->config['secretId'];
|
||||
$this->params['version'] = $this->version;
|
||||
$this->params['timestamp'] = time() * 1000;
|
||||
$this->params['nonce'] = sprintf("%d", rand());
|
||||
$this->params = $this->toUtf8($this->params);
|
||||
|
||||
|
||||
|
||||
$container = ApplicationContext::getContainer();
|
||||
$this->httpRuest = $container->get(HttpRequest::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算签名
|
||||
* @return string
|
||||
*/
|
||||
protected function gen_signature(): string
|
||||
{
|
||||
ksort($this->params);
|
||||
$buff="";
|
||||
foreach($this->params as $key=>$value){
|
||||
if($value !== null) {
|
||||
$buff .=$key;
|
||||
$buff .=$value;
|
||||
}
|
||||
}
|
||||
$buff .= $this->config['secretKey'];
|
||||
return md5($buff);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将输入数据的编码统一转换成utf8
|
||||
* @param array $params 输入的参数
|
||||
* @return array
|
||||
*/
|
||||
function toUtf8(array $params): array
|
||||
{
|
||||
$utf8s = array();
|
||||
foreach ($params as $key => $value) {
|
||||
$utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", 'auto') : $value;
|
||||
}
|
||||
return $utf8s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Extend\VerifyDun;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use App\Utils\HttpRequest;
|
||||
use App\Utils\Log;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
/**
|
||||
* 实证认证
|
||||
*/
|
||||
class IdCard extends Base
|
||||
{
|
||||
/**
|
||||
* 实证认证
|
||||
* @param array $params
|
||||
* @return string
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function checkIdCard(array $params): string
|
||||
{
|
||||
// 组合请求地址
|
||||
$api_url = $this->api_url . $this->version . '/idcard/check';
|
||||
|
||||
$this->params['businessId'] = "f7262b91aac1448a848d29c0800b109a";
|
||||
|
||||
$this->params = array_merge($this->params,$params);
|
||||
|
||||
// 获取签名
|
||||
$this->params['signature'] = $this->gen_signature();
|
||||
|
||||
$this->options["form_params"] = $this->params;
|
||||
|
||||
$result = $this->httpRuest->getRequest($api_url,$this->options);
|
||||
|
||||
if (empty($result)){
|
||||
return "身份证认证失败";
|
||||
}
|
||||
|
||||
if ($result['code'] != "200"){
|
||||
throw new BusinessException("姓名与身份证号不一致");
|
||||
}
|
||||
|
||||
if (empty($result['result'])){
|
||||
return "身份证认证失败";
|
||||
}
|
||||
|
||||
// 处理不通过情况
|
||||
if ($result['result']['status'] == 2){
|
||||
switch ($result['result']['reasonType']) {
|
||||
case 2:
|
||||
return "输入姓名和身份证号不一致";
|
||||
break;
|
||||
case 3:
|
||||
return "查无此身份证";
|
||||
break;
|
||||
case 4:
|
||||
return "身份证照片信息与输入信息不一致";
|
||||
break;
|
||||
|
||||
default:
|
||||
return "身份证认证失败";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理status为其他情况
|
||||
if ($result['result']['status'] != 1){
|
||||
return "身份证认证失败";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user