Files
water_new/modules/User/Services/VerificationCode.php
2023-03-08 09:16:04 +08:00

92 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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,
];
}
}