2025-09-11 23:48:06 +08:00
|
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-19 20:36:10 +08:00
|
|
|
|
"errors"
|
2025-09-28 01:48:15 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
|
2025-09-11 23:48:06 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
2025-10-19 20:36:10 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
2025-10-24 21:24:48 +08:00
|
|
|
|
domain_notify "git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
|
2025-10-02 00:18:13 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/domain/token"
|
2025-09-11 23:48:06 +08:00
|
|
|
|
"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"
|
2025-10-30 17:15:14 +08:00
|
|
|
|
"github.com/labstack/echo/v4"
|
2025-09-11 23:48:06 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Controller 用户控制器
|
|
|
|
|
|
type Controller struct {
|
2025-10-19 20:36:10 +08:00
|
|
|
|
userRepo repository.UserRepository
|
|
|
|
|
|
monitorService service.MonitorService
|
2025-10-26 15:48:38 +08:00
|
|
|
|
tokenService token.Service
|
2025-10-24 21:24:48 +08:00
|
|
|
|
notifyService domain_notify.Service
|
2025-10-19 20:36:10 +08:00
|
|
|
|
logger *logs.Logger
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewController 创建用户控制器实例
|
2025-10-24 21:24:48 +08:00
|
|
|
|
func NewController(
|
|
|
|
|
|
userRepo repository.UserRepository,
|
|
|
|
|
|
monitorService service.MonitorService,
|
|
|
|
|
|
logger *logs.Logger,
|
2025-10-26 15:48:38 +08:00
|
|
|
|
tokenService token.Service,
|
2025-10-24 21:24:48 +08:00
|
|
|
|
notifyService domain_notify.Service,
|
|
|
|
|
|
) *Controller {
|
2025-09-11 23:48:06 +08:00
|
|
|
|
return &Controller{
|
2025-10-19 20:36:10 +08:00
|
|
|
|
userRepo: userRepo,
|
|
|
|
|
|
monitorService: monitorService,
|
|
|
|
|
|
tokenService: tokenService,
|
2025-10-24 21:24:48 +08:00
|
|
|
|
notifyService: notifyService,
|
2025-10-19 20:36:10 +08:00
|
|
|
|
logger: logger,
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:48:15 +08:00
|
|
|
|
// --- Controller Methods ---
|
|
|
|
|
|
|
2025-09-12 16:58:39 +08:00
|
|
|
|
// CreateUser godoc
|
|
|
|
|
|
// @Summary 创建新用户
|
|
|
|
|
|
// @Description 根据用户名和密码创建一个新的系统用户。
|
|
|
|
|
|
// @Tags 用户管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param user body dto.CreateUserRequest true "用户信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.CreateUserResponse} "业务码为201代表创建成功"
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/users [post]
|
2025-10-30 17:15:14 +08:00
|
|
|
|
func (c *Controller) CreateUser(ctx echo.Context) error {
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.CreateUserRequest
|
2025-10-30 17:15:14 +08:00
|
|
|
|
if err := ctx.Bind(&req); err != nil {
|
2025-09-12 14:54:07 +08:00
|
|
|
|
c.logger.Errorf("创建用户: 参数绑定失败: %v", err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user := &models.User{
|
|
|
|
|
|
Username: req.Username,
|
|
|
|
|
|
Password: req.Password, // 密码会在 BeforeSave 钩子中哈希
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.userRepo.Create(user); err != nil {
|
2025-09-12 14:54:07 +08:00
|
|
|
|
c.logger.Errorf("创建用户: 创建用户失败: %v", err)
|
2025-09-11 23:48:06 +08:00
|
|
|
|
|
|
|
|
|
|
// 尝试查询用户,以判断是否是用户名重复导致的错误
|
|
|
|
|
|
_, findErr := c.userRepo.FindByUsername(req.Username)
|
|
|
|
|
|
if findErr == nil { // 如果能找到用户,说明是用户名重复
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeConflict, "用户名已存在")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 其他创建失败的情况
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建用户失败")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendResponse(ctx, controller.CodeCreated, "用户创建成功", dto.CreateUserResponse{
|
2025-09-11 23:48:06 +08:00
|
|
|
|
Username: user.Username,
|
|
|
|
|
|
ID: user.ID,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 16:58:39 +08:00
|
|
|
|
// Login godoc
|
|
|
|
|
|
// @Summary 用户登录
|
2025-09-27 23:28:06 +08:00
|
|
|
|
// @Description 用户可以使用用户名、邮箱、手机号、微信号或飞书账号进行登录,成功后返回 JWT 令牌。
|
2025-09-12 16:58:39 +08:00
|
|
|
|
// @Tags 用户管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param credentials body dto.LoginRequest true "登录凭证"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.LoginResponse} "业务码为200代表登录成功"
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/users/login [post]
|
2025-10-30 17:15:14 +08:00
|
|
|
|
func (c *Controller) Login(ctx echo.Context) error {
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.LoginRequest
|
2025-10-30 17:15:14 +08:00
|
|
|
|
if err := ctx.Bind(&req); err != nil {
|
2025-09-12 14:54:07 +08:00
|
|
|
|
c.logger.Errorf("登录: 参数绑定失败: %v", err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-27 23:28:06 +08:00
|
|
|
|
// 使用新的方法,通过唯一标识符(用户名、邮箱等)查找用户
|
|
|
|
|
|
user, err := c.userRepo.FindUserForLogin(req.Identifier)
|
2025-09-11 23:48:06 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
2025-09-12 14:54:07 +08:00
|
|
|
|
c.logger.Errorf("登录: 查询用户失败: %v", err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !user.CheckPassword(req.Password) {
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 登录成功,生成 JWT token
|
2025-09-12 13:11:04 +08:00
|
|
|
|
tokenString, err := c.tokenService.GenerateToken(user.ID)
|
2025-09-11 23:48:06 +08:00
|
|
|
|
if err != nil {
|
2025-09-12 14:54:07 +08:00
|
|
|
|
c.logger.Errorf("登录: 生成令牌失败: %v", err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败,无法生成认证信息")
|
2025-09-11 23:48:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendResponse(ctx, controller.CodeSuccess, "登录成功", dto.LoginResponse{
|
2025-09-11 23:48:06 +08:00
|
|
|
|
Username: user.Username,
|
|
|
|
|
|
ID: user.ID,
|
|
|
|
|
|
Token: tokenString,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-09-28 01:48:15 +08:00
|
|
|
|
|
|
|
|
|
|
// ListUserHistory godoc
|
|
|
|
|
|
// @Summary 获取指定用户的操作历史
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// @Description 根据用户ID,分页获取该用户的操作审计日志。支持与通用日志查询接口相同的过滤和排序参数。
|
2025-09-28 01:48:15 +08:00
|
|
|
|
// @Tags 用户管理
|
2025-10-13 14:15:38 +08:00
|
|
|
|
// @Security BearerAuth
|
2025-09-28 01:48:15 +08:00
|
|
|
|
// @Produce json
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// @Param id path int true "用户ID"
|
|
|
|
|
|
// @Param query query dto.ListUserActionLogRequest false "查询参数 (除了 user_id,它被路径中的ID覆盖)"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.ListUserActionLogResponse} "业务码为200代表成功获取"
|
2025-09-28 01:48:15 +08:00
|
|
|
|
// @Router /api/v1/users/{id}/history [get]
|
2025-10-30 17:15:14 +08:00
|
|
|
|
func (c *Controller) ListUserHistory(ctx echo.Context) error {
|
2025-09-28 01:48:15 +08:00
|
|
|
|
const actionType = "获取用户操作历史"
|
|
|
|
|
|
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// 1. 解析路径中的用户ID,它的优先级最高
|
2025-09-28 01:48:15 +08:00
|
|
|
|
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)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", userIDStr)
|
2025-09-28 01:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// 2. 绑定通用的查询请求 DTO
|
|
|
|
|
|
var req dto.ListUserActionLogRequest
|
2025-10-30 17:15:14 +08:00
|
|
|
|
if err := ctx.Bind(&req); err != nil {
|
2025-10-19 20:36:10 +08:00
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
2025-10-19 20:36:10 +08:00
|
|
|
|
}
|
2025-09-28 01:48:15 +08:00
|
|
|
|
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// 3. 准备 Service 调用参数,并强制使用路径中的 UserID
|
|
|
|
|
|
uid := uint(userID)
|
|
|
|
|
|
req.UserID = &uid // 强制覆盖
|
|
|
|
|
|
|
|
|
|
|
|
opts := repository.UserActionLogListOptions{
|
|
|
|
|
|
UserID: req.UserID,
|
|
|
|
|
|
Username: req.Username,
|
|
|
|
|
|
ActionType: req.ActionType,
|
|
|
|
|
|
OrderBy: req.OrderBy,
|
|
|
|
|
|
StartTime: req.StartTime,
|
|
|
|
|
|
EndTime: req.EndTime,
|
2025-09-28 01:48:15 +08:00
|
|
|
|
}
|
2025-10-19 20:36:10 +08:00
|
|
|
|
if req.Status != nil {
|
|
|
|
|
|
status := models.AuditStatus(*req.Status)
|
|
|
|
|
|
opts.Status = &status
|
2025-09-28 01:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// 4. 调用 monitorService,复用其业务逻辑
|
|
|
|
|
|
data, total, err := c.monitorService.ListUserActionLogs(opts, req.Page, req.PageSize)
|
2025-09-28 01:48:15 +08:00
|
|
|
|
if err != nil {
|
2025-10-19 20:36:10 +08:00
|
|
|
|
if errors.Is(err, repository.ErrInvalidPagination) {
|
|
|
|
|
|
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", opts)
|
2025-10-19 20:36:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取用户历史记录失败", actionType, "服务层查询失败", opts)
|
2025-09-28 01:48:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-19 20:36:10 +08:00
|
|
|
|
// 5. 使用复用的 DTO 构建并发送成功响应
|
|
|
|
|
|
resp := dto.NewListUserActionLogResponse(data, total, req.Page, req.PageSize)
|
|
|
|
|
|
c.logger.Infof("%s: 成功获取用户 %d 的操作历史, 数量: %d", actionType, userID, len(data))
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作历史成功", resp, actionType, "获取用户操作历史成功", opts)
|
2025-09-28 01:48:15 +08:00
|
|
|
|
}
|
2025-10-24 21:24:48 +08:00
|
|
|
|
|
|
|
|
|
|
// SendTestNotification godoc
|
|
|
|
|
|
// @Summary 发送测试通知
|
|
|
|
|
|
// @Description 为指定用户发送一条特定渠道的测试消息,以验证其配置是否正确。
|
|
|
|
|
|
// @Tags 用户管理
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path int true "用户ID"
|
|
|
|
|
|
// @Param body body dto.SendTestNotificationRequest true "请求体"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=string} "成功响应"
|
|
|
|
|
|
// @Router /api/v1/users/{id}/notifications/test [post]
|
2025-10-30 17:15:14 +08:00
|
|
|
|
func (c *Controller) SendTestNotification(ctx echo.Context) error {
|
2025-10-24 21:24:48 +08:00
|
|
|
|
const actionType = "发送测试通知"
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 从 URL 中获取用户 ID
|
|
|
|
|
|
userID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 无效的用户ID格式: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的用户ID格式", actionType, "无效的用户ID格式", ctx.Param("id"))
|
2025-10-24 21:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 从请求体 (JSON Body) 中获取要测试的通知类型
|
|
|
|
|
|
var req dto.SendTestNotificationRequest
|
2025-10-30 17:15:14 +08:00
|
|
|
|
if err := ctx.Bind(&req); err != nil {
|
2025-10-24 21:24:48 +08:00
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "请求体格式错误或缺少 'type' 字段: "+err.Error(), actionType, "请求体绑定失败", req)
|
2025-10-24 21:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 调用领域服务
|
|
|
|
|
|
err = c.notifyService.SendTestMessage(uint(userID), req.Type)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 服务层调用失败: %v", actionType, err)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "发送测试消息失败: "+err.Error(), actionType, "服务层调用失败", map[string]interface{}{"userID": userID, "type": req.Type})
|
2025-10-24 21:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 返回成功响应
|
|
|
|
|
|
c.logger.Infof("%s: 成功为用户 %d 发送类型为 %s 的测试消息", actionType, userID, req.Type)
|
2025-10-30 17:15:14 +08:00
|
|
|
|
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "测试消息已发送,请检查您的接收端。", nil, actionType, "测试消息发送成功", map[string]interface{}{"userID": userID, "type": req.Type})
|
2025-10-24 21:24:48 +08:00
|
|
|
|
}
|