Преглед на файлове

改用V20210111版本,改为$next回调,添加autoCheckError, 添加Facade

runphp преди 6 месеца
родител
ревизия
7193f5ea40
променени са 2 файла, в които са добавени 68 реда и са изтрити 20 реда
  1. 17 0
      src/Facade/SmsClient.php
  2. 51 20
      src/SmsClient.php

+ 17 - 0
src/Facade/SmsClient.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace SixShop\TencentCloudSms\Facade;
+
+use SixShop\TencentCloudSms\SmsClient as TencentCloudSmsSmsClient;
+use think\Facade;
+
+/**
+ * @method static array sendSms(array|string $phoneNumberSet, string $templateId, array|string $templateParamSet,  ?callable $next = null, bool $autoCheckError = true) 发送短信
+ */
+class SmsClient extends Facade
+{
+    protected static function getFacadeClass()
+    {
+        return TencentCloudSmsSmsClient::class;
+    }
+}

+ 51 - 20
src/SmsClient.php

@@ -3,13 +3,12 @@ declare(strict_types=1);
 
 namespace SixShop\TencentCloudSms;
 
-
 use TencentCloud\Common\Credential;
 use TencentCloud\Common\Exception\TencentCloudSDKException;
 use TencentCloud\Common\Profile\ClientProfile;
 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
 {
@@ -45,37 +44,69 @@ class SmsClient
      * @param array|string $phoneNumberSet 手机号码列表
      * @param string $templateId 模板ID
      * @param array|string $templateParamSet 模板参数
-     * @param callable|null $responseCallback 回调函数
+     * @param callable|null $next 中间件回调函数
+     * @param bool $autoCheckError 是否自动检查错误
      * @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;
         $templateParamSet = (array)$templateParamSet;
+        
         if (!isset($this->templateMap[$templateId])) {
             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
     {
         $req = new SendSmsRequest();
         $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);
         return $req;
     }