Skip to content

Commit

Permalink
新增短信宝支持 (overtrue#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwindy authored Sep 13, 2020
1 parent 3e91c7d commit 7f880cb
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 5 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
- [七牛云](https://www.qiniu.com/)
- [UE35.net](http://uesms.ue35.cn/)
- [Ucloud](https://www.ucloud.cn)

- [短信宝](http://www.smsbao.com/)
## 环境需求

- PHP >= 5.6
Expand Down Expand Up @@ -698,6 +698,24 @@ $easySms->send(18888888888, [

```


### [短信宝](http://www.smsbao.com/)
短信使用 `template`

```php
'smsbao' => [
'user' => '', //账号
'password' => '' //密码
],
```

```php
$easySms->send(18888888888, [
'template' => '您的验证码为: 6379', //短信模板
]);

```

## PHP 扩展包开发

> 想知道如何从零开始构建 PHP 扩展包?
Expand Down
78 changes: 78 additions & 0 deletions src/Gateways/SmsbaoGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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 SmsbaoGateway
* @author iwindy <[email protected]>
* @see http://www.smsbao.com/openapi/
*/
class SmsbaoGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_URL = 'http://api.smsbao.com/%s';

const SUCCESS_CODE = '0';

protected $errorStatuses = [
'0' => '短信发送成功',
'-1' => '参数不全',
'-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!',
'30' => '密码错误',
'40' => '账号不存在',
'41' => '余额不足',
'42' => '帐户已过期',
'43' => 'IP地址限制',
'50' => '内容含有敏感词'
];

public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$data = $message->getContent($this);

if (is_null($to->getIDDCode())) {
$number = $to->getNumber();
$action = 'sms';
} else {
$number = $to->getUniversalNumber();
$action = 'wsms';
}

$params = [
'u' => $config->get('user'),
'p' => md5($config->get('password')),
'm' => $number,
'c' => $data
];

$result = $this->get($this->buildEndpoint($action), $params);

if ($result !== self::SUCCESS_CODE) {
throw new GatewayErrorException($this->errorStatuses[$result], $result);
}

return $result;
}

protected function buildEndpoint($type)
{
return sprintf(self::ENDPOINT_URL, $type);
}
}
8 changes: 4 additions & 4 deletions src/Traits/HasHttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait HasHttpRequest
* @param array $query
* @param array $headers
*
* @return array
* @return ResponseInterface|array|string
*/
protected function get($endpoint, $query = [], $headers = [])
{
Expand All @@ -43,7 +43,7 @@ protected function get($endpoint, $query = [], $headers = [])
* @param array $params
* @param array $headers
*
* @return array
* @return ResponseInterface|array|string
*/
protected function post($endpoint, $params = [], $headers = [])
{
Expand All @@ -60,7 +60,7 @@ protected function post($endpoint, $params = [], $headers = [])
* @param array $params
* @param array $headers
*
* @return array
* @return ResponseInterface|array|string
*/
protected function postJson($endpoint, $params = [], $headers = [])
{
Expand All @@ -77,7 +77,7 @@ protected function postJson($endpoint, $params = [], $headers = [])
* @param string $endpoint
* @param array $options http://docs.guzzlephp.org/en/latest/request-options.html
*
* @return array
* @return ResponseInterface|array|string
*/
protected function request($method, $endpoint, $options = [])
{
Expand Down
97 changes: 97 additions & 0 deletions tests/Gateways/SmsbaoGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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\SmsbaoGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;

/**
* Class SmsbaoGatewayTest
* @author iwindy <[email protected]>
*/
class SmsbaoGatewayTest extends TestCase
{
public function testSendWithSMS()
{
$config = [
'user' => 'mock-user',
'password' => 'mock-password'
];

$gateway = \Mockery::mock(SmsbaoGateway::class . '[get]', [$config])->shouldAllowMockingProtectedMethods();

$params = [
'u' => 'mock-user',
'p' => md5('mock-password'),
'm' => '18188888888',
'c' => 'This is a test message.'
];

$endpoint = sprintf(SmsbaoGateway::ENDPOINT_URL, 'sms');
$gateway->shouldReceive('get')
->with($endpoint, $params)
->andReturn('0', '30')
->times(2);

$message = new Message(['content' => 'This is a test message.']);
$config = new Config($config);

$this->assertSame(
'0',
$gateway->send(new PhoneNumber(18188888888), $message, $config)
);

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(30);
$gateway->send(new PhoneNumber(18188888888), $message, $config);
}

public function testSendWithWSMS()
{
$config = [
'user' => 'mock-user',
'password' => 'mock-password'
];

$gateway = \Mockery::mock(SmsbaoGateway::class . '[get]', [$config])->shouldAllowMockingProtectedMethods();

$params = [
'u' => 'mock-user',
'p' => md5('mock-password'),
'm' => '+8618188888888',
'c' => 'This is a test message.'
];

$endpoint = sprintf(SmsbaoGateway::ENDPOINT_URL, 'wsms');
$gateway->shouldReceive('get')
->with($endpoint, $params)
->andReturn('0', '30')
->times(2);

$message = new Message(['content' => 'This is a test message.']);
$config = new Config($config);

$this->assertSame(
'0',
$gateway->send(new PhoneNumber(18188888888, 86), $message, $config)
);

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(30);
$gateway->send(new PhoneNumber(18188888888, 86), $message, $config);
}
}

0 comments on commit 7f880cb

Please sign in to comment.