支持用手机号等信息代替用户名登录

This commit is contained in:
2025-09-27 23:28:06 +08:00
parent 6d9d4ff91b
commit b177781fa1
2 changed files with 25 additions and 6 deletions

View File

@@ -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