Skip to content

Commit

Permalink
创蓝短信支持验证码及营销短信的不同渠道的选择配置;更改数据提交方式为post (overtrue#111)
Browse files Browse the repository at this point in the history
* 创蓝短信支持验证码及营销短信的不同渠道的选择配置;更改数据提交方式为post

* 代码格式化;修改部分函数comment中返回值类型的说明

* 代码格式调整

* 代码格式调整

* 代码格式调整

* 修复\Overtrue\EasySms\Gateways\ChuanglanGateway.php 中的bug

* 添加创蓝单元测试代码

* 提交创蓝的数据以JSON格式提交

* 代码风格调整;phpunit版本修改

* 代码风格调整
  • Loading branch information
Bostin.wang authored and overtrue committed Jun 26, 2018
1 parent 1c08e5a commit 7e31817
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 19 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,16 @@ $easySms->send(13188888888, $message);

```php
'chuanglan' => [
'username' => '',
'account' => '',
'password' => '',

// \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE => 验证码通道(默认)
// \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_PROMOTION_CODE => 会员营销通道
'channel' => \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE,

// 会员营销通道 特定参数。创蓝规定:api提交营销短信的时候,需要自己加短信的签名及退订信息
'sign' => '【通讯云】',
'unsubscribe' => '回TD退订',
],
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"phpunit/phpunit": "^5.6",
"mockery/mockery": "1.0.x-dev"
},
"autoload": {
Expand Down
96 changes: 79 additions & 17 deletions src/Gateways/ChuanglanGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;
Expand All @@ -26,46 +27,107 @@ class ChuanglanGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'https://sms.253.com/msg/send/json';
/**
* URL模板
*/
const ENDPOINT_URL_TEMPLATE = 'https://%s.253.com/msg/send/json';

/**
* 验证码渠道code.
*/
const CHANNEL_VALIDATE_CODE = 'smsbj1';

/**
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param \Overtrue\EasySms\Support\Config $config
* 会员营销渠道code.
*/
const CHANNEL_PROMOTION_CODE = 'smssh1';

/**
* @param PhoneNumberInterface $to
* @param MessageInterface $message
* @param Config $config
*
* @return array
*
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
* @throws GatewayErrorException
* @throws InvalidArgumentException
*/
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$params = [
'username' => $config->get('username'),
'account' => $config->get('account'),
'password' => $config->get('password'),
'phone' => $to->getNumber(),
'msg' => $message->getContent($this),
'msg' => $this->wrapChannelContent($message->getContent($this), $config),
];

$result = $this->get(self::ENDPOINT_URL, $params);

$formatResult = $this->formatResult($result);
$result = $this->postJson($this->buildEndpoint($config), $params);

if (!empty($formatResult[1])) {
throw new GatewayErrorException($result, $formatResult[1], $formatResult);
if (!isset($result['code']) || '0' != $result['code']) {
throw new GatewayErrorException(json_encode($result, JSON_UNESCAPED_UNICODE), isset($result['code']) ? $result['code'] : 0, $result);
}

return $result;
}

/**
* @param $result http return from 253 service
* @param Config $config
*
* @return array
* @return string
*
* @throws InvalidArgumentException
*/
protected function formatResult($result)
protected function buildEndpoint(Config $config)
{
$result = str_replace("\n", ',', $result);
$channel = $this->getChannel($config);

return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel);
}

/**
* @param Config $config
*
* @return mixed
*
* @throws InvalidArgumentException
*/
protected function getChannel(Config $config)
{
$channel = $config->get('channel', self::CHANNEL_VALIDATE_CODE);

if (!in_array($channel, [self::CHANNEL_VALIDATE_CODE, self::CHANNEL_PROMOTION_CODE])) {
throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.');
}

return $channel;
}

/**
* @param string $content
* @param Config $config
*
* @return string|string
*
* @throws InvalidArgumentException
*/
protected function wrapChannelContent($content, Config $config)
{
$channel = $this->getChannel($config);

if (self::CHANNEL_PROMOTION_CODE == $channel) {
$sign = (string) $config->get('sign', '');
if (empty($sign)) {
throw new InvalidArgumentException('Invalid sign for ChuanglanGateway when using promotion channel');
}

$unsubscribe = (string) $config->get('unsubscribe', '');
if (empty($unsubscribe)) {
throw new InvalidArgumentException('Invalid unsubscribe for ChuanglanGateway when using promotion channel');
}

$content = $sign.$content.$unsubscribe;
}

return explode(',', $result);
return $content;
}
}
17 changes: 17 additions & 0 deletions src/Traits/HasHttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ protected function post($endpoint, $params = [], $headers = [])
]);
}

/**
* Make a post request with json params.
*
* @param $endpoint
* @param array $params
* @param array $headers
*
* @return array
*/
protected function postJson($endpoint, $params = [], $headers = [])
{
return $this->request('post', $endpoint, [
'headers' => $headers,
'json' => $params,
]);
}

/**
* Make a http request.
*
Expand Down
Loading

0 comments on commit 7e31817

Please sign in to comment.