Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

宁成龙 / dataIntegration

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • dataIntegration
  • app
  • Http
  • Middleware
  • Monitor.php
  • 宁成龙's avatar
    Initial commit · 7166432c
    宁成龙 committed 3 years ago
    7166432c
Monitor.php 4.73 KB
Edit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php

namespace App\Http\Middleware;

use Error;
use Closure;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Psy\Exception\ErrorException;
use Psy\Exception\FatalErrorException;

class Monitor
{
    /**
     * The App container
     *
     * @var Container
     */
    protected $container;

    /**
     * The Monitor Client
     *
     * @var
     */
    protected $monitor;

    /**
     * Create a new middleware instance.
     *
     * @param Container $container
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
//        dump("monitorDing");
        $enabled = config('monitorDing.enabled');
        try {
            $response = $next($request);
        } catch (Exception $e) {
            $response = $this->handleException($request, $e);
            $enabled && $this->sendText(sprintf("文件:%s (%s 行) 内容:%s", $e->getFile(), $e->getLine(), $e->getMessage()));

        } catch (Error $error) {
            $e = new FatalErrorException($error);
            $response = $this->handleException($request, $e);
            $enabled && $this->sendText(sprintf("文件:%s (%s 行) 内容:%s", $e->getFile(), $e->getLine(), $e->getMessage()));
        } catch (ErrorException $error) {
            $e = new FatalErrorException($error);
            $response = $this->handleException($request, $e);
            $enabled && $this->sendText(sprintf("文件:%s (%s 行) 内容:%s", $e->getFile(), $e->getLine(), $e->getMessage()));
        } finally {
            if ($response->getStatusCode() == '500' && (isset($response->exception) && $response->exception && $response->exception !== null)) {
                $sysName = config('monitorDing.web_name');

                if (strpos($_SERVER['HTTP_HOST'], 'liexin') !== false) {
                    $sysName = '本地' . $sysName;
                } else {
                    $sysName = '外网' . $sysName;
                }

                $this->sendText(substr($sysName . ":" . $response->exception, 0,
                        500) . ',请求数据:' . json_encode($request->input()) . "---[更多详情请看日志]");
            }
        }
        return $response;
    }

    /**
     * Handle the given exception.
     *
     * (Copy from Illuminate\Routing\Pipeline by Taylor Otwell)
     *
     * @param $passable
     * @param Exception $e
     * @return mixed
     * @throws Exception
     */
    protected function handleException($passable, Exception $e)
    {
        if (!$this->container->bound(ExceptionHandler::class) || !$passable instanceof Request) {
            throw $e;
        }

        $handler = $this->container->make(ExceptionHandler::class);

        $handler->report($e);

        return $handler->render($passable, $e);
    }


    /**
     * 发送文本类型的消息
     *
     * @param $content string 消息内容
     * @param array $atMobiles 被@人的手机号
     * @param bool $isAtAll 是否 @ 所有人
     * @throws SendErrorException
     */
    public function sendText($content, $atMobiles = [], $isAtAll = false)
    {
        $params = [
            'msgtype' => 'text',
            'text' => [
                'content' => $content,
            ],
            'at' => [
                'atMobiles' => $atMobiles,
                'isAtAll' => $isAtAll
            ]
        ];
        $this->send($params);
    }

    /**
     * 发送
     * @param array $params 请求需要的参数
     * @throws SendErrorException
     */
    private function send($params = [])
    {
        if (!config('monitorDing.enabled')) {
            \Log::info('~~ Monitor Ding ~~');
            \Log::info($params);
        } else {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, config("monitorDing.webhook"));
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            if (config()) {
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            }
            $data = json_decode(curl_exec($ch), true);
            curl_close($ch);

            if ($data['errcode']) {
//                throw new SendErrorException($data['errmsg']);
            }
        }

    }
}