1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
4
|
|
|
// | ThinkLibrary 6.0 for ThinkPhP 6.0 |
5
|
|
|
// +---------------------------------------------------------------------- |
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
7
|
|
|
// +---------------------------------------------------------------------- |
8
|
|
|
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
9
|
|
|
// +---------------------------------------------------------------------- |
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
11
|
|
|
// +---------------------------------------------------------------------- |
12
|
|
|
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
13
|
|
|
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
14
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
15
|
|
|
// +---------------------------------------------------------------------- |
16
|
|
|
|
17
|
|
|
namespace DtApp\ThinkLibrary\exception; |
18
|
|
|
|
19
|
|
|
use DtApp\ThinkLibrary\service\curl\HttpService; |
20
|
|
|
use DtApp\ThinkLibrary\service\DingTalkService; |
21
|
|
|
use DtApp\ThinkLibrary\service\QqWryService; |
22
|
|
|
use DtApp\ThinkLibrary\service\wechat\QyService; |
23
|
|
|
use think\exception\Handle; |
24
|
|
|
use think\exception\HttpException; |
25
|
|
|
use think\exception\ValidateException; |
26
|
|
|
use think\Request; |
27
|
|
|
use think\Response; |
28
|
|
|
use Throwable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 异常处理接管 |
32
|
|
|
* Class ThinkException |
33
|
|
|
* @package DtApp\ThinkLibrary\exception |
34
|
|
|
*/ |
35
|
|
|
class ThinkException extends Handle |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* 异常处理接管 |
39
|
|
|
* @param Request $request |
40
|
|
|
* @param Throwable $e |
41
|
|
|
* @return Response |
42
|
|
|
* @throws DtaException |
43
|
|
|
*/ |
44
|
|
|
public function render($request, Throwable $e): Response |
45
|
|
|
{ |
46
|
|
|
// 参数验证错误 |
47
|
|
|
if ($e instanceof ValidateException) { |
48
|
|
|
return json($e->getError(), 422); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// 请求异常 |
52
|
|
|
if ($e instanceof HttpException && $request->isAjax()) { |
53
|
|
|
return response($e->getMessage(), $e->getStatusCode()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->show($e->getMessage()); |
57
|
|
|
|
58
|
|
|
// 其他错误交给系统处理 |
59
|
|
|
return parent::render($request, $e); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $msg |
64
|
|
|
* @return bool |
65
|
|
|
* @throws DtaException |
66
|
|
|
*/ |
67
|
|
|
private function show($msg): bool |
68
|
|
|
{ |
69
|
|
|
if (empty($msg)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
$nt = config('dtapp.exception.type', ''); |
73
|
|
|
if (!empty($nt) && $nt === 'dingtalk') { |
74
|
|
|
$access_token = config('dtapp.exception.dingtalk.access_token', ''); |
75
|
|
|
if (!empty($access_token)) { |
76
|
|
|
return DingTalkService::instance() |
77
|
|
|
->accessToken($access_token) |
|
|
|
|
78
|
|
|
->text($msg); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
if (!empty($nt) && $nt === 'qyweixin') { |
82
|
|
|
$key = config('dtapp.exception.qyweixin.key', ''); |
83
|
|
|
if (!empty($key)) { |
84
|
|
|
return QyService::instance() |
85
|
|
|
->key($key) |
|
|
|
|
86
|
|
|
->text($msg); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if (!empty($nt) && $nt === 'wechat') { |
90
|
|
|
$openid = config('dtapp.exception.wechat.openid', ''); |
91
|
|
|
$ip = config('dtapp.exception.wechat.ip', '未配置'); |
92
|
|
|
$seip = get_ip(); |
93
|
|
|
$ipinfo = QqWryService::instance()->getLocation($seip); |
94
|
|
|
if (!isset($ipinfo['location_all'])) { |
95
|
|
|
$ipinfo['location_all'] = ''; |
96
|
|
|
} |
97
|
|
|
if (!empty($openid)) { |
98
|
|
|
return HttpService::instance() |
99
|
|
|
->url("https://api.dtapp.net/v1/wechatmp/tmplmsgWebError/openid/{$openid}") |
100
|
|
|
->post() |
101
|
|
|
->data([ |
102
|
|
|
'domain' => request()->domain(), |
103
|
|
|
'url' => request()->url(), |
104
|
|
|
'node' => config('dtapp.exception.wechat.node', ''), |
105
|
|
|
'info' => "ServerIp:" . $ip . ";CdnIp:" . $_SERVER['REMOTE_ADDR'] . ";ClientIp:" . get_ip(), |
106
|
|
|
'ip' => $ipinfo['location_all'], |
107
|
|
|
'error' => base64_encode($msg) |
108
|
|
|
]) |
109
|
|
|
->toArray(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|