From f5a22dd875c52ae6b5dba21f5e8080eda8aa1d6f Mon Sep 17 00:00:00 2001 From: Alone88 Date: Fri, 8 Mar 2024 10:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0:=20=E7=BD=91=E6=98=93?= =?UTF-8?q?=E4=BA=91=E4=BF=A1=E5=A2=9E=E5=8A=A0=E6=A8=A1=E6=9D=BF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E7=9F=AD=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++ src/Gateways/YunxinGateway.php | 26 +++++++++++++ tests/Gateways/YunxinGatewayTest.php | 55 ++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/README.md b/README.md index 1b73066..eb8ffe0 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Gateways/YunxinGateway.php b/src/Gateways/YunxinGateway.php index aadf2fa..8db2784 100755 --- a/src/Gateways/YunxinGateway.php +++ b/src/Gateways/YunxinGateway.php @@ -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); @@ -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) + ]; + + } } diff --git a/tests/Gateways/YunxinGatewayTest.php b/tests/Gateways/YunxinGatewayTest.php index 1503736..f01288d 100755 --- a/tests/Gateways/YunxinGatewayTest.php +++ b/tests/Gateways/YunxinGatewayTest.php @@ -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 = [