Files
datahub/backend/app/Controller/Api/V1/OperationLogController.php
T

138 lines
5.9 KiB
PHP
Raw Normal View History

2026-03-17 15:54:53 +08:00
<?php
declare(strict_types=1);
namespace App\Controller\Api\V1;
use App\Controller\AbstractDataController;
use App\Middleware\AuthMiddleware;
use App\Middleware\PermissionMiddleware;
use App\Model\OperationLog;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use OpenApi\Attributes as OA;
use Psr\Http\Message\ResponseInterface;
/**
* 操作日志查看接口
*
* 仅 admin 角色可访问,展示关键业务操作审计日志
*/
#[OA\Tag(name: 'Operation Logs', description: '操作日志查看')]
#[Controller(prefix: "/api/v1/logs/operations")]
#[Middleware(AuthMiddleware::class)]
#[Middleware(PermissionMiddleware::class)]
class OperationLogController extends AbstractDataController
{
protected function getModelClass(): string
{
return OperationLog::class;
}
protected function getListFields(): array
{
return [
'id', 'user_id', 'action', 'target_type', 'target_id',
'description', 'ip', 'created_at',
];
}
protected function getDetailFields(): array
{
return ['*'];
}
protected function getAllowedFilters(): array
{
return [
'user_id' => 'exact',
'action' => 'exact',
'target_type' => 'exact',
'created_at_from' => 'date_from',
'created_at_to' => 'date_to',
];
}
protected function getDefaultSort(): string
{
return 'created_at';
}
/**
* 操作日志列表
*/
#[OA\Get(
path: '/api/v1/logs/operations',
summary: '操作日志列表',
description: '获取操作日志列表,支持分页、按用户/操作类型/目标类型/时间筛选。仅 admin 可访问。',
security: [['bearerAuth' => []]],
tags: ['Operation Logs'],
parameters: [
new OA\Parameter(name: 'page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 1)),
new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15, maximum: 100)),
new OA\Parameter(name: 'user_id', in: 'query', required: false, description: '用户 ID 精确筛选', schema: new OA\Schema(type: 'integer')),
new OA\Parameter(name: 'action', in: 'query', required: false, description: '操作类型精确筛选', schema: new OA\Schema(type: 'string')),
new OA\Parameter(name: 'target_type', in: 'query', required: false, description: '目标类型精确筛选', schema: new OA\Schema(type: 'string')),
new OA\Parameter(name: 'created_at_from', in: 'query', required: false, description: '创建时间起始(含)', schema: new OA\Schema(type: 'string', format: 'date', example: '2026-01-01')),
new OA\Parameter(name: 'created_at_to', in: 'query', required: false, description: '创建时间截止(含)', schema: new OA\Schema(type: 'string', format: 'date', example: '2026-12-31')),
],
responses: [
new OA\Response(
response: 200,
description: '获取成功',
content: new OA\JsonContent(properties: [
new OA\Property(property: 'code', type: 'integer', example: 0),
new OA\Property(property: 'message', type: 'string', example: '获取成功'),
new OA\Property(property: 'data', properties: [
new OA\Property(property: 'items', type: 'array', items: new OA\Items(ref: '#/components/schemas/OperationLogList')),
new OA\Property(property: 'total', type: 'integer', example: 100),
new OA\Property(property: 'page', type: 'integer', example: 1),
new OA\Property(property: 'per_page', type: 'integer', example: 15),
], type: 'object'),
])
),
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
new OA\Response(response: 403, description: '无权限', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
]
)]
#[RequestMapping(path: "", methods: "GET")]
public function index(): ResponseInterface|array
{
return parent::index();
}
/**
* 操作日志详情
*/
#[OA\Get(
path: '/api/v1/logs/operations/{id}',
summary: '操作日志详情',
description: '获取操作日志详情,含完整操作详情 JSON。仅 admin 可访问。',
security: [['bearerAuth' => []]],
tags: ['Operation Logs'],
parameters: [
new OA\Parameter(name: 'id', in: 'path', required: true, description: '操作日志 ID', schema: new OA\Schema(type: 'integer')),
],
responses: [
new OA\Response(
response: 200,
description: '获取成功',
content: new OA\JsonContent(properties: [
new OA\Property(property: 'code', type: 'integer', example: 0),
new OA\Property(property: 'message', type: 'string', example: '获取成功'),
new OA\Property(property: 'data', ref: '#/components/schemas/OperationLogDetail'),
])
),
new OA\Response(response: 401, description: '未认证', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
new OA\Response(response: 403, description: '无权限', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
new OA\Response(response: 404, description: '数据不存在', content: new OA\JsonContent(ref: '#/components/schemas/ErrorResponse')),
]
)]
#[RequestMapping(path: "{id}", methods: "GET")]
public function show(int $id): ResponseInterface|array
{
return parent::show($id);
}
}