Skip to content

Commit

Permalink
更新: 网易云信增加模板通知短信 (#350)
Browse files Browse the repository at this point in the history
* 更新ucloud测试

* 更新: 网易云信增加模板通知短信
  • Loading branch information
anhao authored Mar 8, 2024
1 parent c61a6dc commit bb88b24
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 25 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,18 @@ $easySms->send(18888888888, [
],
]);
```
通知模板短信

```php
$easySms->send(18888888888, [
'template' => 'templateid', // 模板编号(由客户顾问配置之后告知开发者)
'data' => [
'action' => 'sendTemplate', // 默认为 `sendCode`,校验短信验证码使用 `verifyCode`
'params' => [1,2,3], //短信参数列表,用于依次填充模板
],
]);
```


### [云之讯](https://www.ucpaas.com/index.html)

Expand Down
26 changes: 26 additions & 0 deletions src/Gateways/YunxinGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
case 'verifyCode':
$params = $this->buildVerifyCodeParams($to, $message);

break;
case "sendTemplate":
$params = $this->buildTemplateParams($to, $message, $config);

break;
default:
throw new GatewayErrorException(sprintf('action: %s not supported', $action), 0);
Expand Down Expand Up @@ -159,4 +163,26 @@ public function buildVerifyCodeParams(PhoneNumberInterface $to, MessageInterface
'code' => $data['code'],
];
}

/**
* @param PhoneNumberInterface $to
* @param MessageInterface $message
* @param Config $config
* @return array
*
*/
public function buildTemplateParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
{
$data = $message->getData($this);

$template = $message->getTemplate($this);

return [
'templateid'=>$template,
'mobiles'=>json_encode([$to->getUniversalNumber()]),
'params'=>array_key_exists('params',$data) ? json_encode($data['params']) : '',
'needUp'=>$config->get('need_up', false)
];

}
}
62 changes: 37 additions & 25 deletions tests/Gateways/UcloudGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Overtrue\EasySms\Tests\Gateways;

use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\UcloudGateway;
use Overtrue\EasySms\Gateways\YunxinGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
Expand Down Expand Up @@ -69,46 +71,56 @@ public function testSend()

$gateway->send(new PhoneNumber(18888888888), $message, $config);
}

public function testSignContent()
{
$defaultSigContent = 'default_sig_content';

$dataSigContent = 'data_sig_content';

$config = [
'private_key' => '', //私钥
'public_key' => '', //公钥
'sig_content' => $defaultSigContent, //签名
'project_id' => '',
'project_id' => '', //默认不填,子账号才需要填
];
$easySms = new EasySms($config);

$phoneNumber = new PhoneNumber('18888888888');
$gateway = $easySms->gateway('ucloud');

$reflectionMethod = new \ReflectionMethod(get_class($gateway), 'buildParams');
$reflectionMethod->setAccessible(true);
//验证指定签名
$message = new Message([
'template' => '',
'data' => [
'code' => '', // 如果是多个参数可以用数组
'mobiles' => '', //同时发送多个手机也可以用数组来,[1111111,11111]
'sig_content'=>$dataSigContent
],
]);
$result = $reflectionMethod->invokeArgs($gateway, [$phoneNumber, $message, $easySms->getConfig()]);
$this->assertSame($dataSigContent, $result['SigContent']);
//验证配置默认签名

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

$gateway->shouldReceive('request')->with(
'get',
\Mockery::on(function ($api) {
return 0 === strpos($api, UcloudGateway::ENDPOINT_URL);
}),
\Mockery::on(function ($params) {
return true;
})
)
->andReturn([
'RetCode' => UcloudGateway::SUCCESS_CODE,
], [
'RetCode' => 170,
'Message' => 'Missing signature',
])->times(2);

$message = new Message([
'template' => '',
'data' => [
'code' => '', // 如果是多个参数可以用数组
'mobiles' => '', //同时发送多个手机也可以用数组来,[1111111,11111]
'sig_content'=> $dataSigContent
],
]);
$result = $reflectionMethod->invokeArgs($gateway, [$phoneNumber, $message, $easySms->getConfig()]);
$this->assertSame($defaultSigContent, $result['SigContent']);
$config = new Config($config);

$this->assertSame([
'RetCode' => UcloudGateway::SUCCESS_CODE,
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(170);
$this->expectExceptionMessage('Missing signature');

$gateway->send(new PhoneNumber(18888888888), $message, $config);
}
}
55 changes: 55 additions & 0 deletions tests/Gateways/YunxinGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,61 @@ public function testSendWithVerifyCode()
$gateway->send($phone, $message, $config);
}

public function testSendWithTemplateMsg()
{
$config = [
'app_key' => 'mock-app-key',
'app_secret' => 'mock-app-secret',
];

$gateway = \Mockery::mock(YunxinGateway::class.'[post,buildHeaders,buildTemplateParams]', [$config]);
$gateway->shouldAllowMockingProtectedMethods();

$phone = new PhoneNumber('18888888888');

$message = new Message([
'template' => 'mock-template-code',
'data' => [
'params' => ['1'],
'action' => 'sendTemplate',
],
]);

$config = new Config($config);

$gateway->shouldReceive('buildHeaders')
->with($config)
->andReturn('mock-headers');

$gateway->shouldReceive('buildTemplateParams')
->with($phone, $message, $config)
->andReturn('mock-params');

$gateway->shouldReceive('post')
->with('https://api.netease.im/sms/sendtemplate.action', 'mock-params', 'mock-headers')
->andReturn([
'code' => 200,
'msg' => 5,
'obj' => 6379,
], [
'code' => 414,
'msg' => 'checksum',
])
->twice();

$this->assertSame([
'code' => 200,
'msg' => 5,
'obj' => 6379,
], $gateway->send($phone, $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(414);
$this->expectExceptionMessage('checksum');

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

public function testBuildEndpoint()
{
$config = [
Expand Down

0 comments on commit bb88b24

Please sign in to comment.