Просмотр исходного кода

修正serverchan消息推送、代码优化

admin 7 лет назад
Родитель
Сommit
6fae30ec32

+ 30 - 0
app/Components/Curl.php

@@ -0,0 +1,30 @@
+<?php
+
+class Curl
+{
+    /**
+     * @param string $url  请求地址
+     * @param array  $data 数据,如果有数据则用POST请求
+     *
+     * @return mixed
+     */
+    public static function send($url, $data = [])
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($ch, CURLOPT_URL, $url);
+
+        if ($data) {
+            curl_setopt($ch, CURLOPT_POST, 1);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        }
+
+        $res = curl_exec($ch);
+        curl_close($ch);
+
+        return $res;
+    }
+}

+ 2 - 23
app/Components/Namesilo.php

@@ -2,6 +2,7 @@
 
 namespace App\Components;
 
+use Curl;
 use LSS\XML2Array;
 use Log;
 
@@ -84,7 +85,7 @@ class Namesilo
         $content = '请求操作:[' . $operation . '] --- 请求数据:[' . http_build_query($query) . ']';
 
         try {
-            $result = $this->curlRequest(self::$host . $operation . '?' . http_build_query($query));
+            $result = Curl::send(self::$host . $operation . '?' . http_build_query($query));
             $result = XML2Array::createArray($result);
 
             // 出错
@@ -102,26 +103,4 @@ class Namesilo
             return false;
         }
     }
-
-    // 发起一个CURL请求
-    private function curlRequest($url, $data = [])
-    {
-        $ch = curl_init();
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
-        curl_setopt($ch, CURLOPT_URL, $url);
-
-        // 如果data有数据,则用POST请求
-        if ($data) {
-            curl_setopt($ch, CURLOPT_POST, 1);
-            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-        }
-
-        $res = curl_exec($ch);
-        curl_close($ch);
-
-        return $res;
-    }
 }

+ 13 - 25
app/Components/ServerChan.php

@@ -3,9 +3,7 @@
 namespace App\Components;
 
 use App\Http\Models\EmailLog;
-use GuzzleHttp\Client;
-use GuzzleHttp\Psr7;
-use GuzzleHttp\Exception\RequestException;
+use Curl;
 use Log;
 
 class ServerChan
@@ -24,30 +22,20 @@ class ServerChan
      * @param string $content 消息内容
      *
      * @return mixed
-     * @throws \GuzzleHttp\Exception\GuzzleException
      */
     public static function send($title, $content)
     {
-        $client = new Client();
-
-        try {
-            $response = $client->request('GET', 'https://sc.ftqq.com/' . self::$systemConfig['server_chan_key'] . '.send', [
-                'query' => [
-                    'text' => $title,
-                    'desp' => $content
-                ]
-            ]);
-
-            $result = json_decode($response->getBody());
-            if (!$result->errno) {
-                self::addlog($title, $content);
-            } else {
-                self::addlog($title, $content, 0, $result->errmsg);
-            }
-        } catch (RequestException $e) {
-            Log::error(Psr7\str($e->getRequest()));
-            if ($e->hasResponse()) {
-                Log::error(Psr7\str($e->getResponse()));
+        if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
+            try {
+                $response = Curl::send('https://sc.ftqq.com/' . self::$systemConfig['server_chan_key'] . '.send?text=' . $title . '&desp=' . $content);
+                $result = json_decode($response);
+                if (!$result->errno) {
+                    self::addLog($title, $content);
+                } else {
+                    self::addLog($title, $content, 0, $result->errmsg);
+                }
+            } catch (\Exception $e) {
+                Log::error($e);
             }
         }
     }
@@ -62,7 +50,7 @@ class ServerChan
      *
      * @return int
      */
-    private static function addlog($title, $content, $status = 1, $error = '')
+    private static function addLog($title, $content, $status = 1, $error = '')
     {
         $log = new EmailLog();
         $log->type = 2;

+ 1 - 16
app/Console/Commands/AutoCheckNodeStatus.php

@@ -159,7 +159,7 @@ class AutoCheckNodeStatus extends Command
     private function notifyMaster($title, $content, $nodeName, $nodeServer)
     {
         $this->notifyMasterByEmail($title, $content, $nodeName, $nodeServer);
-        $this->notifyMasterByServerchan($title, $content);
+        ServerChan::send($title, $content);
     }
 
     /**
@@ -182,21 +182,6 @@ class AutoCheckNodeStatus extends Command
         }
     }
 
-    /**
-     * 通过ServerChan发微信消息提醒管理员
-     *
-     * @param string $title   消息标题
-     * @param string $content 消息内容
-     *
-     * @throws \GuzzleHttp\Exception\GuzzleException
-     */
-    private function notifyMasterByServerchan($title, $content)
-    {
-        if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
-            ServerChan::send($title, $content);
-        }
-    }
-
     /**
      * 发起一个CURL请求
      *

+ 1 - 16
app/Console/Commands/AutoReportNode.php

@@ -43,7 +43,7 @@ class AutoReportNode extends Command
                     }
                 }
 
-                $this->notifyMasterByServerchan('节点日报', $msg);
+                ServerChan::send('节点日报', $msg);
             }
         }
 
@@ -52,19 +52,4 @@ class AutoReportNode extends Command
 
         Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
     }
-
-    /**
-     * 通过ServerChan发微信消息提醒管理员
-     *
-     * @param string $title   消息标题
-     * @param string $content 消息内容
-     *
-     * @throws \GuzzleHttp\Exception\GuzzleException
-     */
-    private function notifyMasterByServerchan($title, $content)
-    {
-        if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
-            ServerChan::send($title, $content);
-        }
-    }
 }

+ 1 - 1
app/Console/Commands/UserTrafficAbnormalAutoWarning.php

@@ -46,7 +46,7 @@ class UserTrafficAbnormalAutoWarning extends Command
                 $user = User::query()->where('id', $vo->user_id)->first();
 
                 // 通过ServerChan发微信消息提醒管理员
-                if ($vo->totalTraffic > (self::$systemConfig['traffic_ban_value'] * 1024 * 1024 * 1024) && self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
+                if ($vo->totalTraffic > (self::$systemConfig['traffic_ban_value'] * 1024 * 1024 * 1024)) {
                     $traffic = UserTrafficHourly::query()->where('node_id', 0)->where('user_id', $vo->user_id)->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))->selectRaw("user_id, sum(`u`) as totalU, sum(`d`) as totalD, sum(total) as totalTraffic")->first();
 
                     $content = "用户**{$user->username}(ID:{$user->id})**,最近1小时**上行流量:" . flowAutoShow($traffic->totalU) . ",下行流量:" . flowAutoShow($traffic->totalD) . ",共计:" . flowAutoShow($traffic->totalTraffic) . "**。";

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

@@ -84,7 +84,7 @@ class TicketController extends Controller
                 }
 
                 // 通过ServerChan发微信消息提醒管理员
-                if (!Auth::user()->is_admin && self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
+                if (!Auth::user()->is_admin) {
                     ServerChan::send($title, $content);
                 }
 

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

@@ -409,10 +409,7 @@ class UserController extends Controller
                 }
             }
 
-            // 通过ServerChan发微信消息提醒管理员
-            if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
-                ServerChan::send($emailTitle, $content);
-            }
+            ServerChan::send($emailTitle, $content);
 
             return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
         } else {
@@ -457,10 +454,7 @@ class UserController extends Controller
                     }
                 }
 
-                // 通过ServerChan发微信消息提醒管理员
-                if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
-                    ServerChan::send($title, $content);
-                }
+                ServerChan::send($title, $content);
 
                 return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);
             } else {