初始化提交

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));
}
}