From b177781fa15a06abc6908bf0a7c78eced0f98606 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 27 Sep 2025 23:28:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=A8=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7=E7=AD=89=E4=BF=A1=E6=81=AF=E4=BB=A3=E6=9B=BF=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=90=8D=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/controller/user/user_controller.go | 14 ++++++++------ internal/infra/repository/user_repository.go | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/app/controller/user/user_controller.go b/internal/app/controller/user/user_controller.go index 14579b5..9e13a17 100644 --- a/internal/app/controller/user/user_controller.go +++ b/internal/app/controller/user/user_controller.go @@ -34,8 +34,9 @@ type CreateUserRequest struct { // LoginRequest 定义登录请求的结构体 type LoginRequest struct { - Username string `json:"username" binding:"required" example:"testuser"` - Password string `json:"password" binding:"required" example:"password123"` + // Identifier 可以是用户名、邮箱、手机号、微信号或飞书账号 + Identifier string `json:"identifier" binding:"required" example:"testuser"` + Password string `json:"password" binding:"required" example:"password123"` } // CreateUserResponse 定义创建用户成功响应的结构体 @@ -96,7 +97,7 @@ func (c *Controller) CreateUser(ctx *gin.Context) { // Login godoc // @Summary 用户登录 -// @Description 用户使用用户名和密码登录,成功后返回 JWT 令牌。 +// @Description 用户可以使用用户名、邮箱、手机号、微信号或飞书账号进行登录,成功后返回 JWT 令牌。 // @Tags 用户管理 // @Accept json // @Produce json @@ -111,10 +112,11 @@ func (c *Controller) Login(ctx *gin.Context) { return } - user, err := c.userRepo.FindByUsername(req.Username) + // 使用新的方法,通过唯一标识符(用户名、邮箱等)查找用户 + user, err := c.userRepo.FindUserForLogin(req.Identifier) if err != nil { if err == gorm.ErrRecordNotFound { - controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "用户名或密码不正确") + controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确") return } c.logger.Errorf("登录: 查询用户失败: %v", err) @@ -123,7 +125,7 @@ func (c *Controller) Login(ctx *gin.Context) { } if !user.CheckPassword(req.Password) { - controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "用户名或密码不正确") + controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "登录凭证不正确") return } diff --git a/internal/infra/repository/user_repository.go b/internal/infra/repository/user_repository.go index dcee8fa..8a290cf 100644 --- a/internal/infra/repository/user_repository.go +++ b/internal/infra/repository/user_repository.go @@ -12,6 +12,7 @@ type UserRepository interface { Create(user *models.User) error FindByUsername(username string) (*models.User, error) FindByID(id uint) (*models.User, error) + FindUserForLogin(identifier string) (*models.User, error) } // gormUserRepository 是 UserRepository 的 GORM 实现 @@ -39,6 +40,22 @@ func (r *gormUserRepository) FindByUsername(username string) (*models.User, erro return &user, nil } +// FindUserForLogin 根据提供的标识符查找用户,可用于登录验证 +// 标识符可以是用户名、邮箱、手机号、微信号或飞书账号 +func (r *gormUserRepository) FindUserForLogin(identifier string) (*models.User, error) { + var user models.User + // 使用 ->> 操作符来查询 JSONB 字段中的文本值 + err := r.db.Where( + "username = ? OR contact ->> 'email' = ? OR contact ->> 'phone' = ? OR contact ->> 'wechat' = ? OR contact ->> 'feishu' = ?", + identifier, identifier, identifier, identifier, identifier, + ).First(&user).Error + + if err != nil { + return nil, err + } + return &user, nil +} + // FindByID 根据 ID 查找用户 func (r *gormUserRepository) FindByID(id uint) (*models.User, error) { var user models.User