feat: add user register
This commit is contained in:
@@ -6,10 +6,16 @@ type CreateTaskResponse struct {
|
||||
}
|
||||
|
||||
type GetFormulaTaskResponse struct {
|
||||
TaskNo string `json:"task_no"`
|
||||
Status int `json:"status"`
|
||||
Count int `json:"count"`
|
||||
Latex string `json:"latex"`
|
||||
TaskNo string `json:"task_no"`
|
||||
Status int `json:"status"`
|
||||
Count int `json:"count"`
|
||||
Latex string `json:"latex"`
|
||||
Markdown string `json:"markdown"`
|
||||
MathML string `json:"mathml"`
|
||||
MathMLMW string `json:"mathml_mw"`
|
||||
ImageBlob string `json:"image_blob"`
|
||||
DocxURL string `json:"docx_url"`
|
||||
PDFURL string `json:"pdf_url"`
|
||||
}
|
||||
|
||||
// FormulaRecognitionResponse 公式识别服务返回的响应
|
||||
|
||||
@@ -14,7 +14,8 @@ type PhoneLoginRequest struct {
|
||||
}
|
||||
|
||||
type PhoneLoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
Token string `json:"token"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
}
|
||||
|
||||
type UserInfoResponse struct {
|
||||
@@ -22,3 +23,23 @@ type UserInfoResponse struct {
|
||||
Phone string `json:"phone"`
|
||||
Status int `json:"status"` // 0: not login, 1: login
|
||||
}
|
||||
|
||||
type EmailRegisterRequest struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
}
|
||||
|
||||
type EmailRegisterResponse struct {
|
||||
Token string `json:"token"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
}
|
||||
|
||||
type EmailLoginRequest struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type EmailLoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
ExpiresAt int64 `json:"expires_at"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ type User struct {
|
||||
BaseModel
|
||||
Username string `gorm:"column:username" json:"username"`
|
||||
Phone string `gorm:"column:phone" json:"phone"`
|
||||
Email string `gorm:"column:email" json:"email"`
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
WechatOpenID string `gorm:"column:wechat_open_id" json:"wechat_open_id"`
|
||||
WechatUnionID string `gorm:"column:wechat_union_id" json:"wechat_union_id"`
|
||||
@@ -51,3 +52,14 @@ func (dao *UserDao) GetByID(tx *gorm.DB, id int64) (*User, error) {
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (dao *UserDao) GetByEmail(tx *gorm.DB, email string) (*User, error) {
|
||||
var user User
|
||||
if err := tx.Where("email = ?", email).First(&user).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user