diff --git a/publish/sms.php b/publish/sms.php index a549376..3d81625 100644 --- a/publish/sms.php +++ b/publish/sms.php @@ -13,6 +13,7 @@ declare(strict_types=1); use zyimm\Sms\Drivers\AliyunDriver; use zyimm\Sms\Drivers\BaiduCloudDriver; +use Zyimm\Sms\Drivers\HogeDriver; use zyimm\Sms\Drivers\HuaweiCloudDriver; use zyimm\Sms\Drivers\JuheDataDriver; use zyimm\Sms\Drivers\LogDriver; @@ -37,7 +38,7 @@ return [ 'senders' => ['aliyun', 'tencent_cloud'], ], - 'senders' => [ + 'senders' => [ 'aliyun' => [ 'driver' => AliyunDriver::class, 'config' => [ @@ -181,13 +182,22 @@ return [ ], ], - 'log' => [ + 'log' => [ 'driver' => LogDriver::class, 'config' => [ 'name' => 'sms.local', 'group' => 'default', ], ], + 'hoge' => [ + 'driver' => HogeDriver::class, + 'config' => [ + 'sms_api_url' => '', + 'sms_appid' => '', + 'sms_appkey' => '', + 'sms_sign' => '' + ], + ] ], 'default_mobile_number_region' => null, ]; diff --git a/src/Contracts/SmsableInterface.php b/src/Contracts/SmsableInterface.php index 4dc7453..4828dfc 100644 --- a/src/Contracts/SmsableInterface.php +++ b/src/Contracts/SmsableInterface.php @@ -1,15 +1,11 @@ buildUrl(self::SEND_URL); + $data = [ + 'mobile' => $smsable->to->getNationalNumber() + ]; + $data['custom_appid'] = $this->config->get('sms_appid'); + $data['client_ip'] = $this->config->get('client_ip', '127.0.0.1'); + $data['sign_name'] = $this->config->get('sms_sign'); + $data['timestamp'] = time(); + $data['content'] = $smsable->content; + $string_temp = $this->getSignContent(Arr::except($data, + 'signature')).'&key='.$this->config->get('sms_appkey'); + $signature = strtoupper(hash_hmac('sha256', $string_temp, $this->config->get('sms_appkey'))); + $data['signature'] = $signature; + $url = $url.'?'.http_build_query($data); + $response = $this->client->post($url, $data); + $result = $response->toArray() ?? []; + if ($result['code'] != 200) { + throw new DriverErrorException($result['message'] ?? '未知错误', $result['code'], $response); + } + return []; + } + + protected function getSignContent($content): string + { + ksort($content); + $sign_data = []; + foreach ($content as $k => $v) { + if (is_array($v)) { + $v = json_encode($v); + } + $v = trim($v); + if (!empty($v)) { + $sign_data[] = $k.'='.$v; + } + } + return implode('&', $sign_data); + } + + /** + * buildUrl + * + * @param string $path + * @return string + */ + protected function buildUrl(string $path): string + { + return $this->config->get('sms_api_url').$path; + } +} \ No newline at end of file diff --git a/src/Drivers/YunxinDriver.php b/src/Drivers/YunxinDriver.php index 8a9a7c1..dc02db2 100755 --- a/src/Drivers/YunxinDriver.php +++ b/src/Drivers/YunxinDriver.php @@ -30,11 +30,9 @@ class YunxinDriver extends AbstractDriver switch ($action) { case 'sendCode': $params = $this->buildSendCodeParams($smsable); - break; case 'verifyCode': $params = $this->buildVerifyCodeParams($smsable); - break; default: throw new DriverErrorException(sprintf('action: %s not supported', $action), 0); diff --git a/src/Exceptions/DriverErrorException.php b/src/Exceptions/DriverErrorException.php index 22a5122..fa39e3c 100644 --- a/src/Exceptions/DriverErrorException.php +++ b/src/Exceptions/DriverErrorException.php @@ -11,9 +11,9 @@ use Throwable; class DriverErrorException extends RuntimeException { /** - * @var \Psr\Http\Message\ResponseInterface + * @var ResponseInterface */ - public $response; + public ResponseInterface $response; public function __construct(string $message, $code = null, ResponseInterface $response = null, Throwable $previous = null) { diff --git a/src/Response.php b/src/Response.php index 062f63c..81d9b92 100644 --- a/src/Response.php +++ b/src/Response.php @@ -18,7 +18,7 @@ use Psr\Http\Message\StreamInterface; class Response implements ResponseInterface, Arrayable { /** - * @var \Psr\Http\Message\ResponseInterface + * @var PsrResponseInterface */ private $response; diff --git a/src/Sender.php b/src/Sender.php index 669de2d..573b9b7 100644 --- a/src/Sender.php +++ b/src/Sender.php @@ -10,7 +10,9 @@ declare(strict_types=1); */ namespace Zyimm\Sms; -use Hyperf\Utils\Traits\Macroable; + +use Hyperf\Macroable\Macroable; +use Zyimm\Sms\Contracts\DriverInterface; use Zyimm\Sms\Contracts\SenderInterface; use Zyimm\Sms\Contracts\SmsableInterface; use Zyimm\Sms\Events\SmsMessageSending; @@ -28,7 +30,7 @@ class Sender implements SenderInterface protected $name; /** - * @var \Zyimm\Sms\Contracts\DriverInterface + * @var DriverInterface */ protected $driver; @@ -38,7 +40,7 @@ class Sender implements SenderInterface protected $container; /** - * @var \Psr\EventDispatcher\EventDispatcherInterface + * @var EventDispatcherInterface */ protected $eventDispatcher; diff --git a/src/Smsable.php b/src/Smsable.php index 6e71f51..b81773f 100644 --- a/src/Smsable.php +++ b/src/Smsable.php @@ -1,19 +1,15 @@ from = $from; return $this; } - public function to(MobileNumberInterface $to) + public function to(MobileNumberInterface $to): Smsable { $this->to = $to; return $this; } - public function content(string $content) + public function content(string $content): Smsable { $this->content = $content; return $this; } - public function template(string $template) + public function template(string $template): Smsable { $this->template = $template; return $this; } - public function signature(string $signature) + public function signature(string $signature): Smsable { $this->signature = $signature; return $this; } - public function with($key, $value = null) + public function with($key, $value = null): Smsable { if (is_array($key)) { $this->data = array_merge($this->data, $key); @@ -113,27 +109,31 @@ abstract class Smsable implements SmsableInterface, CompressInterface, UnCompres return $this; } - public function strategy(string $class) + public function strategy(string $class): Smsable { $this->strategy = $class; return $this; } - public function senders(array $names) + public function senders(array $names): Smsable { $this->senders = $names; return $this; } - public function sender(string $name) + public function sender(string $name): Smsable { $this->sender = $name; return $this; } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function send(?SenderInterface $sender = null): array { return $sender instanceof SenderInterface @@ -141,11 +141,21 @@ abstract class Smsable implements SmsableInterface, CompressInterface, UnCompres : ApplicationContext::getContainer()->get(SmsManagerInterface::class)->sendNow($this); } + /** + * queue + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function queue(?string $queue = null): bool { return $this->pushQueuedJob($this->newQueuedJob(), $queue); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function later(int $delay, ?string $queue = null): bool { return $this->pushQueuedJob($this->newQueuedJob(), $queue, $delay); @@ -181,8 +191,15 @@ abstract class Smsable implements SmsableInterface, CompressInterface, UnCompres /** * Push the queued SMS message job onto the queue. + * + * @param QueuedSmsableJob $job + * @param string|null $queue + * @param int|null $delay + * @return bool + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - protected function pushQueuedJob(QueuedSmsableJob $job, ?string $queue = null, ?int $delay = null) + protected function pushQueuedJob(QueuedSmsableJob $job, ?string $queue = null, ?int $delay = null): bool { $queue = $queue ?: (property_exists($this, 'queue') ? $this->queue : array_key_first(config('async_queue')));