|
|
@@ -8,86 +8,141 @@
|
|
|
// +----------------------------------------------------------------------
|
|
|
// | Author: 萤火科技 <admin@yiovo.com>
|
|
|
// +----------------------------------------------------------------------
|
|
|
-declare (strict_types = 1);
|
|
|
+declare (strict_types=1);
|
|
|
|
|
|
namespace app\common\library\sms;
|
|
|
|
|
|
-use think\Exception;
|
|
|
+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
|
|
|
+ * Class Driver
|
|
|
* @package app\common\library\sms
|
|
|
*/
|
|
|
class Driver
|
|
|
{
|
|
|
- private $config; // 配置信息
|
|
|
- private $engine; // 当前短信服务引擎类
|
|
|
- private $engineName; // 当前短信引擎名称
|
|
|
+ use ErrorTrait;
|
|
|
|
|
|
- private $error = ''; // 错误信息
|
|
|
+ // 后台短信配置信息
|
|
|
+ private $smsConfig;
|
|
|
+
|
|
|
+ // 当前短信平台 (后台设置)
|
|
|
+ private $gateway;
|
|
|
|
|
|
/**
|
|
|
* 构造方法
|
|
|
* Driver constructor.
|
|
|
- * @param array $config
|
|
|
- * @throws Exception
|
|
|
+ * @param array $smsConfig
|
|
|
*/
|
|
|
- public function __construct(array $config)
|
|
|
+ public function __construct(array $smsConfig)
|
|
|
{
|
|
|
// 配置信息
|
|
|
- $this->config = $config;
|
|
|
- // 当前引擎名称
|
|
|
- $this->engineName = $this->config['default'];
|
|
|
- // 实例化当前存储引擎
|
|
|
- $this->engine = $this->getEngineClass();
|
|
|
+ $this->smsConfig = $smsConfig;
|
|
|
+ // 当前短信平台
|
|
|
+ $this->gateway = $this->smsConfig['default'];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送短信通知
|
|
|
- * @param string $sceneValue 场景值
|
|
|
- * @param array $templateParams 短信模板变量
|
|
|
- * @param array $sceneConfig 短信场景配置
|
|
|
- * @param bool $isTest
|
|
|
+ * @param string $acceptPhone 接收的手机号
|
|
|
+ * @param string $templateCode 短信模板ID
|
|
|
+ * @param array $templateParams 短信模板参数
|
|
|
* @return bool
|
|
|
+ * @throws BaseException
|
|
|
+ * @throws InvalidArgumentException
|
|
|
*/
|
|
|
- public function sendSms(string $sceneValue, array $templateParams, array $sceneConfig = [], bool $isTest = false)
|
|
|
+ public function sendSms(string $acceptPhone, string $templateCode, array $templateParams): bool
|
|
|
{
|
|
|
- // 短信场景配置
|
|
|
- $sceneConfig = array_merge($this->config['scene'][$sceneValue], $sceneConfig);
|
|
|
- if ($isTest == false && !$sceneConfig['isEnable']) {
|
|
|
- $this->error = '短信通知服务未开启,请在商户后台中设置';
|
|
|
- return false;
|
|
|
+ // 实例化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);
|
|
|
}
|
|
|
- // 执行发送短信
|
|
|
- if (!$this->engine->sendSms($sceneConfig, $templateParams)) {
|
|
|
- $this->error = $this->engine->getError();
|
|
|
- return false;
|
|
|
+ // 记录日志
|
|
|
+ helper::logInfo('发送短信', [
|
|
|
+ 'gateway' => $this->smsConfig['default'],
|
|
|
+ 'acceptPhone' => $acceptPhone,
|
|
|
+ 'templateCode' => $templateCode,
|
|
|
+ 'templateParams' => $templateParams,
|
|
|
+ 'sendStatus' => $sendStatus,
|
|
|
+ ]);
|
|
|
+ // 存在异常时抛错
|
|
|
+ $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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取错误信息
|
|
|
- * @return mixed
|
|
|
+ * 短信发送异常的错误处理
|
|
|
+ * @param NoGatewayAvailableException $e
|
|
|
*/
|
|
|
- public function getError()
|
|
|
+ private function exceptionHandle(NoGatewayAvailableException $e)
|
|
|
{
|
|
|
- return $this->error;
|
|
|
+ // 短信发送失败
|
|
|
+ $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();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前的存储引擎
|
|
|
- * @return mixed
|
|
|
- * @throws Exception
|
|
|
+ * 生成短信模板数据
|
|
|
+ * @param array $templateParams
|
|
|
+ * @return array
|
|
|
*/
|
|
|
- private function getEngineClass()
|
|
|
+ private function getSmsTemplateData(array $templateParams): array
|
|
|
{
|
|
|
- $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($this->engineName);
|
|
|
- if (!class_exists($classSpace)) {
|
|
|
- throwError('未找到存储引擎类: ' . $this->engineName);
|
|
|
+ if ($this->gateway === 'qcloud') {
|
|
|
+ $templateParams = array_values($templateParams);
|
|
|
}
|
|
|
- return new $classSpace($this->config['engine'][$this->engineName]);
|
|
|
+ return $templateParams;
|
|
|
}
|
|
|
-
|
|
|
}
|