更新
This commit is contained in:
@@ -85,7 +85,7 @@ public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:50',
|
||||
'parent_id' => 'nullable|integer|exists:auth_departments,id',
|
||||
'parent_id' => 'nullable|integer',
|
||||
'leader' => 'nullable|string|max:50',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'sort' => 'nullable|integer|min:0',
|
||||
@@ -108,7 +108,7 @@ public function update(Request $request, $id)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'nullable|string|max:50',
|
||||
'parent_id' => 'nullable|integer|exists:auth_departments,id',
|
||||
'parent_id' => 'nullable|integer',
|
||||
'leader' => 'nullable|string|max:50',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'sort' => 'nullable|integer|min:0',
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\LogRequest;
|
||||
use App\Services\System\LogService;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Exports\GenericExport;
|
||||
|
||||
@@ -30,39 +31,9 @@ public function index(LogRequest $request)
|
||||
public function export(LogRequest $request)
|
||||
{
|
||||
$params = $request->validated();
|
||||
$pageSize = $params['page_size'] ?? 10000; // 导出时默认获取更多数据
|
||||
$filePath = $this->logService->export($params);
|
||||
|
||||
// 获取所有符合条件的日志(不分页)
|
||||
$query = $this->logService->getListQuery($params);
|
||||
$logs = $query->limit($pageSize)->get();
|
||||
|
||||
// 准备导出数据
|
||||
$headers = [
|
||||
'ID', '用户名', '模块', '操作', '请求方法', 'URL', 'IP地址',
|
||||
'状态码', '状态', '错误信息', '执行时间(ms)', '创建时间'
|
||||
];
|
||||
|
||||
$data = [];
|
||||
foreach ($logs as $log) {
|
||||
$data[] = [
|
||||
$log->id,
|
||||
$log->username,
|
||||
$log->module,
|
||||
$log->action,
|
||||
$log->method,
|
||||
$log->url,
|
||||
$log->ip,
|
||||
$log->status_code,
|
||||
$log->status === 'success' ? '成功' : '失败',
|
||||
$log->error_message ?? '-',
|
||||
$log->execution_time,
|
||||
$log->created_at->format('Y-m-d H:i:s'),
|
||||
];
|
||||
}
|
||||
|
||||
$filename = '系统操作日志_' . date('YmdHis') . '.xlsx';
|
||||
|
||||
return Excel::download(new GenericExport($headers, $data), $filename);
|
||||
return response()->download($filePath)->deleteFileAfterSend(true);
|
||||
}
|
||||
|
||||
public function show(int $id)
|
||||
@@ -93,7 +64,7 @@ public function destroy(int $id)
|
||||
]);
|
||||
}
|
||||
|
||||
public function batchDelete(Request $request)
|
||||
public function batchDelete(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$this->logService->batchDelete($request->input('ids', []));
|
||||
return response()->json([
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
class LogRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
* Determine if user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -19,7 +19,7 @@ public function authorize(): bool
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
* Get validation rules that apply to request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -34,6 +34,7 @@ public function rules(): array
|
||||
'username' => 'nullable|string|max:50',
|
||||
'module' => 'nullable|string|max:50',
|
||||
'action' => 'nullable|string|max:100',
|
||||
'method' => 'nullable|in:GET,POST,PUT,DELETE,PATCH',
|
||||
'status' => 'nullable|in:success,error',
|
||||
'start_date' => 'nullable|date',
|
||||
'end_date' => 'nullable|date|after_or_equal:start_date',
|
||||
@@ -77,6 +78,7 @@ public function messages(): array
|
||||
'username.max' => '用户名最多50个字符',
|
||||
'module.max' => '模块名最多50个字符',
|
||||
'action.max' => '操作名最多100个字符',
|
||||
'method.in' => '请求方式必须是 GET、POST、PUT、DELETE 或 PATCH',
|
||||
'status.in' => '状态值必须是 success 或 error',
|
||||
'start_date.date' => '开始日期格式不正确',
|
||||
'end_date.date' => '结束日期格式不正确',
|
||||
@@ -121,7 +123,7 @@ protected function failedValidation(Validator $validator): void
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
* Prepare for validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
||||
@@ -48,7 +48,7 @@ public function getListQuery(array $params)
|
||||
*/
|
||||
protected function buildQuery(array $params)
|
||||
{
|
||||
$query = Log::query()->with('user:id,name,username');
|
||||
$query = Log::query()->with('user:id,username');
|
||||
|
||||
if (!empty($params['user_id'])) {
|
||||
$query->where('user_id', $params['user_id']);
|
||||
@@ -108,6 +108,14 @@ public function getStatistics(array $params = []): array
|
||||
{
|
||||
$query = Log::query();
|
||||
|
||||
if (!empty($params['method'])) {
|
||||
$query->where('method', $params['method']);
|
||||
}
|
||||
|
||||
if (!empty($params['status'])) {
|
||||
$query->where('status', $params['status']);
|
||||
}
|
||||
|
||||
if (!empty($params['start_date']) && !empty($params['end_date'])) {
|
||||
$query->whereBetween('created_at', [$params['start_date'], $params['end_date']]);
|
||||
}
|
||||
@@ -116,10 +124,74 @@ public function getStatistics(array $params = []): array
|
||||
$successCount = (clone $query)->where('status', 'success')->count();
|
||||
$errorCount = (clone $query)->where('status', 'error')->count();
|
||||
|
||||
// 计算平均响应时间
|
||||
$avgTime = (clone $query)->avg('execution_time');
|
||||
$avgTime = $avgTime ? round($avgTime, 2) : 0;
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
'success' => $successCount,
|
||||
'error' => $errorCount,
|
||||
'avg_time' => $avgTime,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出日志
|
||||
*
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
public function export(array $params): string
|
||||
{
|
||||
$query = $this->buildQuery($params);
|
||||
$query->orderBy('created_at', 'desc');
|
||||
|
||||
$logs = $query->limit(10000)->get();
|
||||
|
||||
// 创建导出数据
|
||||
$data = [];
|
||||
foreach ($logs as $log) {
|
||||
$data[] = [
|
||||
'ID' => $log->id,
|
||||
'用户名' => $log->username,
|
||||
'模块' => $log->module,
|
||||
'操作' => $log->action,
|
||||
'请求方式' => $log->method,
|
||||
'URL' => $log->url,
|
||||
'IP地址' => $log->ip,
|
||||
'状态码' => $log->status_code,
|
||||
'状态' => $log->status === 'success' ? '成功' : '失败',
|
||||
'执行时间' => $log->execution_time . 'ms',
|
||||
'创建时间' => $log->created_at,
|
||||
];
|
||||
}
|
||||
|
||||
// 生成CSV文件
|
||||
$filename = '系统日志_' . date('YmdHis') . '.csv';
|
||||
$filepath = storage_path('app/exports/' . $filename);
|
||||
|
||||
// 确保目录存在
|
||||
if (!file_exists(dirname($filepath))) {
|
||||
mkdir(dirname($filepath), 0755, true);
|
||||
}
|
||||
|
||||
$file = fopen($filepath, 'w');
|
||||
// 添加BOM以支持Excel中文显示
|
||||
fprintf($file, chr(0xEF) . chr(0xBB) . chr(0xBF));
|
||||
|
||||
// 写入表头
|
||||
if (!empty($data)) {
|
||||
fputcsv($file, array_keys($data[0]));
|
||||
|
||||
// 写入数据
|
||||
foreach ($data as $row) {
|
||||
fputcsv($file, $row);
|
||||
}
|
||||
}
|
||||
|
||||
fclose($file);
|
||||
|
||||
return $filepath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user