-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
711 additions
and
31 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 overtrue | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
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
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,147 @@ | ||
<?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 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/%s/json'; | ||
|
||
/** | ||
* 支持单发、群发短信 | ||
*/ | ||
const CHANNEL_NORMAL_CODE = 'v1/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'), | ||
'report' => $config->get('needstatus') ?? false | ||
]; | ||
|
||
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['phone'] = $to->getNumber(); | ||
$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; | ||
} | ||
} |
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
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
Oops, something went wrong.