Pārlūkot izejas kodu

serverChan优化

bingo 7 gadi atpakaļ
vecāks
revīzija
b9937baaaf

+ 50 - 4
app/Components/ServerChan.php

@@ -2,6 +2,8 @@
 
 
 namespace App\Components;
 namespace App\Components;
 
 
+use App\Http\Models\Config;
+use App\Http\Models\EmailLog;
 use GuzzleHttp\Client;
 use GuzzleHttp\Client;
 use GuzzleHttp\Psr7;
 use GuzzleHttp\Psr7;
 use GuzzleHttp\Exception\RequestException;
 use GuzzleHttp\Exception\RequestException;
@@ -9,25 +11,36 @@ use Log;
 
 
 class ServerChan
 class ServerChan
 {
 {
+    protected static $config;
+
+    function __construct()
+    {
+        self::$config = $this->systemConfig();
+    }
+
     /**
     /**
      * @param string $title 消息标题
      * @param string $title 消息标题
      * @param string $content 消息内容
      * @param string $content 消息内容
-     * @param string $key ServerChan上申请的SCKEY
      * @return string
      * @return string
      */
      */
-    public function send($title, $content, $key)
+    public function send($title, $content)
     {
     {
         $client = new Client();
         $client = new Client();
 
 
         try {
         try {
-            $response = $client->request('GET', 'https://sc.ftqq.com/' . $key . '.send', [
+            $response = $client->request('GET', 'https://sc.ftqq.com/' . self::$config['server_chan_key'] . '.send', [
                 'query' => [
                 'query' => [
                     'text' => $title,
                     'text' => $title,
                     'desp' => $content
                     'desp' => $content
                 ]
                 ]
             ]);
             ]);
 
 
-            return json_decode($response->getBody());
+            $result = json_decode($response->getBody());
+            if ($result->errno > 0) {
+                $this->sendEmailLog(1, '[ServerChan]' . $title, $content);
+            } else {
+                $this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
+            }
         } catch (RequestException $e) {
         } catch (RequestException $e) {
             Log::error(Psr7\str($e->getRequest()));
             Log::error(Psr7\str($e->getRequest()));
             if ($e->hasResponse()) {
             if ($e->hasResponse()) {
@@ -35,4 +48,37 @@ class ServerChan
             }
             }
         }
         }
     }
     }
+
+    /**
+     * 写入邮件发送日志
+     *
+     * @param int    $user_id 用户ID
+     * @param string $title   标题
+     * @param string $content 内容
+     * @param int    $status  投递状态
+     * @param string $error   投递失败时记录的异常信息
+     */
+    private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
+    {
+        $emailLogObj = new EmailLog();
+        $emailLogObj->user_id = $user_id;
+        $emailLogObj->title = $title;
+        $emailLogObj->content = $content;
+        $emailLogObj->status = $status;
+        $emailLogObj->error = $error;
+        $emailLogObj->created_at = date('Y-m-d H:i:s');
+        $emailLogObj->save();
+    }
+
+    // 系统配置
+    private function systemConfig()
+    {
+        $config = Config::query()->get();
+        $data = [];
+        foreach ($config as $vo) {
+            $data[$vo->name] = $vo->value;
+        }
+
+        return $data;
+    }
 }
 }

+ 1 - 6
app/Console/Commands/AutoCheckNodeStatusJob.php

@@ -55,12 +55,7 @@ class AutoCheckNodeStatusJob extends Command
                     // 通过ServerChan发微信消息提醒管理员
                     // 通过ServerChan发微信消息提醒管理员
                     if ($config['is_server_chan'] && $config['server_chan_key']) {
                     if ($config['is_server_chan'] && $config['server_chan_key']) {
                         $serverChan = new ServerChan();
                         $serverChan = new ServerChan();
-                        $result = $serverChan->send($title, $content, $config['server_chan_key']);
-                        if ($result->errno > 0) {
-                            $this->sendEmailLog(1, '[ServerChan]' . $title, $content);
-                        } else {
-                            $this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
-                        }
+                        $serverChan->send($title, $content);
                     }
                     }
 
 
                     // 写入发信缓存
                     // 写入发信缓存

+ 1 - 6
app/Http/Controllers/TicketController.php

@@ -82,12 +82,7 @@ class TicketController extends Controller
                 // 通过ServerChan发微信消息提醒管理员
                 // 通过ServerChan发微信消息提醒管理员
                 if (!$user['is_admin'] && self::$config['is_server_chan'] && self::$config['server_chan_key']) {
                 if (!$user['is_admin'] && self::$config['is_server_chan'] && self::$config['server_chan_key']) {
                     $serverChan = new ServerChan();
                     $serverChan = new ServerChan();
-                    $result = $serverChan->send($title, $content, self::$config['server_chan_key']);
-                    if ($result->errno > 0) {
-                        $this->sendEmailLog(1, '[ServerChan]' . $title, $content);
-                    } else {
-                        $this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
-                    }
+                    $serverChan->send($title, $content);
                 }
                 }
 
 
                 return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);
                 return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);

+ 2 - 12
app/Http/Controllers/UserController.php

@@ -370,12 +370,7 @@ class UserController extends Controller
             // 通过ServerChan发微信消息提醒管理员
             // 通过ServerChan发微信消息提醒管理员
             if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
             if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
                 $serverChan = new ServerChan();
                 $serverChan = new ServerChan();
-                $result = $serverChan->send($emailTitle, $content, self::$config['server_chan_key']);
-                if ($result->errno > 0) {
-                    $this->sendEmailLog(1, '[ServerChan]' . $emailTitle, $content);
-                } else {
-                    $this->sendEmailLog(1, '[ServerChan]' . $emailTitle, $content, 0, $result->errmsg);
-                }
+                $serverChan->send($emailTitle, $content);
             }
             }
 
 
             return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
             return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
@@ -420,12 +415,7 @@ class UserController extends Controller
                 // 通过ServerChan发微信消息提醒管理员
                 // 通过ServerChan发微信消息提醒管理员
                 if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
                 if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
                     $serverChan = new ServerChan();
                     $serverChan = new ServerChan();
-                    $result = $serverChan->send($title, $content, self::$config['server_chan_key']);
-                    if ($result->errno > 0) {
-                        $this->sendEmailLog(1, '[ServerChan]' . $title, $content);
-                    } else {
-                        $this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
-                    }
+                    $serverChan->send($title, $content);
                 }
                 }
 
 
                 return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);
                 return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);