ListUserActionLogs

This commit is contained in:
2025-10-18 15:47:13 +08:00
parent fab26ffca4
commit bcdcaa5631
5 changed files with 170 additions and 28 deletions

View File

@@ -263,3 +263,53 @@ func (c *Controller) ListPendingCollections(ctx *gin.Context) {
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
}
// ListUserActionLogs godoc
// @Summary 获取用户操作日志列表
// @Description 根据提供的过滤条件,分页获取用户操作日志
// @Tags 数据监控
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListUserActionLogRequest true "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListUserActionLogResponse}
// @Router /api/v1/monitor/user-action-logs [get]
func (c *Controller) ListUserActionLogs(ctx *gin.Context) {
const actionType = "获取用户操作日志列表"
var req dto.ListUserActionLogRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
return
}
opts := repository.UserActionLogListOptions{
UserID: req.UserID,
Username: req.Username,
ActionType: req.ActionType,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
if req.Status != nil {
status := models.AuditStatus(*req.Status)
opts.Status = &status
}
data, total, err := c.monitorService.ListUserActionLogs(opts, req.Page, req.PageSize)
if err != nil {
if errors.Is(err, repository.ErrInvalidPagination) {
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
return
}
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用户操作日志失败: "+err.Error(), actionType, "服务层查询失败", req)
return
}
resp := dto.NewListUserActionLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作日志成功", resp, actionType, "获取用户操作日志成功", req)
}

View File

@@ -165,13 +165,11 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
// 3. 调用审计仓库层获取历史数据
id := uint(userID)
findOptions := repository.FindAuditLogOptions{
findOptions := repository.UserActionLogListOptions{
UserID: &id,
ActionType: actionTypeFilter,
Page: page,
PageSize: pageSize,
ActionType: &actionTypeFilter,
}
logs, total, err := c.auditRepo.List(findOptions)
l, total, err := c.auditRepo.List(findOptions, page, pageSize)
if err != nil {
c.logger.Errorf("%s: 查询历史记录失败: %v, Options: %+v", actionType, err, findOptions)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询历史记录失败", actionType, "查询历史记录失败", findOptions)
@@ -179,8 +177,8 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
}
// 4. 将数据库模型转换为响应 DTO
historyResponses := make([]dto.HistoryResponse, 0, len(logs))
for _, log := range logs {
historyResponses := make([]dto.HistoryResponse, 0, len(l))
for _, log := range l {
historyResponses = append(historyResponses, dto.HistoryResponse{
UserID: log.UserID,
Username: log.Username,