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.
94 lines
2.3 KiB
PHP
94 lines
2.3 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 JhSmsSender implements SmsSender
|
|
{
|
|
|
|
public $channel = 'juhe';
|
|
|
|
protected $baseUrl = 'http://v.juhe.cn';
|
|
protected $key;
|
|
protected $tplId;
|
|
protected $client;
|
|
|
|
public function __construct(Tool $tool)
|
|
{
|
|
$this->key = $tool->get('key');
|
|
$this->tplId = $tool->get('tpl_id');
|
|
$this->initClient();
|
|
}
|
|
|
|
public function sendCode($mobile, $code, $activeMinute = 10)
|
|
{
|
|
$tplValue = urlencode('#code#=' . $code . '&#m#=' . $activeMinute);
|
|
$data = [
|
|
'mobile' => $mobile,
|
|
'tpl_id' => $this->tplId,
|
|
'tpl_value' => $tplValue,
|
|
'key' => $this->key,
|
|
];
|
|
|
|
$result = $this->post('/sms/send', $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['error_code'] == 0) {
|
|
throw new BusinessException($result['reason']);
|
|
}
|
|
}
|
|
|
|
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,
|
|
],
|
|
]);
|
|
}
|
|
} |