92 lines
2.1 KiB
PHP
92 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace Modules\User\Services;
|
||
|
||
use Exception;
|
||
use Overtrue\EasySms\Contracts\GatewayInterface;
|
||
use Overtrue\EasySms\Message;
|
||
|
||
class VerificationCode extends Message
|
||
{
|
||
|
||
/**
|
||
* 验证码
|
||
*
|
||
* @var string
|
||
*/
|
||
public string $code;
|
||
|
||
/**
|
||
* @var array
|
||
*/
|
||
protected array $config;
|
||
|
||
/**
|
||
* @var string
|
||
*/
|
||
protected string $channel;
|
||
|
||
public function __construct(array $config, string $channel)
|
||
{
|
||
$this->config = $config;
|
||
$this->channel = $channel;
|
||
|
||
if ($config['debug']) {
|
||
$this->code = $config['debug_code'];
|
||
} else {
|
||
$this->code = sprintf("%0".$config['length']."d", mt_rand(1, pow(10, $config['length']) - 1));
|
||
}
|
||
|
||
parent::__construct();
|
||
}
|
||
|
||
/**
|
||
* Notes : 定义直接使用内容发送平台的内容
|
||
*
|
||
* @Date : 2021/5/27 9:09 上午
|
||
* @Author : <Jason.C>
|
||
* @param GatewayInterface|null $gateway
|
||
* @return string
|
||
*/
|
||
public function getContent(GatewayInterface $gateway = null): string
|
||
{
|
||
return sprintf('验证码%s,您正在登录,若非本人操作,请勿泄露。', $this->code);
|
||
}
|
||
|
||
/**
|
||
* Notes : 定义使用模板发送方式平台所需要的模板 ID
|
||
*
|
||
* @Date : 2021/5/27 9:10 上午
|
||
* @Author : <Jason.C>
|
||
* @param GatewayInterface|null $gateway
|
||
* @return string
|
||
* @throws Exception
|
||
*/
|
||
public function getTemplate(GatewayInterface $gateway = null): string
|
||
{
|
||
$templateId = $this->config['template'][$this->channel] ?? '';
|
||
|
||
if (! $templateId) {
|
||
throw new Exception('不合法的验证通道:'.$this->channel);
|
||
}
|
||
|
||
return $templateId;
|
||
}
|
||
|
||
/**
|
||
* Notes : 模板参数
|
||
*
|
||
* @Date : 2021/5/27 9:10 上午
|
||
* @Author : <Jason.C>
|
||
* @param GatewayInterface|null $gateway
|
||
* @return string[]
|
||
*/
|
||
public function getData(GatewayInterface $gateway = null): array
|
||
{
|
||
return [
|
||
'code' => $this->code,
|
||
];
|
||
}
|
||
|
||
}
|