ThinkException.php 4.2 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19 20 21
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\exception;

李光春's avatar
李光春 已提交
22
use DtApp\ThinkLibrary\service\curl\HttpService;
李光春's avatar
李光春 已提交
23
use DtApp\ThinkLibrary\service\DingTalkService;
李光春's avatar
李光春 已提交
24
use DtApp\ThinkLibrary\service\QqWryService;
李光春's avatar
李光春 已提交
25
use DtApp\ThinkLibrary\service\wechat\QyService;
李光春's avatar
李光春 已提交
26
use think\exception\Handle;
李光春's avatar
李光春 已提交
27
use think\exception\HttpException;
李光春's avatar
李光春 已提交
28 29 30 31 32
use think\exception\ValidateException;
use think\Request;
use think\Response;
use Throwable;

李光春's avatar
李光春 已提交
33 34 35 36 37
/**
 * 异常处理接管
 * Class ThinkException
 * @package DtApp\ThinkLibrary\exception
 */
李光春's avatar
李光春 已提交
38 39 40 41 42 43 44
class ThinkException extends Handle
{
    /**
     * 异常处理接管
     * @param Request $request
     * @param Throwable $e
     * @return Response
李光春's avatar
李光春 已提交
45
     * @throws DtaException
李光春's avatar
李光春 已提交
46 47 48 49
     */
    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
李光春's avatar
李光春 已提交
50 51 52
        if ($e instanceof ValidateException) {
            return json($e->getError(), 422);
        }
李光春's avatar
李光春 已提交
53 54

        // 请求异常
李光春's avatar
李光春 已提交
55 56 57
        if ($e instanceof HttpException && $request->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }
李光春's avatar
李光春 已提交
58 59 60 61 62 63

        $this->show($e->getMessage());

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
李光春's avatar
李光春 已提交
64

李光春's avatar
李光春 已提交
65 66 67
    /**
     * @param $msg
     * @return bool
李光春's avatar
李光春 已提交
68
     * @throws DtaException
李光春's avatar
李光春 已提交
69
     */
李光春's avatar
李光春 已提交
70
    private function show($msg): bool
李光春's avatar
李光春 已提交
71
    {
李光春's avatar
李光春 已提交
72 73 74
        if (empty($msg)) {
            return true;
        }
李光春's avatar
李光春 已提交
75
        $nt = config('dtapp.exception.type', '');
李光春's avatar
李光春 已提交
76
        if (!empty($nt) && $nt === 'dingtalk') {
李光春's avatar
李光春 已提交
77
            $access_token = config('dtapp.exception.dingtalk.access_token', '');
李光春's avatar
李光春 已提交
78 79 80 81 82
            if (!empty($access_token)) {
                return DingTalkService::instance()
                    ->accessToken($access_token)
                    ->text($msg);
            }
李光春's avatar
李光春 已提交
83
        }
李光春's avatar
李光春 已提交
84
        if (!empty($nt) && $nt === 'qyweixin') {
李光春's avatar
李光春 已提交
85
            $key = config('dtapp.exception.qyweixin.key', '');
李光春's avatar
李光春 已提交
86 87 88 89 90
            if (!empty($key)) {
                return QyService::instance()
                    ->key($key)
                    ->text($msg);
            }
李光春's avatar
李光春 已提交
91 92
        }
        if (!empty($nt) && $nt === 'wechat') {
李光春's avatar
李光春 已提交
93
            $openid = config('dtapp.exception.wechat.openid', '');
李光春's avatar
李光春 已提交
94 95 96
            $ip = config('dtapp.exception.wechat.ip', '未配置');
            $seip = get_ip();
            $ipinfo = QqWryService::instance()->getLocation($seip);
李光春's avatar
李光春 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            if (!isset($ipinfo['location_all'])) {
                $ipinfo['location_all'] = '';
            }
            if (!empty($openid)) {
                return HttpService::instance()
                    ->url("https://api.dtapp.net/v1/wechatmp/tmplmsgWebError/openid/{$openid}")
                    ->post()
                    ->data([
                        'domain' => request()->domain(),
                        'url' => request()->url(),
                        'node' => config('dtapp.exception.wechat.node', ''),
                        'info' => "ServerIp:" . $ip . ";CdnIp:" . $_SERVER['REMOTE_ADDR'] . ";ClientIp:" . get_ip(),
                        'ip' => $ipinfo['location_all'],
                        'error' => base64_encode($msg)
                    ])
                    ->toArray();
            }
李光春's avatar
李光春 已提交
114
        }
李光春's avatar
李光春 已提交
115
        return true;
李光春's avatar
李光春 已提交
116 117
    }
}