diff --git a/README.md b/README.md index b2c0c5e..5a50cf5 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ - [百度云](https://cloud.baidu.com/) - [华信短信平台](http://www.ipyy.com/) - [253云通讯(创蓝)](https://www.253.com/) +- [创蓝云智](https://www.chuanglan.com/) - [融云](http://www.rongcloud.cn) - [天毅无线](http://www.85hu.com/) - [阿凡达数据](http://www.avatardata.cn/) @@ -520,6 +521,45 @@ $easySms->send($phone_number, [ ], ``` +### [创蓝云智](https://www.chuanglan.com/) + +普通短信发送内容使用 `content` + +```php + 'chuanglanv1' => [ + 'account' => '', + 'password' => '', + 'needstatus' => false, + 'channel' => \Overtrue\EasySms\Gateways\ChuanglanV1Gateway::CHANNEL_NORMAL_CODE, + ], +``` +发送示例: + +```php +$easySms->send(18888888888, [ + 'content' => xxxxxxx +]); +``` + +变量短信发送内容使用 `template` + `data` + +```php + 'chuanglanv1' => [ + 'account' => '', + 'password' => '', + 'needstatus' => false, + 'channel' => \Overtrue\EasySms\Gateways\ChuanglanV1Gateway::CHANNEL_VARIABLE_CODE, + ], +``` +发送示例: + +```php +$easySms->send(18888888888, [ + 'template' => xxxxxx, // 模板内容 + 'data' => 'phone":"15800000000,1234;15300000000,4321', +]); +``` + ### [融云](http://www.rongcloud.cn) 短信分为两大类,验证类和通知类短信。 发送验证类短信使用 `template` + `data` diff --git a/src/Gateways/Chuanglanv1Gateway.php b/src/Gateways/Chuanglanv1Gateway.php new file mode 100644 index 0000000..528c64e --- /dev/null +++ b/src/Gateways/Chuanglanv1Gateway.php @@ -0,0 +1,146 @@ + + * + * 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 Overtrue\EasySms\Contracts\MessageInterface; +use Overtrue\EasySms\Contracts\PhoneNumberInterface; +use Overtrue\EasySms\Exceptions\GatewayErrorException; +use Overtrue\EasySms\Exceptions\InvalidArgumentException; +use Overtrue\EasySms\Support\Config; +use Overtrue\EasySms\Traits\HasHttpRequest; + +/** + * Class ChuanglanGateway. + * + * @see https://www.chuanglan.com/document/6110e57909fd9600010209de/62b3dc1d272e290001af3e75 + */ +class Chuanglanv1Gateway extends Gateway +{ + use HasHttpRequest; + + /** + * 国际短信 + */ + const INT_URL = 'http://intapi.253.com/send/json'; + + /** + * URL模板 + */ + const ENDPOINT_URL_TEMPLATE = 'https://smssh1.253.com/msg/v1/%s/json'; + + /** + * 支持单发、群发短信 + */ + const CHANNEL_NORMAL_CODE = 'send'; + + /** + * 单号码对应单内容批量下发 + */ + const CHANNEL_VARIABLE_CODE = 'variable'; + + /** + * @param PhoneNumberInterface $to + * @param MessageInterface $message + * @param Config $config + * + * @return array + * + * @throws GatewayErrorException + * @throws InvalidArgumentException + */ + public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) + { + $IDDCode = !empty($to->getIDDCode()) ? $to->getIDDCode() : 86; + + $params = [ + 'account' => $config->get('account'), + 'password' => $config->get('password'), + 'phone' => $to->getNumber(), + ]; + + if (86 != $IDDCode) { + $params['mobile'] = $to->getIDDCode() . $to->getNumber(); + $params['account'] = $config->get('intel_account') ?: $config->get('account'); + $params['password'] = $config->get('intel_password') ?: $config->get('password'); + } + + if (self::CHANNEL_VARIABLE_CODE == $this->getChannel($config, $IDDCode)) { + $params['params'] = $message->getData($this); + $params['msg'] = $this->wrapChannelContent($message->getTemplate($this), $config, $IDDCode); + } else { + $params['msg'] = $this->wrapChannelContent($message->getContent($this), $config, $IDDCode); + } + + $result = $this->postJson($this->buildEndpoint($config, $IDDCode), $params); + + 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 Config $config + * @param int $IDDCode + * + * @return string + * + * @throws InvalidArgumentException + */ + protected function buildEndpoint(Config $config, $IDDCode = 86) + { + $channel = $this->getChannel($config, $IDDCode); + + if (self::INT_URL === $channel) { + return $channel; + } + + return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel); + } + + /** + * @param Config $config + * @param int $IDDCode + * + * @return mixed + * + * @throws InvalidArgumentException + */ + protected function getChannel(Config $config, $IDDCode) + { + if (86 != $IDDCode) { + return self::INT_URL; + } + $channel = $config->get('channel', self::CHANNEL_NORMAL_CODE); + + if (!in_array($channel, [self::CHANNEL_NORMAL_CODE, self::CHANNEL_VARIABLE_CODE])) { + throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.'); + } + + return $channel; + } + + /** + * @param string $content + * @param Config $config + * @param int $IDDCode + * + * @return string|string + * + * @throws InvalidArgumentException + */ + protected function wrapChannelContent($content, Config $config, $IDDCode) + { + return $content; + } +}