Compare commits
1 Commits
test
...
feature/em
| Author | SHA1 | Date | |
|---|---|---|---|
| 057681561a |
@@ -43,7 +43,6 @@ func SetupRouter(engine *gin.RouterGroup) {
|
|||||||
userRouter := v1.Group("/user")
|
userRouter := v1.Group("/user")
|
||||||
{
|
{
|
||||||
userRouter.POST("/sms", userEndpoint.SendVerificationCode)
|
userRouter.POST("/sms", userEndpoint.SendVerificationCode)
|
||||||
userRouter.POST("/email/code", userEndpoint.SendEmailVerificationCode)
|
|
||||||
userRouter.POST("/register", userEndpoint.RegisterByEmail)
|
userRouter.POST("/register", userEndpoint.RegisterByEmail)
|
||||||
userRouter.POST("/login", userEndpoint.LoginByEmail)
|
userRouter.POST("/login", userEndpoint.LoginByEmail)
|
||||||
userRouter.GET("/oauth/google/url", userEndpoint.GetGoogleOAuthUrl)
|
userRouter.GET("/oauth/google/url", userEndpoint.GetGoogleOAuthUrl)
|
||||||
|
|||||||
@@ -106,26 +106,6 @@ func (h *UserEndpoint) GetUserInfo(ctx *gin.Context) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UserEndpoint) SendEmailVerificationCode(ctx *gin.Context) {
|
|
||||||
req := model.EmailCodeSendRequest{}
|
|
||||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := h.userService.SendEmailCode(ctx, req.Email); err != nil {
|
|
||||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Error(ctx, "func", "SendEmailVerificationCode", "msg", "发送邮箱验证码失败", "error", err)
|
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.EmailCodeSendResponse{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
||||||
req := model.EmailRegisterRequest{}
|
req := model.EmailRegisterRequest{}
|
||||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
@@ -133,7 +113,7 @@ func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password, req.Code)
|
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
if bizErr, ok := err.(*common.BusinessError); ok {
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ database:
|
|||||||
host: localhost
|
host: localhost
|
||||||
port: 3006
|
port: 3006
|
||||||
username: root
|
username: root
|
||||||
password: texpixel#pwd123!
|
password: root123
|
||||||
dbname: doc_ai
|
dbname: doc_ai
|
||||||
max_idle: 10
|
max_idle: 10
|
||||||
max_open: 100
|
max_open: 100
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
addr: localhost:6079
|
addr: localhost:6079
|
||||||
password: yoge@123321!
|
password: redis123
|
||||||
db: 0
|
db: 0
|
||||||
|
|
||||||
limit:
|
limit:
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
git push origin test
|
||||||
|
|
||||||
git push origin "$BRANCH"
|
ssh ubuntu << 'ENDSSH'
|
||||||
|
cd /home/yoge/Dev/doc_ai_backed
|
||||||
ssh ubuntu "
|
git checkout test
|
||||||
cd /home/yoge/Dev/doc_ai_backed &&
|
git pull origin test
|
||||||
git checkout $BRANCH &&
|
docker compose -f docker-compose.infra.yml up -d
|
||||||
git pull origin $BRANCH &&
|
docker compose down
|
||||||
docker compose -f docker-compose.infra.yml up -d --no-recreate || true &&
|
docker image rm doc_ai_backed-doc_ai:latest
|
||||||
docker compose down &&
|
docker compose up -d
|
||||||
docker image rm doc_ai_backed-doc_ai:latest || true &&
|
ENDSSH
|
||||||
docker compose up -d
|
|
||||||
"
|
|
||||||
BIN
document_ai
Executable file
BIN
document_ai
Executable file
Binary file not shown.
@@ -23,16 +23,9 @@ type UserInfoResponse struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailCodeSendRequest struct {
|
|
||||||
Email string `json:"email" binding:"required,email"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type EmailCodeSendResponse struct{}
|
|
||||||
|
|
||||||
type EmailRegisterRequest struct {
|
type EmailRegisterRequest struct {
|
||||||
Email string `json:"email" binding:"required,email"`
|
Email string `json:"email" binding:"required,email"`
|
||||||
Password string `json:"password" binding:"required,min=6"`
|
Password string `json:"password" binding:"required,min=6"`
|
||||||
Code string `json:"code" binding:"required"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailRegisterResponse struct {
|
type EmailRegisterResponse struct {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/texpixel/document_ai/internal/model/task"
|
"gitea.com/texpixel/document_ai/internal/model/task"
|
||||||
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"gitea.com/texpixel/document_ai/internal/storage/cache"
|
"gitea.com/texpixel/document_ai/internal/storage/cache"
|
||||||
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
||||||
"gitea.com/texpixel/document_ai/pkg/common"
|
"gitea.com/texpixel/document_ai/pkg/common"
|
||||||
"gitea.com/texpixel/document_ai/pkg/email"
|
|
||||||
"gitea.com/texpixel/document_ai/pkg/log"
|
"gitea.com/texpixel/document_ai/pkg/log"
|
||||||
"gitea.com/texpixel/document_ai/pkg/sms"
|
"gitea.com/texpixel/document_ai/pkg/sms"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -116,52 +115,14 @@ func (svc *UserService) GetUserInfo(ctx context.Context, uid int64) (*dao.User,
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *UserService) SendEmailCode(ctx context.Context, emailAddr string) error {
|
func (svc *UserService) RegisterByEmail(ctx context.Context, email, password string) (uid int64, err error) {
|
||||||
limit, err := cache.GetUserSendEmailLimit(ctx, emailAddr)
|
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), email)
|
||||||
if err != nil {
|
|
||||||
log.Error(ctx, "func", "SendEmailCode", "msg", "get send email limit error", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if limit >= cache.UserSendEmailLimitCount {
|
|
||||||
return common.ErrEmailSendLimit
|
|
||||||
}
|
|
||||||
|
|
||||||
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
|
||||||
|
|
||||||
subject := "TexPixel 邮箱验证码"
|
|
||||||
body := fmt.Sprintf(`<p>您的验证码为:<strong>%s</strong>,10分钟内有效,请勿泄露。</p>`, code)
|
|
||||||
if err = email.Send(ctx, emailAddr, subject, body); err != nil {
|
|
||||||
log.Error(ctx, "func", "SendEmailCode", "msg", "send email error", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if cacheErr := cache.SetUserEmailCode(ctx, emailAddr, code); cacheErr != nil {
|
|
||||||
log.Error(ctx, "func", "SendEmailCode", "msg", "set email code error", "error", cacheErr)
|
|
||||||
}
|
|
||||||
if cacheErr := cache.SetUserSendEmailLimit(ctx, emailAddr); cacheErr != nil {
|
|
||||||
log.Error(ctx, "func", "SendEmailCode", "msg", "set send email limit error", "error", cacheErr)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password, code string) (uid int64, err error) {
|
|
||||||
storedCode, err := cache.GetUserEmailCode(ctx, emailAddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get email code error", "error", err)
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if storedCode == "" || storedCode != code {
|
|
||||||
log.Warn(ctx, "func", "RegisterByEmail", "msg", "invalid email code", "email", emailAddr)
|
|
||||||
return 0, common.ErrEmailCodeError
|
|
||||||
}
|
|
||||||
|
|
||||||
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), emailAddr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if existingUser != nil {
|
if existingUser != nil {
|
||||||
log.Warn(ctx, "func", "RegisterByEmail", "msg", "email already registered", "email", emailAddr)
|
log.Warn(ctx, "func", "RegisterByEmail", "msg", "email already registered", "email", email)
|
||||||
return 0, common.ErrEmailExists
|
return 0, common.ErrEmailExists
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,18 +133,15 @@ func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password
|
|||||||
}
|
}
|
||||||
|
|
||||||
user := &dao.User{
|
user := &dao.User{
|
||||||
Email: emailAddr,
|
Email: email,
|
||||||
Password: string(hashedPassword),
|
Password: string(hashedPassword),
|
||||||
}
|
}
|
||||||
if err = svc.userDao.Create(dao.DB.WithContext(ctx), user); err != nil {
|
err = svc.userDao.Create(dao.DB.WithContext(ctx), user)
|
||||||
|
if err != nil {
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "create user error", "error", err)
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "create user error", "error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheErr := cache.DeleteUserEmailCode(ctx, emailAddr); cacheErr != nil {
|
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "delete email code error", "error", cacheErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return user.ID, nil
|
return user.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
internal/storage/cache/user.go
vendored
53
internal/storage/cache/user.go
vendored
@@ -61,56 +61,3 @@ func SetUserSendSmsLimit(ctx context.Context, phone string) error {
|
|||||||
func DeleteUserSmsCode(ctx context.Context, phone string) error {
|
func DeleteUserSmsCode(ctx context.Context, phone string) error {
|
||||||
return RedisClient.Del(ctx, fmt.Sprintf(UserSmsCodePrefix, phone)).Err()
|
return RedisClient.Del(ctx, fmt.Sprintf(UserSmsCodePrefix, phone)).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
UserEmailCodeTTL = 10 * time.Minute
|
|
||||||
UserSendEmailLimitTTL = 24 * time.Hour
|
|
||||||
UserSendEmailLimitCount = 5
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
UserEmailCodePrefix = "user:email_code:%s"
|
|
||||||
UserSendEmailLimit = "user:send_email_limit:%s"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetUserEmailCode(ctx context.Context, email string) (string, error) {
|
|
||||||
code, err := RedisClient.Get(ctx, fmt.Sprintf(UserEmailCodePrefix, email)).Result()
|
|
||||||
if err != nil {
|
|
||||||
if err == redis.Nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return code, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetUserEmailCode(ctx context.Context, email, code string) error {
|
|
||||||
return RedisClient.Set(ctx, fmt.Sprintf(UserEmailCodePrefix, email), code, UserEmailCodeTTL).Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteUserEmailCode(ctx context.Context, email string) error {
|
|
||||||
return RedisClient.Del(ctx, fmt.Sprintf(UserEmailCodePrefix, email)).Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUserSendEmailLimit(ctx context.Context, email string) (int, error) {
|
|
||||||
limit, err := RedisClient.Get(ctx, fmt.Sprintf(UserSendEmailLimit, email)).Result()
|
|
||||||
if err != nil {
|
|
||||||
if err == redis.Nil {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return strconv.Atoi(limit)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetUserSendEmailLimit(ctx context.Context, email string) error {
|
|
||||||
key := fmt.Sprintf(UserSendEmailLimit, email)
|
|
||||||
count, err := RedisClient.Incr(ctx, key).Result()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if count > UserSendEmailLimitCount {
|
|
||||||
return errors.New("send email limit")
|
|
||||||
}
|
|
||||||
return RedisClient.Expire(ctx, key, UserSendEmailLimitTTL).Err()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ const (
|
|||||||
CodeEmailExists = 1004
|
CodeEmailExists = 1004
|
||||||
CodeEmailNotFound = 1005
|
CodeEmailNotFound = 1005
|
||||||
CodePasswordMismatch = 1006
|
CodePasswordMismatch = 1006
|
||||||
CodeEmailCodeError = 1007
|
|
||||||
CodeEmailSendLimit = 1008
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -38,8 +36,6 @@ const (
|
|||||||
CodeEmailExistsMsg = "email already registered"
|
CodeEmailExistsMsg = "email already registered"
|
||||||
CodeEmailNotFoundMsg = "email not found"
|
CodeEmailNotFoundMsg = "email not found"
|
||||||
CodePasswordMismatchMsg = "password mismatch"
|
CodePasswordMismatchMsg = "password mismatch"
|
||||||
CodeEmailCodeErrorMsg = "email code error"
|
|
||||||
CodeEmailSendLimitMsg = "email send limit reached"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BusinessError struct {
|
type BusinessError struct {
|
||||||
@@ -65,6 +61,4 @@ var (
|
|||||||
ErrEmailExists = NewError(CodeEmailExists, CodeEmailExistsMsg, nil)
|
ErrEmailExists = NewError(CodeEmailExists, CodeEmailExistsMsg, nil)
|
||||||
ErrEmailNotFound = NewError(CodeEmailNotFound, CodeEmailNotFoundMsg, nil)
|
ErrEmailNotFound = NewError(CodeEmailNotFound, CodeEmailNotFoundMsg, nil)
|
||||||
ErrPasswordMismatch = NewError(CodePasswordMismatch, CodePasswordMismatchMsg, nil)
|
ErrPasswordMismatch = NewError(CodePasswordMismatch, CodePasswordMismatchMsg, nil)
|
||||||
ErrEmailCodeError = NewError(CodeEmailCodeError, CodeEmailCodeErrorMsg, nil)
|
|
||||||
ErrEmailSendLimit = NewError(CodeEmailSendLimit, CodeEmailSendLimitMsg, nil)
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user