You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.4 KiB
PHP
96 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Helper\SmsSender;
|
|
|
|
use App\Contract\SmsSender;
|
|
use App\Exception\BusinessException;
|
|
use App\Model\Tool;
|
|
use GuzzleHttp\Client;
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
use GuzzleHttp\HandlerStack;
|
|
|
|
class XgSmsSender implements SmsSender
|
|
{
|
|
|
|
public $channel = 'xigu';
|
|
|
|
protected $baseUrl = 'http://api.vlpush.com';
|
|
protected $appId;
|
|
protected $apiKey;
|
|
protected $templateId;
|
|
protected $client;
|
|
|
|
public function __construct(Tool $tool)
|
|
{
|
|
$this->appId = $tool->get('smtp');
|
|
$this->apiKey = $tool->get('smtp_account');
|
|
$this->templateId = $tool->get('smtp_password');
|
|
$this->initClient();
|
|
}
|
|
|
|
public function sendCode($mobile, $code, $activeMinute = 10)
|
|
{
|
|
$data = [
|
|
'appid' => $this->appId,
|
|
'apikey' => $this->apiKey,
|
|
'templateid' => $this->templateId,
|
|
'phone' => $mobile,
|
|
'param' => $code . ',' . $activeMinute,
|
|
];
|
|
|
|
$result = $this->post('/sms/send_sms', $this->parseData($data));
|
|
$this->parseResult($result);
|
|
}
|
|
|
|
public function send($mobile, $content)
|
|
{
|
|
throw new BusinessException('该短信服务不可用');
|
|
}
|
|
|
|
public function sendBatch(array $mobiles, $content)
|
|
{
|
|
throw new BusinessException('该短信服务不可用');
|
|
}
|
|
|
|
protected function parseData($data)
|
|
{
|
|
return http_build_query($data);
|
|
}
|
|
|
|
protected function parseResult($result)
|
|
{
|
|
$result = json_decode($result, true);
|
|
if (!$result) {
|
|
throw new BusinessException('系统异常');
|
|
}
|
|
if ($result['code'] != 200) {
|
|
throw new BusinessException($result['msg']);
|
|
}
|
|
}
|
|
|
|
protected function post($uri, $data)
|
|
{
|
|
$response = $this->client->post($uri, [
|
|
'verify' => false,
|
|
'body' => $data
|
|
]);
|
|
return (string)$response->getBody();
|
|
}
|
|
|
|
protected function initClient()
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => $this->baseUrl,
|
|
'handler' => HandlerStack::create(new CoroutineHandler()),
|
|
'headers' => [
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
],
|
|
'swoole' => [
|
|
'timeout' => 10,
|
|
'socket_buffer_size' => 1024 * 1024 * 2,
|
|
],
|
|
]);
|
|
}
|
|
} |