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/UcloudGatewayTest.php b/tests/Gateways/UcloudGatewayTest.php index 248c509..ff684ed 100644 --- a/tests/Gateways/UcloudGatewayTest.php +++ b/tests/Gateways/UcloudGatewayTest.php @@ -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; @@ -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); } } 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 = [