feat: add user register
This commit is contained in:
@@ -263,12 +263,6 @@ func (s *RecognitionService) processFormulaTask(ctx context.Context, taskID int6
|
||||
return err
|
||||
}
|
||||
|
||||
// downloadURL, err := oss.GetDownloadURL(ctx, fileURL)
|
||||
// if err != nil {
|
||||
// log.Error(ctx, "func", "processFormulaTask", "msg", "获取下载URL失败", "error", err)
|
||||
// return err
|
||||
// }
|
||||
|
||||
// 将图片转为base64编码
|
||||
base64Image := base64.StdEncoding.EncodeToString(imageData)
|
||||
|
||||
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"gitea.com/bitwsd/document_ai/pkg/log"
|
||||
"gitea.com/bitwsd/document_ai/internal/storage/cache"
|
||||
"gitea.com/bitwsd/document_ai/internal/storage/dao"
|
||||
"gitea.com/bitwsd/document_ai/pkg/common"
|
||||
"gitea.com/bitwsd/document_ai/pkg/log"
|
||||
"gitea.com/bitwsd/document_ai/pkg/sms"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
@@ -107,3 +109,53 @@ func (svc *UserService) GetUserInfo(ctx context.Context, uid int64) (*dao.User,
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (svc *UserService) RegisterByEmail(ctx context.Context, email, password string) (uid int64, err error) {
|
||||
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), email)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
||||
return 0, err
|
||||
}
|
||||
if existingUser != nil {
|
||||
log.Warn(ctx, "func", "RegisterByEmail", "msg", "email already registered", "email", email)
|
||||
return 0, common.ErrEmailExists
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "hash password error", "error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
user := &dao.User{
|
||||
Email: email,
|
||||
Password: string(hashedPassword),
|
||||
}
|
||||
err = svc.userDao.Create(dao.DB.WithContext(ctx), user)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "create user error", "error", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
func (svc *UserService) LoginByEmail(ctx context.Context, email, password string) (uid int64, err error) {
|
||||
user, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), email)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "LoginByEmail", "msg", "get user by email error", "error", err)
|
||||
return 0, err
|
||||
}
|
||||
if user == nil {
|
||||
log.Warn(ctx, "func", "LoginByEmail", "msg", "user not found", "email", email)
|
||||
return 0, common.ErrEmailNotFound
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
||||
if err != nil {
|
||||
log.Warn(ctx, "func", "LoginByEmail", "msg", "password mismatch", "email", email)
|
||||
return 0, common.ErrPasswordMismatch
|
||||
}
|
||||
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user