Skip to content

Commit

Permalink
增加: 时代互联的短信网关API https://www.now.cn (#347)
Browse files Browse the repository at this point in the history
* add: 时代互联的短信网关API https://www.now.cn

* ### 补充时代互联文档说明

---------

Co-authored-by: dsuzejian <szj@ericma.onaliyun.com>
Co-authored-by: 安正超 <anzhengchao@gmail.com>
  • Loading branch information
3 people committed Aug 7, 2023
1 parent 78b9bda commit 81d4dee
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [蜘蛛云](https://zzyun.com/)
- [融合云信](https://maap.wo.cn/)
- [天瑞云](http://cms.tinree.com/)
- [时代互联](https://www.now.cn/)
- [火山引擎](https://console.volcengine.com/sms/)

## 环境需求
Expand Down Expand Up @@ -925,6 +926,25 @@ $easySms->send(18888888888, [
]);
```

### [时代互联](https://www.now.cn/)

短信使用 `content`

```php
'nowcn' => [
'key' => '', //用户ID
'secret' => '', //开发密钥
'api_type' => '', // 短信通道,
],
```

发送示例:
```php
$easySms->send(18888888888, [
'content' => '您的验证码为: 6379',
]);
```

### [火山引擎](https://console.volcengine.com/sms/)

短信内容使用 `template` + `data`
Expand Down
34 changes: 34 additions & 0 deletions src/Gateways/NowcnGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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 NowcnGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'http://ad1200.now.net.cn:2003/sms/sendSMS';

const SUCCESS_CODE = 0;

public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$params=[
'mobile' => $to->getNumber(),
'content' => $message->getContent($this),
'userId' => $config->get('key'),
'password' => $config->get('secret'),
'apiType' => $config->get('api_type'),
];
$result = $this->get(self::ENDPOINT_URL, $params);
if (self::SUCCESS_CODE != $result['code']) {
throw new GatewayErrorException($result['msg'], $result['code'], $result);
}
return $result;
}
}
52 changes: 52 additions & 0 deletions tests/Gateways/NowcnGatewaysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Overtrue\EasySms\Tests\Gateways;

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

class NowcnGatewaysTest extends TestCase
{
public function testSend()
{
$config = [
'key' => '1',
'secret' => '1',
'api_type' => '3',
];

$gateway = \Mockery::mock(NowcnGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('request')->with(
'get',
\Mockery::on(function ($api) {
return 0 === strpos($api, NowcnGateway::ENDPOINT_URL);
}),
\Mockery::on(function ($params) {
return true;
})
) ->andReturn([
'code' => NowcnGateway::SUCCESS_CODE,
], [
'code' => "-4",
'msg' => 'authorize failed',
])->times(2);

$message = new Message([
'content' => 'mock-content',
]);
$config = new Config($config);
$this->assertSame([
'code' => NowcnGateway::SUCCESS_CODE,
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(-4);
$this->expectExceptionMessage('authorize failed');
$gateway->send(new PhoneNumber(18888888888), $message, $config);

}
}

0 comments on commit 81d4dee

Please sign in to comment.