|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace SixShop\TencentCloudSms;
|
|
|
@@ -50,23 +51,23 @@ class SmsClient
|
|
|
* @return array
|
|
|
*/
|
|
|
public function sendSms(
|
|
|
- array|string $phoneNumberSet,
|
|
|
- string $templateId,
|
|
|
- array|string $templateParamSet,
|
|
|
+ array|string $phoneNumberSet,
|
|
|
+ string $templateId,
|
|
|
+ array|string $templateParamSet,
|
|
|
?callable $next = null,
|
|
|
bool $autoCheckError = true
|
|
|
): array {
|
|
|
$phoneNumberSet = (array)$phoneNumberSet;
|
|
|
$templateParamSet = (array)$templateParamSet;
|
|
|
-
|
|
|
+
|
|
|
if (!isset($this->templateMap[$templateId])) {
|
|
|
throw new \InvalidArgumentException("template_id {$templateId} not found");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
$req = $this->getSendSmsRequest($phoneNumberSet, $templateId, $templateParamSet);
|
|
|
-
|
|
|
+
|
|
|
// 默认的发送处理函数
|
|
|
- $sendHandler = function($request) {
|
|
|
+ $sendHandler = function ($request) {
|
|
|
try {
|
|
|
$resp = $this->client->SendSms($request);
|
|
|
return json_decode($resp->toJsonString(), true);
|
|
|
@@ -74,7 +75,7 @@ class SmsClient
|
|
|
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 如果没有中间件,直接发送
|
|
|
if ($next === null) {
|
|
|
$response = $sendHandler($req);
|
|
|
@@ -83,9 +84,9 @@ class SmsClient
|
|
|
}
|
|
|
return $response;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 包装中间件调用
|
|
|
- $middleware = function($request, $checkError = true) use ($sendHandler, $autoCheckError) {
|
|
|
+ $middleware = function ($request, $checkError = true) use ($sendHandler, $autoCheckError) {
|
|
|
$response = $sendHandler($request);
|
|
|
// 只有当自动检查错误开启且中间件未显式禁用检查时才进行检查
|
|
|
if ($autoCheckError && $checkError !== false) {
|
|
|
@@ -93,10 +94,10 @@ class SmsClient
|
|
|
}
|
|
|
return $response;
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 执行中间件链
|
|
|
$response = $next($req, $middleware);
|
|
|
-
|
|
|
+
|
|
|
// 确保返回的是数组
|
|
|
return is_array($response) ? $response : [];
|
|
|
}
|
|
|
@@ -120,4 +121,4 @@ class SmsClient
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|