Skip to content

Commit

Permalink
更新:submail 添加内容直接发送 (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
anhao authored Mar 21, 2024
1 parent bb88b24 commit 7a25d59
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Gateways/NowcnGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -35,4 +35,4 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
}
return $result;
}
}
}
36 changes: 24 additions & 12 deletions src/Gateways/SubmailGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,38 @@ 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
*
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
*/
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);
Expand Down
1 change: 0 additions & 1 deletion src/Gateways/YunxinGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
];

}
}
3 changes: 1 addition & 2 deletions tests/Gateways/NowcnGatewaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ public function testSend()
$this->expectExceptionCode(-4);
$this->expectExceptionMessage('authorize failed');
$gateway->send(new PhoneNumber(18888888888), $message, $config);

}
}
}
41 changes: 41 additions & 0 deletions tests/Gateways/SubmailGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Gateways/UcloudGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testSignContent()
'data' => [
'code' => '', // 如果是多个参数可以用数组
'mobiles' => '', //同时发送多个手机也可以用数组来,[1111111,11111]
'sig_content'=> $dataSigContent
'sig_content' => $dataSigContent
],
]);
$config = new Config($config);
Expand Down

0 comments on commit 7a25d59

Please sign in to comment.