You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
<?php
|
|
|
|
namespace app\sms;
|
|
|
|
use app\sms\contracts\SmsDriverInterface;
|
|
|
|
class Sms
|
|
{
|
|
protected $driver;
|
|
/**
|
|
* Sms constructor.
|
|
* @param string|null $channel
|
|
* @throws \Exception
|
|
*/
|
|
public function __construct(?string $channel = null)
|
|
{
|
|
$channel = $channel ?: config('sms.default');
|
|
$drivers = config('sms.channels');
|
|
|
|
if (!isset($drivers[$channel])) {
|
|
throw new \Exception("短信通道 [$channel] 未配置");
|
|
}
|
|
|
|
$class = $drivers[$channel]['driver'];
|
|
$config = $drivers[$channel]['config'];
|
|
|
|
if (!class_exists($class)) {
|
|
throw new \Exception("短信驱动类 [$class] 不存在");
|
|
}
|
|
|
|
$this->driver = new $class($config);
|
|
|
|
if (!($this->driver instanceof SmsDriverInterface)) {
|
|
throw new \Exception("[$class] 必须实现 SmsDriverInterface 接口");
|
|
}
|
|
}
|
|
|
|
public function send(string $mobile, string $templateId, array $data = []): array
|
|
{
|
|
return $this->driver->send($mobile, $templateId, $data);
|
|
}
|
|
}
|
|
|