|
@@ -3,13 +3,12 @@ declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace SixShop\TencentCloudSms;
|
|
namespace SixShop\TencentCloudSms;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
-use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
|
|
|
|
|
-use TencentCloud\Sms\V20190711\SmsClient as TencentCloudSmsClient;
|
|
|
|
|
|
|
+use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
|
|
|
|
|
+use TencentCloud\Sms\V20210111\SmsClient as TencentCloudSmsClient;
|
|
|
|
|
|
|
|
class SmsClient
|
|
class SmsClient
|
|
|
{
|
|
{
|
|
@@ -45,37 +44,69 @@ 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 回调函数
|
|
|
|
|
|
|
+ * @param callable|null $next 中间件回调函数
|
|
|
|
|
+ * @param bool $autoCheckError 是否自动检查错误
|
|
|
* @return array
|
|
* @return array
|
|
|
*/
|
|
*/
|
|
|
- public function sendSms(array|string $phoneNumberSet, string $templateId, array|string $templateParamSet, ?callable $responseCallback = null): array
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public function sendSms(
|
|
|
|
|
+ array|string $phoneNumberSet,
|
|
|
|
|
+ string $templateId,
|
|
|
|
|
+ array|string $templateParamSet,
|
|
|
|
|
+ ?callable $next = null,
|
|
|
|
|
+ bool $autoCheckError = true
|
|
|
|
|
+ ): array {
|
|
|
$phoneNumberSet = (array)$phoneNumberSet;
|
|
$phoneNumberSet = (array)$phoneNumberSet;
|
|
|
$templateParamSet = (array)$templateParamSet;
|
|
$templateParamSet = (array)$templateParamSet;
|
|
|
|
|
+
|
|
|
if (!isset($this->templateMap[$templateId])) {
|
|
if (!isset($this->templateMap[$templateId])) {
|
|
|
throw new \InvalidArgumentException("template_id {$templateId} not found");
|
|
throw new \InvalidArgumentException("template_id {$templateId} not found");
|
|
|
}
|
|
}
|
|
|
- try {
|
|
|
|
|
- $resp = $this->client->SendSms($this->getSendSmsRequest($phoneNumberSet, $templateId, $templateParamSet));
|
|
|
|
|
- } catch (TencentCloudSDKException $e) {
|
|
|
|
|
- throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
|
|
|
- }
|
|
|
|
|
- $resp = json_decode($resp->toJsonString(), true);
|
|
|
|
|
- if ($responseCallback) {
|
|
|
|
|
- return $responseCallback($resp);
|
|
|
|
|
- } else {
|
|
|
|
|
- $this->checkError($resp);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $req = $this->getSendSmsRequest($phoneNumberSet, $templateId, $templateParamSet);
|
|
|
|
|
+
|
|
|
|
|
+ // 默认的发送处理函数
|
|
|
|
|
+ $sendHandler = function($request) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ $resp = $this->client->SendSms($request);
|
|
|
|
|
+ return json_decode($resp->toJsonString(), true);
|
|
|
|
|
+ } catch (TencentCloudSDKException $e) {
|
|
|
|
|
+ throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // 如果没有中间件,直接发送
|
|
|
|
|
+ if ($next === null) {
|
|
|
|
|
+ $response = $sendHandler($req);
|
|
|
|
|
+ if ($autoCheckError) {
|
|
|
|
|
+ $this->checkError($response);
|
|
|
|
|
+ }
|
|
|
|
|
+ return $response;
|
|
|
}
|
|
}
|
|
|
- return $resp;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 包装中间件调用
|
|
|
|
|
+ $middleware = function($request, $checkError = true) use ($sendHandler, $autoCheckError) {
|
|
|
|
|
+ $response = $sendHandler($request);
|
|
|
|
|
+ // 只有当自动检查错误开启且中间件未显式禁用检查时才进行检查
|
|
|
|
|
+ if ($autoCheckError && $checkError !== false) {
|
|
|
|
|
+ $this->checkError($response);
|
|
|
|
|
+ }
|
|
|
|
|
+ return $response;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // 执行中间件链
|
|
|
|
|
+ $response = $next($req, $middleware);
|
|
|
|
|
+
|
|
|
|
|
+ // 确保返回的是数组
|
|
|
|
|
+ return is_array($response) ? $response : [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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->setTemplateID($templateId);
|
|
|
|
|
- $req->setSign($this->config->sign_name);
|
|
|
|
|
|
|
+ $req->setSmsSdkAppId($this->config->app_id);
|
|
|
|
|
+ $req->setTemplateId($templateId);
|
|
|
|
|
+ $req->setSignName($this->config->sign_name);
|
|
|
$req->setTemplateParamSet($templateParamSet);
|
|
$req->setTemplateParamSet($templateParamSet);
|
|
|
return $req;
|
|
return $req;
|
|
|
}
|
|
}
|