package user import ( "strconv" "time" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller" "git.huangwc.com/pig/pig-farm-controller/internal/domain/token" "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/repository" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // Controller 用户控制器 type Controller struct { userRepo repository.UserRepository auditRepo repository.UserActionLogRepository logger *logs.Logger tokenService token.TokenService // 注入 token 服务 } // NewController 创建用户控制器实例 func NewController(userRepo repository.UserRepository, auditRepo repository.UserActionLogRepository, logger *logs.Logger, tokenService token.TokenService) *Controller { return &Controller{ userRepo: userRepo, auditRepo: auditRepo, logger: logger, tokenService: tokenService, } } // --- DTOs --- // CreateUserRequest 定义创建用户请求的结构体 type CreateUserRequest struct { Username string `json:"username" binding:"required" example:"newuser"` Password string `json:"password" binding:"required" example:"password123"` } // LoginRequest 定义登录请求的结构体 type LoginRequest struct { // Identifier 可以是用户名、邮箱、手机号、微信号或飞书账号 Identifier string `json:"identifier" binding:"required" example:"testuser"` Password string `json:"password" binding:"required" example:"password123"` } // CreateUserResponse 定义创建用户成功响应的结构体 type CreateUserResponse struct { Username string `json:"username" example:"newuser"` ID uint `json:"id" example:"1"` } // LoginResponse 定义登录成功响应的结构体 type LoginResponse struct { Username string `json:"username" example:"testuser"` ID uint `json:"id" example:"1"` Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."` } // HistoryResponse 定义单条操作历史的响应结构体 type HistoryResponse struct { UserID uint `json:"user_id" example:"101"` Username string `json:"username" example:"testuser"` ActionType string `json:"action_type" example:"更新设备"` Description string `json:"description" example:"设备更新成功"` TargetResource interface{} `json:"target_resource"` Time string `json:"time"` } // ListHistoryResponse 定义操作历史列表的响应结构体 type ListHistoryResponse struct { History []HistoryResponse `json:"history"` Total int64 `json:"total" example:"100"` } // --- Controller Methods --- // CreateUser godoc // @Summary 创建新用户 // @Description 根据用户名和密码创建一个新的系统用户。 // @Tags 用户管理 // @Accept json // @Produce json // @Param user body CreateUserRequest true "用户信息" // @Success 200 {object} controller.Response{data=user.CreateUserResponse} "业务码为201代表创建成功" // @Router /api/v1/users [post] func (c *Controller) CreateUser(ctx *gin.Context) { var req CreateUserRequest if err := ctx.ShouldBindJSON(&req); err != nil { c.logger.Errorf("创建用户: 参数绑定失败: %v", err) controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error()) return } user := &models.User{ Username: req.Username, Password: req.Password, // 密码会在 BeforeSave 钩子中哈希 } if err := c.userRepo.Create(user); err != nil { c.logger.Errorf("创建用户: 创建用户失败: %v", err) // 尝试查询用户,以判断是否是用户名重复导致的错误 _, findErr := c.userRepo.FindByUsername(req.Username) if findErr == nil { // 如果能找到用户,说明是用户名重复 controller.SendErrorResponse(ctx, controller.CodeConflict, "用户名已存在") return } // 其他创建失败的情况 controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建用户失败") return } controller.SendResponse(ctx, controller.CodeCreated, "用户创建成功", CreateUserResponse{ Username: user.Username, ID: user.ID, }) } // Login godoc // @Summary 用户登录 // @Description 用户可以使用用户名、邮箱、手机号、微信号或飞书账号进行登录,成功后返回 JWT 令牌。 // @Tags 用户管理 // @Accept json // @Produce json // @Param credentials body LoginRequest true "登录凭证" // @Success 200 {object} controller.Response{data=user.LoginResponse} "业务码为200代表登录成功" // @Router /api/v1/users/login [post] func (c *Controller) Login(ctx *gin.Context) { var req LoginRequest if err := ctx.ShouldBindJSON(&req); err != nil { c.logger.Errorf("登录: 参数绑定失败: %v", err) controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error()) return } // 使用新的方法,通过唯一标识符(用户名、邮箱等)查找用户 user, err := c.userRepo.FindUserForLogin(req.Identifier) if err != nil { if err == gorm.ErrRecordNotFound { controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确") return } c.logger.Errorf("登录: 查询用户失败: %v", err) controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败") return } if !user.CheckPassword(req.Password) { controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确") return } // 登录成功,生成 JWT token tokenString, err := c.tokenService.GenerateToken(user.ID) if err != nil { c.logger.Errorf("登录: 生成令牌失败: %v", err) controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败,无法生成认证信息") return } controller.SendResponse(ctx, controller.CodeSuccess, "登录成功", LoginResponse{ Username: user.Username, ID: user.ID, Token: tokenString, }) } // ListUserHistory godoc // @Summary 获取指定用户的操作历史 // @Description 根据用户ID,分页获取该用户的操作审计日志。 // @Tags 用户管理 // @Produce json // @Param id path int true "用户ID" // @Param page query int false "页码" default(1) // @Param page_size query int false "每页大小" default(10) // @Param action_type query string false "按操作类型过滤" // @Success 200 {object} controller.Response{data=user.ListHistoryResponse} "业务码为200代表成功获取" // @Router /api/v1/users/{id}/history [get] func (c *Controller) ListUserHistory(ctx *gin.Context) { const actionType = "获取用户操作历史" // 1. 解析路径中的用户ID userIDStr := ctx.Param("id") userID, err := strconv.ParseUint(userIDStr, 10, 64) if err != nil { c.logger.Errorf("%s: 无效的用户ID格式: %v, ID: %s", actionType, err, userIDStr) controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", userIDStr) return } // 2. 解析分页和过滤参数 page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(ctx.DefaultQuery("page_size", "10")) actionTypeFilter := ctx.Query("action_type") // 确保分页参数有效 if page <= 0 { page = 1 } if pageSize <= 0 || pageSize > 100 { pageSize = 10 } // 3. 调用审计仓库层获取历史数据 id := uint(userID) findOptions := repository.FindAuditLogOptions{ UserID: &id, ActionType: actionTypeFilter, Page: page, PageSize: pageSize, } logs, total, err := c.auditRepo.List(findOptions) if err != nil { c.logger.Errorf("%s: 查询历史记录失败: %v, Options: %+v", actionType, err, findOptions) controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询历史记录失败", actionType, "查询历史记录失败", findOptions) return } // 4. 将数据库模型转换为响应 DTO historyResponses := make([]HistoryResponse, 0, len(logs)) for _, log := range logs { historyResponses = append(historyResponses, HistoryResponse{ UserID: log.UserID, Username: log.Username, ActionType: log.ActionType, Description: log.Description, TargetResource: log.TargetResource, Time: log.Time.Format(time.RFC3339), }) } // 5. 发送成功响应 resp := ListHistoryResponse{ History: historyResponses, Total: total, } c.logger.Infof("%s: 成功获取用户 %d 的操作历史, 数量: %d", actionType, userID, len(historyResponses)) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作历史成功", resp, actionType, "获取用户操作历史成功", resp) }