初始化项目
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\library\sms;
|
||||
|
||||
use Overtrue\EasySms\Strategies\OrderStrategy;
|
||||
|
||||
/**
|
||||
* EasySms配置类
|
||||
* Class Config
|
||||
* @package app\common\library\sms
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* 生成EasySms的配置项
|
||||
* @param array $smsConfig
|
||||
* @return array
|
||||
*/
|
||||
public static function getEasySmsConfig(array $smsConfig): array
|
||||
{
|
||||
return [
|
||||
// HTTP 请求的超时时间(秒)
|
||||
'timeout' => 5.0,
|
||||
|
||||
// 默认发送配置
|
||||
'default' => [
|
||||
// 网关调用策略,默认:顺序调用
|
||||
'strategy' => OrderStrategy::class,
|
||||
|
||||
// 默认可用的发送网关
|
||||
'gateways' => [$smsConfig['default']],
|
||||
],
|
||||
// 可用的网关配置
|
||||
'gateways' => [
|
||||
'aliyun' => [
|
||||
'access_key_id' => $smsConfig['engine']['aliyun']['AccessKeyId'],
|
||||
'access_key_secret' => $smsConfig['engine']['aliyun']['AccessKeySecret'],
|
||||
'sign_name' => $smsConfig['engine']['aliyun']['sign'],
|
||||
],
|
||||
'qcloud' => [
|
||||
'sdk_app_id' => $smsConfig['engine']['qcloud']['SdkAppID'],
|
||||
'secret_id' => $smsConfig['engine']['qcloud']['AccessKeyId'],
|
||||
'secret_key' => $smsConfig['engine']['qcloud']['AccessKeySecret'],
|
||||
'sign_name' => $smsConfig['engine']['qcloud']['sign'],
|
||||
],
|
||||
'qiniu' => [
|
||||
'access_key' => $smsConfig['engine']['qiniu']['AccessKey'],
|
||||
'secret_key' => $smsConfig['engine']['qiniu']['SecretKey'],
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\sms;
|
||||
|
||||
use app\common\library\helper;
|
||||
use cores\traits\ErrorTrait;
|
||||
use cores\exception\BaseException;
|
||||
use Overtrue\EasySms\EasySms;
|
||||
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
|
||||
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
|
||||
|
||||
/**
|
||||
* 短信通知模块驱动
|
||||
* Class Driver
|
||||
* @package app\common\library\sms
|
||||
*/
|
||||
class Driver
|
||||
{
|
||||
use ErrorTrait;
|
||||
|
||||
// 后台短信配置信息
|
||||
private $smsConfig;
|
||||
|
||||
// 当前短信平台 (后台设置)
|
||||
private $gateway;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Driver constructor.
|
||||
* @param array $smsConfig
|
||||
*/
|
||||
public function __construct(array $smsConfig)
|
||||
{
|
||||
// 配置信息
|
||||
$this->smsConfig = $smsConfig;
|
||||
// 当前短信平台
|
||||
$this->gateway = $this->smsConfig['default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信通知
|
||||
* @param string $acceptPhone 接收的手机号
|
||||
* @param string $templateCode 短信模板ID
|
||||
* @param array $templateParams 短信模板参数
|
||||
* @return bool
|
||||
* @throws BaseException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function sendSms(string $acceptPhone, string $templateCode, array $templateParams): bool
|
||||
{
|
||||
// 实例化EasySms
|
||||
$easySmsConfig = Config::getEasySmsConfig($this->smsConfig);
|
||||
$easySms = new EasySms($easySmsConfig);
|
||||
try {
|
||||
// 执行发送短信
|
||||
$result = $easySms->send($acceptPhone, [
|
||||
'template' => $templateCode,
|
||||
'data' => $this->getSmsTemplateData($templateParams),
|
||||
]);
|
||||
// 短信发送成功的错误处理
|
||||
$sendStatus = $this->resultHandle($result);
|
||||
} catch (NoGatewayAvailableException $e) {
|
||||
// 短信发送异常的错误处理
|
||||
$sendStatus = false;
|
||||
$this->exceptionHandle($e);
|
||||
}
|
||||
// 记录日志
|
||||
helper::logInfo('发送短信', [
|
||||
'gateway' => $this->smsConfig['default'],
|
||||
'acceptPhone' => $acceptPhone,
|
||||
'templateCode' => $templateCode,
|
||||
'templateParams' => $templateParams,
|
||||
'sendStatus' => $sendStatus ? 'true' : 'false',
|
||||
'sendErrMsg' => $this->getError(),
|
||||
]);
|
||||
// 存在异常时抛错
|
||||
$sendStatus === false && throwError($this->getError());
|
||||
return $sendStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信发送成功的错误处理
|
||||
* @param array $result
|
||||
* @return bool
|
||||
*/
|
||||
private function resultHandle(array $result): bool
|
||||
{
|
||||
// 腾讯云短信错误: 模板ID不正确
|
||||
if ($this->gateway === 'qcloud') {
|
||||
$response = $result[$this->gateway]['result']['Response']['SendStatusSet'];
|
||||
if (isset($response[0]) && isset($response[0]['Code']) && $response[0]['Code'] !== 'Ok') {
|
||||
$this->error = '请检查后台短信平台参数和模板ID是否正确';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信发送异常的错误处理
|
||||
* @param NoGatewayAvailableException $e
|
||||
*/
|
||||
private function exceptionHandle(NoGatewayAvailableException $e)
|
||||
{
|
||||
// 短信发送失败
|
||||
$err = $e->getLastException();
|
||||
if ($err instanceof \GuzzleHttp\Exception\ClientException) {
|
||||
$body = (string)$err->getResponse()->getBody();
|
||||
$result = helper::jsonDecode($body);
|
||||
if (isset($result['Message'])) {
|
||||
// $errMsg = $result['Message'];
|
||||
$this->error = '请检查后台短信平台的参数设置';
|
||||
return;
|
||||
}
|
||||
if (isset($result['message'])) {
|
||||
$this->error = $result['message'];
|
||||
return;
|
||||
}
|
||||
}
|
||||
// if ($err instanceof \Overtrue\EasySms\Exceptions\GatewayErrorException) {
|
||||
// $this->error = $err->getMessage();
|
||||
// return;
|
||||
// }
|
||||
$this->error = $err->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成短信模板数据
|
||||
* @param array $templateParams
|
||||
* @return array
|
||||
*/
|
||||
private function getSmsTemplateData(array $templateParams): array
|
||||
{
|
||||
if ($this->gateway === 'qcloud') {
|
||||
$templateParams = array_values($templateParams);
|
||||
}
|
||||
return $templateParams;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\sms\engine;
|
||||
|
||||
use app\common\library\helper;
|
||||
use app\common\library\sms\package\aliyun\SignatureHelper;
|
||||
|
||||
/**
|
||||
* 阿里云短信模块引擎
|
||||
* Class Aliyun
|
||||
* @package app\common\library\sms\engine
|
||||
*/
|
||||
class Aliyun extends Server
|
||||
{
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qiniu constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信通知
|
||||
* @param array $sceneConfig 场景配置
|
||||
* @param array $templateParams 短信模板参数
|
||||
* @return bool
|
||||
*/
|
||||
public function sendSms(array $sceneConfig, array $templateParams)
|
||||
{
|
||||
$params = [];
|
||||
// *** 需用户填写部分 ***
|
||||
|
||||
// 必填: 短信接收号码
|
||||
$params["PhoneNumbers"] = $sceneConfig['acceptPhone'];
|
||||
|
||||
// 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
|
||||
$params["SignName"] = $this->config['sign'];
|
||||
|
||||
// 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
|
||||
$params["TemplateCode"] = $sceneConfig['templateCode'];
|
||||
|
||||
// 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
|
||||
$params['TemplateParam'] = $templateParams;
|
||||
|
||||
// 可选: 设置发送短信流水号
|
||||
// $params['OutId'] = "12345";
|
||||
|
||||
// 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
|
||||
// $params['SmsUpExtendCode'] = "1234567";
|
||||
|
||||
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
|
||||
if (!empty($params['TemplateParam'])) {
|
||||
$params['TemplateParam'] = helper::jsonEncode($params['TemplateParam']);
|
||||
}
|
||||
|
||||
// 初始化SignatureHelper实例用于设置参数,签名以及发送请求
|
||||
$helper = new SignatureHelper;
|
||||
|
||||
// 此处可能会抛出异常,注意catch
|
||||
$response = $helper->request(
|
||||
$this->config['AccessKeyId']
|
||||
, $this->config['AccessKeySecret']
|
||||
, "dysmsapi.aliyuncs.com"
|
||||
, array_merge($params, [
|
||||
"RegionId" => "cn-hangzhou",
|
||||
"Action" => "SendSms",
|
||||
"Version" => "2017-05-25",
|
||||
])
|
||||
// 选填: 启用https
|
||||
, true
|
||||
);
|
||||
// 记录日志
|
||||
log_record([
|
||||
'name' => '发送短信',
|
||||
'config' => $this->config,
|
||||
'params' => $params
|
||||
]);
|
||||
log_record($response);
|
||||
$this->error = $response->Message;
|
||||
return $response->Code === 'OK';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\common\library\sms\engine;
|
||||
|
||||
|
||||
abstract class Server
|
||||
{
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library\sms\package\aliyun;
|
||||
|
||||
/**
|
||||
* 签名助手 2017/11/19
|
||||
*
|
||||
* Class SignatureHelper
|
||||
*/
|
||||
class SignatureHelper
|
||||
{
|
||||
/**
|
||||
* 生成签名并发起请求
|
||||
*
|
||||
* @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
|
||||
* @param $accessKeySecret string AccessKeySecret
|
||||
* @param $domain string API接口所在域名
|
||||
* @param $params array API具体参数
|
||||
* @param $security boolean 使用https
|
||||
* @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
|
||||
*/
|
||||
public function request($accessKeyId, $accessKeySecret, $domain, $params, $security = false)
|
||||
{
|
||||
$apiParams = array_merge(array(
|
||||
"SignatureMethod" => "HMAC-SHA1",
|
||||
"SignatureNonce" => uniqid(mt_rand(0, 0xffff), true),
|
||||
"SignatureVersion" => "1.0",
|
||||
"AccessKeyId" => $accessKeyId,
|
||||
"Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
|
||||
"Format" => "JSON",
|
||||
), $params);
|
||||
ksort($apiParams);
|
||||
|
||||
$sortedQueryStringTmp = "";
|
||||
foreach ($apiParams as $key => $value) {
|
||||
$sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
|
||||
}
|
||||
|
||||
$stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
|
||||
|
||||
$sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&", true));
|
||||
|
||||
$signature = $this->encode($sign);
|
||||
|
||||
$url = ($security ? 'https' : 'http') . "://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
|
||||
|
||||
try {
|
||||
$content = $this->fetchContent($url);
|
||||
return json_decode($content);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function encode($str)
|
||||
{
|
||||
$res = urlencode($str);
|
||||
$res = preg_replace("/\+/", "%20", $res);
|
||||
$res = preg_replace("/\*/", "%2A", $res);
|
||||
$res = preg_replace("/%7E/", "~", $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
private function fetchContent($url)
|
||||
{
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
"x-sdk-client" => "php/2.0.0"
|
||||
));
|
||||
|
||||
if (substr($url, 0, 5) == 'https') {
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
|
||||
$rtn = curl_exec($ch);
|
||||
|
||||
if ($rtn === false) {
|
||||
trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user