Skip to content

Commit

Permalink
Feature add sms Gateway DreamNet and Huaxin (overtrue#33)
Browse files Browse the repository at this point in the history
* Feature add gateways Dreamnet and Huanxin ✨

* Feature test the gateway Dreamnet and huaxin ✅

* Fix code style

* Fix drop DreamNet gateway and fix code style 🐛
  • Loading branch information
TIGERB authored and overtrue committed Jul 7, 2017
1 parent d9e0766 commit aaa91be
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 15 deletions.
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- [聚合数据](https://www.juhe.cn)
- [SendCloud](http://www.sendcloud.net/)
- [百度云](https://cloud.baidu.com/)
- [华信短信平台](http://www.ipyy.com/)


## 环境需求
Expand All @@ -63,12 +64,12 @@ use Overtrue\EasySms\EasySms;
$config = [
// HTTP 请求的超时时间(秒)
'timeout' => 5.0,

// 默认发送配置
'default' => [
// 网关调用策略,默认:顺序调用
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

// 默认可用的发送网关
'gateways' => [
'yunpian', 'aliyun', 'alidayu',
Expand Down Expand Up @@ -96,9 +97,9 @@ $config = [
$easySms = new EasySms($config);

$easySms->send(13188888888, [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'code' => 6379
],
]);
Expand All @@ -120,9 +121,9 @@ $easySms->send(13188888888, [

```php
$easySms->send(13188888888, [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'code' => 6379
],
], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值
Expand Down Expand Up @@ -173,9 +174,9 @@ $easySms->extend('mygateway', function($gatewayConfig){
});

$easySms->send(13188888888, [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'content' => '您的验证码为: 6379',
'template' => 'SMS_001',
'data' => [
'code' => 6379
],
]);
Expand All @@ -202,19 +203,19 @@ class OrderPaidMessage extends Message
{
$this->order = $order;
}

// 定义直接使用内容发送平台的内容
public function getContent(GatewayInterface $gateway = null)
{
return sprintf('您的订单:%s, 已经完成付款', $this->order->no);
}

// 定义使用模板发送方式平台所需要的模板 ID
public function getTemplate(GatewayInterface $gateway = null)
{
return 'SMS_003';
return 'SMS_003';
}

// 模板参数
public function getData(GatewayInterface $gateway = null)
{
Expand Down Expand Up @@ -351,6 +352,18 @@ $easySms->send(13188888888, $message);
],
```

### [华信短信平台](http://www.ipyy.com/)

```php
'huaxin' => [
'user_id' => '',
'password' => '',
'account' => '',
'ip' => '',
'ext_no' => '',
],
```

## License

MIT
75 changes: 75 additions & 0 deletions src/Gateways/HuaxinGateway.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\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;

/**
* Class HuaxinGateway.
*
* @see http://www.ipyy.com/help/
*/
class HuaxinGateway extends Gateway
{
use HasHttpRequest;

const ENDPOINT_TEMPLATE = 'http://%s/smsJson.aspx';

/**
* @param array|int|string $to
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param \Overtrue\EasySms\Support\Config $config
*
* @return array
*
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException;
*/
public function send($to, MessageInterface $message, Config $config)
{
$endpoint = $this->buildEndpoint(
$config->get('ip')
);

$result = $this->post($endpoint, [
'userid' => $config->get('user_id'),
'account' => $config->get('account'),
'password' => $config->get('password'),
'mobile' => $to,
'content' => $message->getContent(),
'sendTime' => '',
'action' => 'send',
'extno' => $config->get('ext_no'),
]);

if ($result['returnstatus'] !== 'Success') {
throw new GatewayErrorException($result['message'], 500, $result);
}

return $result;
}

/**
* Build endpoint url.
*
* @param string $ip
* @param string $port
*
* @return string
*/
protected function buildEndpoint($ip)
{
return sprintf(self::ENDPOINT_TEMPLATE, $ip);
}
}
75 changes: 75 additions & 0 deletions tests/Gateways/HuaxinGatewayTest.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\Tests\Gateways;

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

class HuaxinGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'user_id' => 'mock-user-id',
'password' => 'mock-password',
'account' => 'mock-account',
'ip' => '127.0.0.1',
'ext_no' => '',
];
$gateway = \Mockery::mock(HuaxinGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods();

$gateway->shouldReceive('post')->with('http://127.0.0.1/smsJson.aspx', [
'userid' => 'mock-user-id',
'password' => 'mock-password',
'account' => 'mock-account',
'mobile' => 18188888888,
'content' => '【TIGERB】This is a test message.',
'sendTime' => '',
'action' => 'send',
'extno' => '',
])->andReturn([
'returnstatus' => 'Success',
'message' => '操作成功',
'remainpoint' => '100',
'taskID' => '1504080852350206',
'successCounts' => '1',
], [
'returnstatus' => 'Faild',
'message' => '操作失败',
'remainpoint' => '0',
'taskID' => '0',
'successCounts' => '0',
])->times(2);

$message = new Message(['content' => '【TIGERB】This is a test message.']);
$config = new Config($config);
$this->assertSame(
[
'returnstatus' => 'Success',
'message' => '操作成功',
'remainpoint' => '100',
'taskID' => '1504080852350206',
'successCounts' => '1',
],
$gateway->send(18188888888, $message, $config)
);

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(500);
$this->expectExceptionMessage('操作失败');

$gateway->send(18188888888, $message, $config);
}
}

0 comments on commit aaa91be

Please sign in to comment.