|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Http\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Facades\Event; |
|
7
|
|
|
|
|
8
|
|
|
use Fabrica\Http\Requests; |
|
9
|
|
|
use Fabrica\Http\Api\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use Fabrica\System\Eloquent\ApiAccessLogs; |
|
12
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
13
|
|
|
|
|
14
|
|
|
class AccessLogsController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Display a listing of the resource. |
|
18
|
|
|
* |
|
19
|
|
|
* @return \Illuminate\Http\Response |
|
20
|
|
|
*/ |
|
21
|
|
|
public function index(Request $request) |
|
22
|
|
|
{ |
|
23
|
|
|
ini_set('memory_limit', '-1'); |
|
24
|
|
|
ini_set('max_execution_time', '0'); |
|
25
|
|
|
|
|
26
|
|
|
$query = ApiAccessLogs::query(); |
|
27
|
|
|
|
|
28
|
|
|
$uid = $request->input('uid'); |
|
29
|
|
|
if (isset($uid) && $uid) { |
|
30
|
|
|
$query = $query->where('user.id', $uid); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$method = $request->input('method'); |
|
34
|
|
|
if (isset($method) && $method) { |
|
35
|
|
|
$query = $query->where('request_method', $method); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$project_key = $request->input('project_key'); |
|
39
|
|
|
if (isset($project_key) && $project_key) { |
|
40
|
|
|
$query = $query->where('project_key', $project_key); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$module = $request->input('module'); |
|
44
|
|
|
if (isset($module) && $module) { |
|
45
|
|
|
$query = $query->where('module', $module); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$request_time = $request->input('request_time'); |
|
49
|
|
|
if (isset($request_time) && $request_time) { |
|
50
|
|
|
if (strpos($request_time, '~') !== false) { |
|
51
|
|
|
$sections = explode('~', $request_time); |
|
52
|
|
|
if ($sections[0]) { |
|
53
|
|
|
$query->where('requested_start_at', '>=', strtotime($sections[0]) * 1000); |
|
54
|
|
|
} |
|
55
|
|
|
if ($sections[1]) { |
|
56
|
|
|
$query->where('requested_start_at', '<=', strtotime($sections[1] . ' 23:59:59') * 1000); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$url = $request->input('request_url'); |
|
62
|
|
|
if (isset($url) && $url) { |
|
63
|
|
|
$query->where('request_url', 'like', '%' . $url . '%'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$request_source_ip = $request->input('request_source_ip'); |
|
67
|
|
|
if (isset($request_source_ip) && $request_source_ip) { |
|
68
|
|
|
$query->where('request_source_ip', $request_source_ip); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$exec_time = $request->input('exec_time'); |
|
72
|
|
|
if (isset($exec_time) && $exec_time) { |
|
73
|
|
|
$flag = substr($exec_time, 0, 1); |
|
74
|
|
|
if ($flag == '-') { |
|
75
|
|
|
$query->where('exec_time', '<=', abs(floatval($exec_time)) * 1000); |
|
76
|
|
|
} |
|
77
|
|
|
else |
|
78
|
|
|
{ |
|
79
|
|
|
$query->where('exec_time', '>=', abs(floatval($exec_time)) * 1000); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// get total |
|
84
|
|
|
$total = $query->count(); |
|
85
|
|
|
$query->orderBy('_id', 'desc'); |
|
86
|
|
|
$page_size = $request->input('limit') ? intval($request->input('limit')) : 100; |
|
87
|
|
|
$page = $request->input('page') ?: 1; |
|
88
|
|
|
$query = $query->skip($page_size * ($page - 1))->take($page_size); |
|
89
|
|
|
|
|
90
|
|
|
$logs = $query->get(); |
|
91
|
|
|
|
|
92
|
|
|
$from = $request->input('from'); |
|
93
|
|
|
if (isset($from) && $from == 'export') { |
|
94
|
|
|
$this->export($logs); |
|
95
|
|
|
exit(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return response()->json([ 'ecode' => 0, 'data' => $logs, 'options' => [ 'total' => $total, 'sizePerPage' => $page_size ] ]); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* export xls for access logs |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $issues |
|
|
|
|
|
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function export($logs) |
|
108
|
|
|
{ |
|
109
|
|
|
set_time_limit(0); |
|
110
|
|
|
|
|
111
|
|
|
$headers = [ |
|
112
|
|
|
'用户', |
|
113
|
|
|
'方法', |
|
114
|
|
|
'Url', |
|
115
|
|
|
'模块', |
|
116
|
|
|
'请求开始时间', |
|
117
|
|
|
'请求结束时间', |
|
118
|
|
|
'请求时长(毫秒)', |
|
119
|
|
|
'来源IP', |
|
120
|
|
|
'User-Agent', |
|
121
|
|
|
'Body' |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
$file_name = 'access-logs'; |
|
125
|
|
|
Excel::create( |
|
126
|
|
|
$file_name, function ($excel) use ($headers, $logs) { |
|
127
|
|
|
$excel->sheet( |
|
128
|
|
|
'Sheetname', function ($sheet) use ($headers, $logs) { |
|
129
|
|
|
$sheet->appendRow($headers); |
|
130
|
|
|
foreach ($logs as $log) |
|
131
|
|
|
{ |
|
132
|
|
|
$tmp = []; |
|
133
|
|
|
$tmp[] = $log->user ? $log->user['name'] : ''; |
|
134
|
|
|
$tmp[] = $log->request_method ?: ''; |
|
135
|
|
|
$tmp[] = $log->request_url ?: ''; |
|
136
|
|
|
$tmp[] = $log->module ?: ''; |
|
137
|
|
|
$tmp[] = $log->requested_start_at ? date('Y-m-d h:i:s', intval($log->requested_start_at / 1000)): ''; |
|
138
|
|
|
$tmp[] = $log->requested_end_at ? date('Y-m-d h:i:s', intval($log->requested_end_at / 1000)) : ''; |
|
139
|
|
|
$tmp[] = $log->exec_time ?: ''; |
|
140
|
|
|
$tmp[] = $log->request_source_ip ?: ''; |
|
141
|
|
|
$tmp[] = $log->request_user_agent ?: ''; |
|
142
|
|
|
$tmp[] = $log->request_body ? json_encode($log->request_body) : ''; |
|
143
|
|
|
$sheet->appendRow($tmp); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
)->download('xls'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: