Browse Source

隐藏报错

xieruidong 2 years ago
parent
commit
c77a1a03e0

+ 4 - 1
.env.sample

@@ -15,4 +15,7 @@ password=123456
 prefix=videopoint
 
 [sms]
-testcode=1111
+testcode=1111
+
+[company_wechat]
+webhook=

+ 1 - 0
application/api/controller/Index.php

@@ -19,5 +19,6 @@ class Index extends Api
     protected $noNeedLogin = '*';
     protected $noNeedRight = '*';
     public function test(){
+        aaa();
     }
 }

+ 23 - 20
application/api/library/ExceptionHandle.php

@@ -2,6 +2,7 @@
 
 namespace app\api\library;
 
+use app\service\company_wechat\Webhook;
 use app\UserException;
 use Exception;
 use think\db\exception\DataNotFoundException;
@@ -20,30 +21,32 @@ class ExceptionHandle extends Handle
     ];
     public function render(Exception $e)
     {
+        $statuscode = $code = 0;
+        $msg = '服务器错误';
+        // 验证异常
+        if ($e instanceof \think\exception\ValidateException) {
+            $code = 0;
+            $statuscode = 200;
+            $msg = $e->getError();
+        }
+        // Http异常
+        elseif ($e instanceof \think\exception\HttpException) {
+            $statuscode = $code = $e->getStatusCode();
+        }
+        elseif ($e instanceof UserException) {
+            $code = 0;
+            $msg = $e->getMessage();
+            $statuscode = 200;
+        }
+        elseif ($e instanceof ModelNotFoundException) {
+            $msg = '查找的数据不存在';
+        }else{
+            Webhook::push($e);
+        }
         // 在生产环境下返回code信息
         //if (!\think\Config::get('app_debug')) {
         if(request()->isAjax() || strpos(request()->header('Accept'),'/json')!==false) {
-            $statuscode = $code = 0;
             //$msg = 'An error occurred';
-            $msg = '服务器错误';
-            // 验证异常
-            if ($e instanceof \think\exception\ValidateException) {
-                $code = 0;
-                $statuscode = 200;
-                $msg = $e->getError();
-            }
-            // Http异常
-            if ($e instanceof \think\exception\HttpException) {
-                $statuscode = $code = $e->getStatusCode();
-            }
-            if ($e instanceof UserException) {
-                $code = 0;
-                $msg = $e->getMessage();
-                $statuscode = 200;
-            }
-            if ($e instanceof ModelNotFoundException) {
-                $msg = '查找的数据不存在';
-            }
             return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
         }
         //}

+ 16 - 0
application/extra/bugpush.php

@@ -0,0 +1,16 @@
+<?php
+/*
+ * +----------------------------------------------------------------------+
+ * |                          xiegeng admin system                              |
+ * +----------------------------------------------------------------------+
+ * |    Administrator-7:19-2022-PhpStorm
+ * +----------------------------------------------------------------------+
+ * | Author: xiegeng <957723538@qq.com>                                   |
+ * | FILENAME: bugpush.php                                               |
+ * +----------------------------------------------------------------------+
+ */
+return [
+    'company_wechat'=>[
+        'webhook'=>\think\Env::get('company_wechat.webhook')
+    ]
+];

+ 41 - 0
application/service/company_wechat/Webhook.php

@@ -0,0 +1,41 @@
+<?php
+/*
+ * +----------------------------------------------------------------------+
+ * |                          xiegeng admin system                              |
+ * +----------------------------------------------------------------------+
+ * |    Administrator-7:24-2022-PhpStorm
+ * +----------------------------------------------------------------------+
+ * | Author: xiegeng <957723538@qq.com>                                   |
+ * | FILENAME: ComWechatWebhook.php                                               |
+ * +----------------------------------------------------------------------+
+ */
+namespace app\service\company_wechat;
+
+use think\Cache;
+use think\Db;
+
+class Webhook{
+    public static function push(\Throwable $e){
+        $message=$e->getMessage();
+        $md5Msg=md5($message);
+        $cacheName=sprintf('bug_report_%s',$md5Msg);
+        $now=time();
+        $table=Db::name('bug_report');
+        $exists=$table
+            ->where('hash',$md5Msg)
+            ->whereBetween('create_time',[strtotime(date('Y-m-d 00:00:00',$now)),strtotime('+1day',strtotime(date('Y-m-d 00:00:00',$now)))])
+            ->value('id');
+        if(!$exists){
+            $table->insert([
+                'hash'=>$md5Msg,
+                'content'=>json_encode([
+                    'msg'=>$message,
+                    'file'=>$e->getFile(),
+                    'line'=>$e->getLine(),
+                    'trace'=>$e->getTrace(),
+                ],256),
+                'create_time'=>$now
+            ]);
+        }
+    }
+}

+ 43 - 0
commands/bug_report.php

@@ -0,0 +1,43 @@
+<?php
+
+use app\admin\model\Admin;
+use think\Db;
+use think\Env;
+use Workerman\Lib\Timer;
+use Workerman\Worker as WO;
+
+class BugReport extends Com{
+    public static function run(){
+        $work=self::newWorker();
+        $work->onWorkerStart=function (WO $worker){
+            $file=ROOT_PATH.'/webhook';
+            $table= Db::name('bug_report');
+            Timer::add(3,function ()use ($worker,$file,$table){
+                if(file_exists($file)){
+                    $url=preg_replace('/\s+/','',file_get_contents(ROOT_PATH.'/webhook'));
+                    var_dump($url);
+                    if($url){
+                        $bug=$table->where('send',0)->order('id','desc')->find();
+                        if($bug){
+                            $bug->where('id',$bug['id'])->update(['send'=>1]);
+                            $bug['content']=json_decode($bug['content'],true);
+                            $curl=(new \GuzzleHttp\Client())
+                                ->post($url,[
+                                    'json'=>[
+                                        'msgtype'=>'markdown',
+                                        'markdown'=>[
+                                            'content'=>"ERROR:<font color='warning'>{$bug['content']['msg']}</font>\n
+                                            >FILE:<font color='comment'>{$bug['content']['file']}</font>
+                                            >LINE:<font color='comment'>{$bug['content']['line']}</font>
+                                            "
+                                        ]
+                                    ]
+                                ]);
+                        }
+                    }
+                }
+            });
+        };
+    }
+}
+BugReport::run();

+ 4 - 0
webhook

@@ -0,0 +1,4 @@
+https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f9b8f6c8-7cb6-4176-908e-5687d085927a
+
+
+

+ 4 - 3
work

@@ -31,8 +31,9 @@ abstract class Com{
     }
 }
 
-foreach (glob(__DIR__.'/commands/*.php') as $file){
+/*foreach (glob(__DIR__.'/commands/*.php') as $file){
     require $file;
-}
+}*/
+require __DIR__.'/commands/bug_report.php';
 
-WO::runAll();
+WO::runAll();