初始化项目
This commit is contained in:
126
app/Http/Controllers/System/Admin/Log.php
Normal file
126
app/Http/Controllers/System/Admin/Log.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\System\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\LogRequest;
|
||||
use App\Services\System\LogService;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Exports\GenericExport;
|
||||
|
||||
class Log extends Controller
|
||||
{
|
||||
protected $logService;
|
||||
|
||||
public function __construct(LogService $logService)
|
||||
{
|
||||
$this->logService = $logService;
|
||||
}
|
||||
|
||||
public function index(LogRequest $request)
|
||||
{
|
||||
$result = $this->logService->getList($request->validated());
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => $result
|
||||
]);
|
||||
}
|
||||
|
||||
public function export(LogRequest $request)
|
||||
{
|
||||
$params = $request->validated();
|
||||
$pageSize = $params['page_size'] ?? 10000; // 导出时默认获取更多数据
|
||||
|
||||
// 获取所有符合条件的日志(不分页)
|
||||
$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);
|
||||
}
|
||||
|
||||
public function show(int $id)
|
||||
{
|
||||
$log = $this->logService->getById($id);
|
||||
if (!$log) {
|
||||
return response()->json([
|
||||
'code' => 404,
|
||||
'message' => '日志不存在',
|
||||
'data' => null
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => $log
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id)
|
||||
{
|
||||
$this->logService->delete($id);
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => '删除成功',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function batchDelete(Request $request)
|
||||
{
|
||||
$this->logService->batchDelete($request->input('ids', []));
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => '批量删除成功',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function clearLogs(Request $request)
|
||||
{
|
||||
$days = $request->input('days', 30);
|
||||
$this->logService->clearLogs($days);
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => '清理成功',
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function getStatistics(LogRequest $request)
|
||||
{
|
||||
$statistics = $this->logService->getStatistics($request->validated());
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => $statistics
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user