mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	实现用户通过邮件重置密码功能
This commit is contained in:
		@@ -142,7 +142,24 @@ func (this *UserDAO) FindEnabledUserIdWithUsername(tx *dbs.Tx, username string)
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserFullname 获取管理员名称
 | 
			
		||||
// FindUserId 根据多个条件查找用户ID
 | 
			
		||||
func (this *UserDAO) FindUserId(tx *dbs.Tx, verifiedEmail string, verifiedMobile string) (int64, error) {
 | 
			
		||||
	var query = this.Query(tx).
 | 
			
		||||
		State(UserStateEnabled).
 | 
			
		||||
		ResultPk()
 | 
			
		||||
 | 
			
		||||
	if len(verifiedEmail) > 0 {
 | 
			
		||||
		query.Attr("verifiedEmail", verifiedEmail)
 | 
			
		||||
	} else if len(verifiedMobile) > 0 {
 | 
			
		||||
		query.Attr("verifiedMobile", verifiedMobile)
 | 
			
		||||
	} else {
 | 
			
		||||
		return 0, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return query.FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserFullname 获取用户名称
 | 
			
		||||
func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
@@ -150,6 +167,14 @@ func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error)
 | 
			
		||||
		FindStringCol("")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserVerifiedEmail 查询用户已绑定邮箱
 | 
			
		||||
func (this *UserDAO) FindUserVerifiedEmail(tx *dbs.Tx, userId int64) (string, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
		Result("verifiedEmail").
 | 
			
		||||
		FindStringCol("")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateUser 创建用户
 | 
			
		||||
func (this *UserDAO) CreateUser(tx *dbs.Tx, username string,
 | 
			
		||||
	password string,
 | 
			
		||||
@@ -257,8 +282,20 @@ func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string,
 | 
			
		||||
	if len(password) > 0 {
 | 
			
		||||
		op.Password = stringutil.Md5(password)
 | 
			
		||||
	}
 | 
			
		||||
	err := this.Save(tx, op)
 | 
			
		||||
	return err
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateUserPassword 修改用户密码
 | 
			
		||||
func (this *UserDAO) UpdateUserPassword(tx *dbs.Tx, userId int64, password string) error {
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return errors.New("invalid userId")
 | 
			
		||||
	}
 | 
			
		||||
	var op = NewUserOperator()
 | 
			
		||||
	op.Id = userId
 | 
			
		||||
	if len(password) > 0 {
 | 
			
		||||
		op.Password = stringutil.Md5(password)
 | 
			
		||||
	}
 | 
			
		||||
	return this.Save(tx, op)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountAllEnabledUsers 计算用户数量
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ type User struct {
 | 
			
		||||
	Password        string   `field:"password"`        // 密码
 | 
			
		||||
	Fullname        string   `field:"fullname"`        // 真实姓名
 | 
			
		||||
	Mobile          string   `field:"mobile"`          // 手机号
 | 
			
		||||
	VerifiedMobile  string   `field:"verifiedMobile"`  // 已验证手机号
 | 
			
		||||
	Tel             string   `field:"tel"`             // 联系电话
 | 
			
		||||
	Remark          string   `field:"remark"`          // 备注
 | 
			
		||||
	Email           string   `field:"email"`           // 邮箱地址
 | 
			
		||||
@@ -41,6 +42,7 @@ type UserOperator struct {
 | 
			
		||||
	Password        any // 密码
 | 
			
		||||
	Fullname        any // 真实姓名
 | 
			
		||||
	Mobile          any // 手机号
 | 
			
		||||
	VerifiedMobile  any // 已验证手机号
 | 
			
		||||
	Tel             any // 联系电话
 | 
			
		||||
	Remark          any // 备注
 | 
			
		||||
	Email           any // 邮箱地址
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										28
									
								
								internal/db/models/user_verify_code_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								internal/db/models/user_verify_code_dao.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type UserVerifyCodeDAO dbs.DAO
 | 
			
		||||
 | 
			
		||||
func NewUserVerifyCodeDAO() *UserVerifyCodeDAO {
 | 
			
		||||
	return dbs.NewDAO(&UserVerifyCodeDAO{
 | 
			
		||||
		DAOObject: dbs.DAOObject{
 | 
			
		||||
			DB:     Tea.Env,
 | 
			
		||||
			Table:  "edgeUserVerifyCodes",
 | 
			
		||||
			Model:  new(UserVerifyCode),
 | 
			
		||||
			PkName: "id",
 | 
			
		||||
		},
 | 
			
		||||
	}).(*UserVerifyCodeDAO)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var SharedUserVerifyCodeDAO *UserVerifyCodeDAO
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	dbs.OnReady(func() {
 | 
			
		||||
		SharedUserVerifyCodeDAO = NewUserVerifyCodeDAO()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								internal/db/models/user_verify_code_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/user_verify_code_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
package models_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										32
									
								
								internal/db/models/user_verify_code_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								internal/db/models/user_verify_code_model.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
// UserVerifyCode 重置密码之验证码
 | 
			
		||||
type UserVerifyCode struct {
 | 
			
		||||
	Id         uint64 `field:"id"`         // ID
 | 
			
		||||
	Email      string `field:"email"`      // 邮箱地址
 | 
			
		||||
	Mobile     string `field:"mobile"`     // 手机号
 | 
			
		||||
	Code       string `field:"code"`       // 验证码
 | 
			
		||||
	Type       string `field:"type"`       // 类型
 | 
			
		||||
	IsSent     bool   `field:"isSent"`     // 是否已发送
 | 
			
		||||
	IsVerified bool   `field:"isVerified"` // 是否已激活
 | 
			
		||||
	CreatedAt  uint64 `field:"createdAt"`  // 创建时间
 | 
			
		||||
	ExpiresAt  uint64 `field:"expiresAt"`  // 过期时间
 | 
			
		||||
	Day        string `field:"day"`        // YYYYMMDD
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserVerifyCodeOperator struct {
 | 
			
		||||
	Id         any // ID
 | 
			
		||||
	Email      any // 邮箱地址
 | 
			
		||||
	Mobile     any // 手机号
 | 
			
		||||
	Code       any // 验证码
 | 
			
		||||
	Type       any // 类型
 | 
			
		||||
	IsSent     any // 是否已发送
 | 
			
		||||
	IsVerified any // 是否已激活
 | 
			
		||||
	CreatedAt  any // 创建时间
 | 
			
		||||
	ExpiresAt  any // 过期时间
 | 
			
		||||
	Day        any // YYYYMMDD
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewUserVerifyCodeOperator() *UserVerifyCodeOperator {
 | 
			
		||||
	return &UserVerifyCodeOperator{}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								internal/db/models/user_verify_code_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/db/models/user_verify_code_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package models
 | 
			
		||||
@@ -797,8 +797,8 @@ func (this *UserService) RenewUserServersState(ctx context.Context, req *pb.Rene
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CheckUserEmailIsUsing 检查邮箱是否被使用
 | 
			
		||||
func (this *UserService) CheckUserEmailIsUsing(ctx context.Context, req *pb.CheckUserEmailIsUsingRequest) (*pb.CheckUserEmailIsUsingResponse, error) {
 | 
			
		||||
// CheckUserEmail 检查邮箱是否被使用
 | 
			
		||||
func (this *UserService) CheckUserEmail(ctx context.Context, req *pb.CheckUserEmailRequest) (*pb.CheckUserEmailResponse, error) {
 | 
			
		||||
	userId, err := this.ValidateUserNode(ctx, false)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -814,7 +814,35 @@ func (this *UserService) CheckUserEmailIsUsing(ctx context.Context, req *pb.Chec
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if emailOwnerUserId > 0 && userId != emailOwnerUserId {
 | 
			
		||||
		return &pb.CheckUserEmailIsUsingResponse{IsUsing: true}, nil
 | 
			
		||||
		return &pb.CheckUserEmailResponse{Exists: true}, nil
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.CheckUserEmailIsUsingResponse{IsUsing: false}, nil
 | 
			
		||||
	return &pb.CheckUserEmailResponse{Exists: false}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserVerifiedEmailWithUsername 根据用户名查询用户绑定的邮箱
 | 
			
		||||
func (this *UserService) FindUserVerifiedEmailWithUsername(ctx context.Context, req *pb.FindUserVerifiedEmailWithUsernameRequest) (*pb.FindUserVerifiedEmailWithUsernameResponse, error) {
 | 
			
		||||
	_, err := this.ValidateUserNode(ctx, false)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	userId, err := models.SharedUserDAO.FindEnabledUserIdWithUsername(tx, req.Username)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if userId <= 0 {
 | 
			
		||||
		return &pb.FindUserVerifiedEmailWithUsernameResponse{
 | 
			
		||||
			Email: "",
 | 
			
		||||
		}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	email, err := models.SharedUserDAO.FindUserVerifiedEmail(tx, userId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindUserVerifiedEmailWithUsernameResponse{
 | 
			
		||||
		Email: email,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user