初始化提交

This commit is contained in:
2023-02-17 17:10:16 +08:00
commit 4ca844f470
152 changed files with 20390 additions and 0 deletions
+127
View File
@@ -0,0 +1,127 @@
<?php
namespace Extend\Alibaba;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\CodeLog;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config as OpenApiConfig;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
/**
* 阿里大鱼-短信
*/
class Dysms
{
/**
* 创建客户端
* @param string $accessKeyId
* @param string $accessKeySecret
* @return OpenApiClient Client
*/
public static function createClient(string $accessKeyId, string $accessKeySecret): OpenApiClient
{
$config = new OpenApiConfig([
// 必填,您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 必填,您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// 访问的域名
$config->endpoint = "dysmsapi.aliyuncs.com";
return new OpenApiClient($config);
}
/**
* API 相关
* @return Params OpenApi.Params
*/
public static function createApiInfo(): Params
{
$params = new Params([
// 接口名称
"action" => "SendSms",
// 接口版本
"version" => "2017-05-25",
// 接口协议
"protocol" => "HTTPS",
// 接口 HTTP 方法
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
// 接口 PATH
"pathname" => "/",
// 接口请求体内容格式
"reqBodyType" => "json",
// 接口响应体内容格式
"bodyType" => "json"
]);
return $params;
}
/**
* 发送短信
* @param string $phone_numbers 手机号
* @param array $template_param 参数
* @param string $template_code 短信模版编码
* @param int $scene 场景
*/
public static function sendSms(string $phone_numbers,array $template_param,string $template_code,int $scene): void
{
$config = config("alibaba.dysms");
$client = self::createClient($config['accessKey'], $config['accessKeySecret']);
$params = self::createApiInfo();
// query params
$queries = [];
$queries["PhoneNumbers"] = $phone_numbers;
$queries["SignName"] = "肝胆相照";
$queries["TemplateCode"] = $template_code;
$queries["TemplateParam"] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
// runtime options
$runtime = new RuntimeOptions([]);
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode
$result = $client->callApi($params, $request, $runtime);
if (empty($result)){
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
}
if (empty($result['body'])){
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
}
if ($result['body']['Code'] != "OK"){
throw new BusinessException($result['body']['Message'],HttpEnumCode::CODE_FAIL);
}
if (empty($result['body']['RequestId'])){
// 无唯一值
throw new BusinessException($result['body']['Message'],HttpEnumCode::CODE_FAIL);
}
// 记录log
$data = array();
$data['type'] = 1;
$data['status'] = 1;
$data['scene'] = $scene;
$data['phone'] = $phone_numbers;
$data['code'] = $template_code;
$data['third_code'] = $result['body']['RequestId'];
$data['remarks'] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
$res = CodeLog::addCodeLog($data);
if (empty($res)){
// 发送成功,记录失败
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
}
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
namespace Extend\Alibaba;
use App\Exception\BusinessException;
use DateTime;
use DateTimeInterface;
use OSS\Core\OssException;
use OSS\OssClient;
/**
* 阿里云oss
*/
class Oss
{
protected array $config;// 系统配置
public function __construct()
{
$this->config = config("alibaba.oss");
}
/**
* 创建客户端
* @return OssClient
*/
public function createClient(): OssClient
{
try {
return new OssClient($this->config['accessKey'], $this->config['accessKeySecret'], $this->config['endpoint']);
} catch (OssException $e) {
throw new BusinessException($e->getMessage());
}
}
/**
* 获取签名
* @param string $dir 用户上传文件时指定的前缀。
* @return array
*/
public function signature(string $dir): array
{
try {
$now = time();
$expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
$start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
$conditions[] = $start;
$arr = array('expiration' => $expiration, 'conditions' => $conditions);
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->config['accessKeySecret'], true));
$response = array();
$response['accessid'] = $this->config['accessKey'];
$response['host'] = $this->config['bucket'] . '.' . $this->config['endpoint'];
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
$response['callback'] = "";
$response['dir'] = $dir; // 这个参数是设置用户上传文件时指定的前缀。
return $response;
} catch (\Exception $e) {
throw new BusinessException($e->getMessage());
}
}
/**
* @param $time
* @return string
* @throws \Exception
*/
protected function gmt_iso8601($time): string
{
return str_replace('+00:00', '.000Z', gmdate('c', $time));
}
}
+81
View File
@@ -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;
}
}
+76
View File
@@ -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 "";
}
}