|
@@ -1,5 +1,6 @@
|
|
|
<?php
|
|
<?php
|
|
|
declare(strict_types=1);
|
|
declare(strict_types=1);
|
|
|
|
|
+
|
|
|
namespace SixShop\TencentCloudSms;
|
|
namespace SixShop\TencentCloudSms;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -26,6 +27,7 @@ class SmsClient
|
|
|
'InvalidParameterValue.ContentLengthLimit' => '内容长度超出限制',
|
|
'InvalidParameterValue.ContentLengthLimit' => '内容长度超出限制',
|
|
|
'InvalidParameterValue.IncorrectPhoneNumber' => '号码格式错误',
|
|
'InvalidParameterValue.IncorrectPhoneNumber' => '号码格式错误',
|
|
|
];
|
|
];
|
|
|
|
|
+
|
|
|
public function __construct(private Config $config)
|
|
public function __construct(private Config $config)
|
|
|
{
|
|
{
|
|
|
$this->templateMap = array_to_map($this->config->tencent_sms_templates, 'template_id', 'template_content');
|
|
$this->templateMap = array_to_map($this->config->tencent_sms_templates, 'template_id', 'template_content');
|
|
@@ -43,8 +45,10 @@ class SmsClient
|
|
|
* @param array|string $phoneNumberSet 手机号码列表
|
|
* @param array|string $phoneNumberSet 手机号码列表
|
|
|
* @param string $templateId 模板ID
|
|
* @param string $templateId 模板ID
|
|
|
* @param array|string $templateParamSet 模板参数
|
|
* @param array|string $templateParamSet 模板参数
|
|
|
|
|
+ * @param callable|null $responseCallback 回调函数
|
|
|
|
|
+ * @return array
|
|
|
*/
|
|
*/
|
|
|
- public function sendSms(array|string $phoneNumberSet, string $templateId, array|string $templateParamSet): array
|
|
|
|
|
|
|
+ public function sendSms(array|string $phoneNumberSet, string $templateId, array|string $templateParamSet, ?callable $responseCallback = null): array
|
|
|
{
|
|
{
|
|
|
$phoneNumberSet = (array)$phoneNumberSet;
|
|
$phoneNumberSet = (array)$phoneNumberSet;
|
|
|
$templateParamSet = (array)$templateParamSet;
|
|
$templateParamSet = (array)$templateParamSet;
|
|
@@ -57,13 +61,17 @@ class SmsClient
|
|
|
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
|
}
|
|
}
|
|
|
$resp = json_decode($resp->toJsonString(), true);
|
|
$resp = json_decode($resp->toJsonString(), true);
|
|
|
- $this->checkError($resp);
|
|
|
|
|
|
|
+ if ($responseCallback) {
|
|
|
|
|
+ return $responseCallback($resp);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->checkError($resp);
|
|
|
|
|
+ }
|
|
|
return $resp;
|
|
return $resp;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function getSendSmsRequest(array $phoneNumberSet, string $templateId, array $templateParamSet): SendSmsRequest
|
|
private function getSendSmsRequest(array $phoneNumberSet, string $templateId, array $templateParamSet): SendSmsRequest
|
|
|
{
|
|
{
|
|
|
- $req = new SendSmsRequest();
|
|
|
|
|
|
|
+ $req = new SendSmsRequest();
|
|
|
$req->setPhoneNumberSet($phoneNumberSet);
|
|
$req->setPhoneNumberSet($phoneNumberSet);
|
|
|
$req->setSmsSdkAppid($this->config->app_id);
|
|
$req->setSmsSdkAppid($this->config->app_id);
|
|
|
$req->setTemplateID($templateId);
|
|
$req->setTemplateID($templateId);
|
|
@@ -71,6 +79,7 @@ class SmsClient
|
|
|
$req->setTemplateParamSet($templateParamSet);
|
|
$req->setTemplateParamSet($templateParamSet);
|
|
|
return $req;
|
|
return $req;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
private function checkError(array $resp): void
|
|
private function checkError(array $resp): void
|
|
|
{
|
|
{
|
|
|
foreach ($resp['SendStatusSet'] as $statusItem) {
|
|
foreach ($resp['SendStatusSet'] as $statusItem) {
|