Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加移动云MAS黑名单HTTP模式通道 #355

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- [天瑞云](http://cms.tinree.com/)
- [时代互联](https://www.now.cn/)
- [火山引擎](https://console.volcengine.com/sms/)
- [移动云MAS(黑名单模式)](https://mas.10086.cn)

## 环境需求

Expand Down Expand Up @@ -995,6 +996,27 @@ $easySms->send(18888888888, [
]);
```

### [移动云MAS(黑名单模式)](https://mas.10086.cn/)

短信内容使用 `template` + `data`

```php
'yidongmasblack' => [
'ecName' => '', // 机构名称
'secretKey' => '', // 密钥
'apId' => '', // 应用ID
'sign' => '', // 签名
'addSerial' => '', // 通道号默认空
],
```

发送示例:

```php
$easySms->send(18888888888, [
'content' => '您的验证码为: 6379',
]);
```
## :heart: 支持我

[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)
Expand Down
75 changes: 75 additions & 0 deletions src/Gateways/YiDongMasBlackGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;

/**
* Class YidongmasblackGateway.
* 移动MAS黑名单模式(免创建模板)
* @author houang <[email protected]>
*
* @see https://mas.10086.cn
*/
class YidongmasblackGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'http://112.35.1.155:1992/sms/norsubmit';
overtrue marked this conversation as resolved.
Show resolved Hide resolved

const ENDPOINT_METHOD = 'send';

/**
* @param PhoneNumberInterface $to
* @param MessageInterface $message
* @param Config $config
*
* @return \Psr\Http\Message\ResponseInterface|array|string
*
* @throws GatewayErrorException
*/
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$params["ecName"] = $config->get('ecName');
$params["apId"] = $config->get('apId');
$params["sign"] = $config->get('sign');
$params["addSerial"] = $config->get('addSerial');
$params["mobiles"] = $to->getNumber();
$params["content"] = $message->getContent();
$result = $this->postJson(self::ENDPOINT_URL, $this->generateContent($params));

if ('true' != $result['success']) {
throw new GatewayErrorException($result['success'], $result['rspcod'], $result);
}

return $result;
}

/**
* Generate Content.
*
* @param array $params
*
* @return string
*/
protected function generateContent($params)
{
$secretKey = $this->config->get('secretKey');
$params['mac'] = md5($params["ecName"].$params["apId"].$secretKey.$params["mobiles"].$params["content"].$params["sign"].$params["addSerial"]);

return base64_encode(json_encode($params));
}
}
87 changes: 87 additions & 0 deletions tests/Gateways/YiDongMasBlackGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Tests\Gateways;

use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\YidongmasblackGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;

class YidongmasblackGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'ecName' => 'mock-ec-name',
'secretKey' => 'mock-secret-key',
'apId' => 'mock-ap-id',
'sign' => 'mock-sign',
'addSerial' => 'mock-add-serial',
];
$gateway = \Mockery::mock(YidongmasblackGateway::class.'[get]', [$config])->shouldAllowMockingProtectedMethods();

$expected = [
'RegionId' => 'cn-hangzhou',
'AccessKeyId' => 'mock-api-key',
'Format' => 'JSON',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureVersion' => '1.0',
// 'SignatureNonce' => uniqid(),
// 'Timestamp' => date('Y-m-d\TH:i:s\Z'),
'Action' => 'SendSms',
'Version' => '2017-05-25',
'PhoneNumbers' => strval(new PhoneNumber(18888888888)),
'SignName' => 'mock-api-sign-name',
'TemplateCode' => 'mock-template-code',
'TemplateParam' => json_encode(['code' => '123456']),
];
$gateway->shouldReceive('post')
->with(YidongmasblackGateway::ENDPOINT_URL, \Mockery::on(function ($params) use ($expected) {
if (empty($params['Signature'])) {
return false;
}

unset($params['SignatureNonce'], $params['Timestamp'], $params['Signature']);

ksort($params);
ksort($expected);

return $params == $expected;
}))
->andReturn([
'Code' => 'OK',
'Message' => 'mock-result',
], [
'Code' => 1234,
'Message' => 'mock-err-msg',
])
->twice();

$message = new Message([
'content' => '123456',
]);

$config = new Config($config);

$this->assertSame([
'Code' => 'OK',
'Message' => 'mock-result',
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(1234);
$this->expectExceptionMessage('mock-err-msg');
$gateway->send(new PhoneNumber(18888888888), $message, $config);
}
}
Loading