新增驱动

This commit is contained in:
zhouyangyang 2022-04-08 19:49:16 +08:00
parent 15c0f9f44f
commit cca29a2e9e
8 changed files with 141 additions and 55 deletions

View File

@ -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;
@ -188,6 +189,15 @@ return [
'group' => 'default',
],
],
'hoge' => [
'driver' => HogeDriver::class,
'config' => [
'sms_api_url' => '',
'sms_appid' => '',
'sms_appkey' => '',
'sms_sign' => ''
],
]
],
'default_mobile_number_region' => null,
];

View File

@ -1,15 +1,11 @@
<?php
declare(strict_types=1);
/**
* This file is part of hyperf-ext/sms.
*
* @link https://github.com/hyperf-ext/sms
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/sms/blob/master/LICENSE
*/
namespace Zyimm\Sms\Contracts;
use Zyimm\Sms\Exceptions\DriverErrorException;
/**
* @property string[] $senders
* @property string $strategy
@ -27,35 +23,35 @@ interface SmsableInterface
*
* @return $this
*/
public function from(string $from);
public function from(string $from): SmsableInterface;
/**
* Set the SMS message recipient number.
*
* @return $this
*/
public function to(MobileNumberInterface $to);
public function to(MobileNumberInterface $to): SmsableInterface;
/**
* Set the SMS message content.
*
* @return $this
*/
public function content(string $content);
public function content(string $content): SmsableInterface;
/**
* Set the SMS message template.
*
* @return $this
*/
public function template(string $template);
public function template(string $template): SmsableInterface;
/**
* Set the SMS message signature.
*
* @return $this
*/
public function signature(string $signature);
public function signature(string $signature): SmsableInterface;
/**
* Set the SMS message data.
@ -65,14 +61,14 @@ interface SmsableInterface
*
* @return $this
*/
public function with($key, $value = null);
public function with($key, $value = null): SmsableInterface;
/**
* Set the strategy.
*
* @return $this
*/
public function strategy(string $class);
public function strategy(string $class): SmsableInterface;
/**
* Set the list of sender name of the SMS message.
@ -81,19 +77,19 @@ interface SmsableInterface
*
* @return $this
*/
public function senders(array $names);
public function senders(array $names): SmsableInterface;
/**
* Set the sender name of the SMS message. This will override `$senders` property value.
*
* @return $this
*/
public function sender(string $name);
public function sender(string $name): SmsableInterface;
/**
* Send the SMS message immediately.
*
* @throws \Zyimm\Sms\Exceptions\DriverErrorException
* @throws DriverErrorException
*/
public function send(?SenderInterface $sender = null): array;

View File

@ -0,0 +1,63 @@
<?php
namespace Zyimm\Sms\Drivers;
use Hyperf\Utils\Arr;
use Zyimm\Sms\Contracts\SmsableInterface;
use Zyimm\Sms\Exceptions\DriverErrorException;
class HogeDriver extends AbstractDriver
{
const SEND_URL = '/api/sms/send';
public function send(SmsableInterface $smsable): array
{
$url = $this->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;
}
}

View File

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

View File

@ -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)
{

View File

@ -18,7 +18,7 @@ use Psr\Http\Message\StreamInterface;
class Response implements ResponseInterface, Arrayable
{
/**
* @var \Psr\Http\Message\ResponseInterface
* @var PsrResponseInterface
*/
private $response;

View File

@ -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;

View File

@ -1,19 +1,15 @@
<?php
declare(strict_types=1);
/**
* This file is part of hyperf-ext/sms.
*
* @link https://github.com/hyperf-ext/sms
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/sms/blob/master/LICENSE
*/
namespace Zyimm\Sms\;
namespace Zyimm\Sms;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Contract\CompressInterface;
use Hyperf\Contract\UnCompressInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zyimm\Sms\Contracts\MobileNumberInterface;
use Zyimm\Sms\Contracts\SenderInterface;
use Zyimm\Sms\Contracts\SmsableInterface;
@ -25,84 +21,84 @@ abstract class Smsable implements SmsableInterface, CompressInterface, UnCompres
/**
* @var string
*/
public $strategy = OrderStrategy::class;
public string $strategy = OrderStrategy::class;
/**
* @var string[]
*/
public $senders;
public array $senders;
/**
* @var string
*/
public $sender;
public string $sender;
/**
* @var string
*/
public $from;
public string $from;
/**
* @var \Zyimm\Sms\Contracts\MobileNumberInterface
* @var MobileNumberInterface
*/
public $to;
public MobileNumberInterface $to;
/**
* @var string
*/
public $content;
public string $content;
/**
* @var string
*/
public $template;
public string $template;
/**
* @var string
*/
public $signature;
public string $signature;
/**
* @var array
*/
public $data = [];
public array $data = [];
public function from(string $from)
public function from(string $from): Smsable
{
$this->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')));