diff --git a/src/Gateways/NowcnGateway.php b/src/Gateways/NowcnGateway.php index 1a41d8a..0638ace 100644 --- a/src/Gateways/NowcnGateway.php +++ b/src/Gateways/NowcnGateway.php @@ -21,7 +21,7 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config if (!$config->get('key')) { throw new GatewayErrorException("key not found", -2, []); } - $params=[ + $params = [ 'mobile' => $to->getNumber(), 'content' => $message->getContent($this), 'userId' => $config->get('key'), @@ -35,4 +35,4 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config } return $result; } -} \ No newline at end of file +} diff --git a/src/Gateways/SubmailGateway.php b/src/Gateways/SubmailGateway.php index 7d4678f..d496175 100644 --- a/src/Gateways/SubmailGateway.php +++ b/src/Gateways/SubmailGateway.php @@ -32,8 +32,8 @@ class SubmailGateway extends Gateway /** * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to - * @param \Overtrue\EasySms\Contracts\MessageInterface $message - * @param \Overtrue\EasySms\Support\Config $config + * @param \Overtrue\EasySms\Contracts\MessageInterface $message + * @param \Overtrue\EasySms\Support\Config $config * * @return array * @@ -41,17 +41,29 @@ class SubmailGateway extends Gateway */ public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) { - $endpoint = $this->buildEndpoint($this->inChineseMainland($to) ? 'message/xsend' : 'internationalsms/xsend'); - - $data = $message->getData($this); + $isContent = !!$message->getContent($this); + if ($isContent) { + $endpoint = $this->buildEndpoint($this->inChineseMainland($to) ? 'sms/send' : 'internationalsms/send'); + $params = [ + 'appid' => $config->get('app_id'), + 'signature' => $config->get('app_key'), + 'content' => $message->getContent($this), + 'to' => $to->getUniversalNumber(), + ]; + } else { + $endpoint = $this->buildEndpoint($this->inChineseMainland($to) ? 'message/xsend' : 'internationalsms/xsend'); + $data = $message->getData($this); + $template_code = $message->getTemplate($this); + $params = [ + 'appid' => $config->get('app_id'), + 'signature' => $config->get('app_key'), + 'project' => !empty($template_code) ? $template_code : (!empty($data['project']) ? $data['project'] : $config->get('project')), + 'to' => $to->getUniversalNumber(), + 'vars' => json_encode($data, JSON_FORCE_OBJECT), + ]; + } - $result = $this->post($endpoint, [ - 'appid' => $config->get('app_id'), - 'signature' => $config->get('app_key'), - 'project' => !empty($data['project']) ? $data['project'] : $config->get('project'), - 'to' => $to->getUniversalNumber(), - 'vars' => json_encode($data, JSON_FORCE_OBJECT), - ]); + $result = $this->post($endpoint, $params); if ('success' != $result['status']) { throw new GatewayErrorException($result['msg'], $result['code'], $result); diff --git a/src/Gateways/YunxinGateway.php b/src/Gateways/YunxinGateway.php index 8db2784..dd765fe 100755 --- a/src/Gateways/YunxinGateway.php +++ b/src/Gateways/YunxinGateway.php @@ -183,6 +183,5 @@ public function buildTemplateParams(PhoneNumberInterface $to, MessageInterface $ 'params'=>array_key_exists('params',$data) ? json_encode($data['params']) : '', 'needUp'=>$config->get('need_up', false) ]; - } } diff --git a/tests/Gateways/NowcnGatewaysTest.php b/tests/Gateways/NowcnGatewaysTest.php index 7f20809..85e2cfb 100644 --- a/tests/Gateways/NowcnGatewaysTest.php +++ b/tests/Gateways/NowcnGatewaysTest.php @@ -47,6 +47,5 @@ public function testSend() $this->expectExceptionCode(-4); $this->expectExceptionMessage('authorize failed'); $gateway->send(new PhoneNumber(18888888888), $message, $config); - } -} \ No newline at end of file +} diff --git a/tests/Gateways/SubmailGatewayTest.php b/tests/Gateways/SubmailGatewayTest.php index 692392a..e50cfbd 100644 --- a/tests/Gateways/SubmailGatewayTest.php +++ b/tests/Gateways/SubmailGatewayTest.php @@ -63,6 +63,47 @@ public function testSend() $gateway->send(new PhoneNumber(18188888888), $message, $config); } + public function testContentSend() + { + $config = [ + 'app_id' => 'mock-app-id', + 'app_key' => 'mock-app-key', + ]; + $gateway = \Mockery::mock(SubmailGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods(); + + $gateway->shouldReceive('post')->with('https://api.mysubmail.com/sms/send.json', [ + 'appid' => 'mock-app-id', + 'signature' => 'mock-app-key', + 'to' => new PhoneNumber(18188888888), + 'content' => '【easysms】 mock-app-content' + ])->andReturn([ + 'status' => 'success', + 'send_id' => '093c0a7df143c087d6cba9cdf0cf3738', + 'fee' => 1, + 'sms_credits' => 14197, + ], [ + 'status' => 'error', + 'code' => 100, + 'msg' => 'mock-err-msg', + ])->times(2); + + $message = new Message(['content' => '【easysms】 mock-app-content']); + $config = new Config($config); + + $this->assertSame([ + 'status' => 'success', + 'send_id' => '093c0a7df143c087d6cba9cdf0cf3738', + 'fee' => 1, + 'sms_credits' => 14197, + ], $gateway->send(new PhoneNumber(18188888888), $message, $config)); + + $this->expectException(GatewayErrorException::class); + $this->expectExceptionCode(100); + $this->expectExceptionMessage('mock-err-msg'); + + $gateway->send(new PhoneNumber(18188888888), $message, $config); + } + public function testProject() { $config = [ diff --git a/tests/Gateways/UcloudGatewayTest.php b/tests/Gateways/UcloudGatewayTest.php index ff684ed..2c00e81 100644 --- a/tests/Gateways/UcloudGatewayTest.php +++ b/tests/Gateways/UcloudGatewayTest.php @@ -108,7 +108,7 @@ public function testSignContent() 'data' => [ 'code' => '', // 如果是多个参数可以用数组 'mobiles' => '', //同时发送多个手机也可以用数组来,[1111111,11111] - 'sig_content'=> $dataSigContent + 'sig_content' => $dataSigContent ], ]); $config = new Config($config);