forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 添加融云支持 * fix CS. * fix CS. * code review. * add comment. * code review. * fix CS.
- Loading branch information
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/easy-sms. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Overtrue\EasySms\Gateways; | ||
|
||
use GuzzleHttp\Exception\ClientException; | ||
use Overtrue\EasySms\Contracts\MessageInterface; | ||
use Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Traits\HasHttpRequest; | ||
|
||
/** | ||
* Class RongcloudGateway. | ||
* | ||
* @author Darren Gao <[email protected]> | ||
* | ||
* @see http://www.rongcloud.cn/docs/sms_service.html#send_sms_code | ||
*/ | ||
class RongcloudGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_TEMPLATE = 'http://api.sms.ronghub.com/%s.%s'; | ||
|
||
const ENDPOINT_ACTION = 'sendCode'; | ||
|
||
const ENDPOINT_FORMAT = 'json'; | ||
|
||
const ENDPOINT_REGION = '86'; // 中国区,目前只支持此国别 | ||
|
||
const SUCCESS_CODE = 200; | ||
|
||
/** | ||
* @param array|int|string $to | ||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message | ||
* @param \Overtrue\EasySms\Support\Config $config | ||
* | ||
* @return array | ||
* | ||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
*/ | ||
public function send($to, MessageInterface $message, Config $config) | ||
{ | ||
$data = $message->getData(); | ||
|
||
if (array_key_exists('action', $data)) { | ||
$action = $data['action']; | ||
} else { | ||
$action = self::ENDPOINT_ACTION; | ||
} | ||
$endpoint = $this->buildEndpoint($action); | ||
|
||
$headers = [ | ||
'Nonce' => uniqid(), | ||
'App-Key' => $config->get('app_key'), | ||
'Timestamp' => time(), | ||
]; | ||
$headers['Signature'] = $$this->generateSign($headers, $config); | ||
|
||
switch ($action) { | ||
case 'sendCode': | ||
$params = [ | ||
'mobile' => $to, | ||
'region' => self::ENDPOINT_REGION, | ||
'templateId' => $message->getTemplate($this), | ||
]; | ||
|
||
break; | ||
case 'verifyCode': | ||
if (!array_key_exists('code', $data) | ||
or !array_key_exists('sessionId', $data)) { | ||
throw new GatewayErrorException('"code" or "sessionId" is not set', 0); | ||
} | ||
$params = [ | ||
'code' => $data['code'], | ||
'sessionId' => $data['sessionId'], | ||
]; | ||
|
||
break; | ||
default: | ||
throw new GatewayErrorException(sprintf('action: %s not supported', $action)); | ||
} | ||
|
||
try { | ||
$result = $this->post($endpoint, $params, $headers); | ||
|
||
if (self::SUCCESS_CODE !== $result['code']) { | ||
throw new GatewayErrorException($result['errorMessage'], $result['code'], $result); | ||
} | ||
} catch (ClientException $e) { | ||
return new GatewayErrorException($e->getMessage(), $e->getCode()); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Generate Sign. | ||
* | ||
* @param array $params | ||
* @param \Overtrue\EasySms\Support\Config $config | ||
* | ||
* @return string | ||
*/ | ||
protected function generateSign($params, Config $config) | ||
{ | ||
return sha1(sprintf('%s%s%s', $config->get('app_secret'), $params['Nonce'], $params['Timestamp'])); | ||
} | ||
|
||
/** | ||
* Build endpoint url. | ||
* | ||
* @param string $action | ||
* | ||
* @return string | ||
*/ | ||
protected function buildEndpoint($action) | ||
{ | ||
return sprintf(self::ENDPOINT_TEMPLATE, $action, self::ENDPOINT_FORMAT); | ||
} | ||
} |