Skip to content

Commit

Permalink
更新: 网易云信增加模板通知短信
Browse files Browse the repository at this point in the history
  • Loading branch information
anhao committed Mar 8, 2024
1 parent e31bb19 commit f5a22dd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 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)
];

}
}
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 f5a22dd

Please sign in to comment.