From 559192d30a08c31b44a6eb774a8360ee32fc053f Mon Sep 17 00:00:00 2001 From: chao <48119764+withchao@users.noreply.github.com> Date: Tue, 21 Nov 2023 03:23:27 -0600 Subject: [PATCH] fix: default verifyCode type and admin register user (#274) * fix: default verifyCode type and admin register user * fix: default verifyCode type and admin register user * fix: default verifyCode type and admin register user * fix: default verifyCode type and admin register user --- config/config.yaml | 2 +- internal/api/mw.go | 11 +++++++++++ internal/api/router.go | 2 +- internal/rpc/chat/login.go | 14 ++++++++++---- pkg/common/mw/gin_log.go | 4 ++-- pkg/proto/chat/chat.go | 8 ++++---- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 94098d65b..6ae122a1d 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -76,7 +76,7 @@ verifyCode: maxCount: 10 # 单位时间内最大获取次数 superCode: "666666" # 超级验证码(只有use为空时使用) len: 6 # 验证码长度 - use: "ali" # 使用的验证码服务(use: "ali") + use: "" # 使用的验证码服务(use: "ali") ali: endpoint: "dysmsapi.aliyuncs.com" accessKeyId: "" diff --git a/internal/api/mw.go b/internal/api/mw.go index c0d1cdb54..d3d1a4328 100644 --- a/internal/api/mw.go +++ b/internal/api/mw.go @@ -136,3 +136,14 @@ func (o *MW) CheckUser(c *gin.Context) { } o.setToken(c, userID, constant.NormalUser) } + +func (o *MW) CheckAdminOrNil(c *gin.Context) { + defer c.Next() + userID, userType, _, err := o.parseToken(c) + if err != nil { + return + } + if userType == constant.AdminUser { + o.setToken(c, userID, constant.AdminUser) + } +} diff --git a/internal/api/router.go b/internal/api/router.go index 0a455dd82..d7d52db9e 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -36,7 +36,7 @@ func NewChatRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegis account := router.Group("/account") account.POST("/code/send", chat.SendVerifyCode) // 发送验证码 account.POST("/code/verify", chat.VerifyCode) // 校验验证码 - account.POST("/register", chat.RegisterUser) // 注册 + account.POST("/register", mw.CheckAdminOrNil, chat.RegisterUser) // 注册 account.POST("/login", chat.Login) // 登录 account.POST("/password/reset", chat.ResetPassword) // 忘记密码 account.POST("/password/change", mw.CheckToken, chat.ChangePassword) // 修改密码 diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index 5a507db69..5782839b7 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -54,7 +54,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe return nil, errs.ErrArgs.Wrap("area code or phone number is empty") } if req.AreaCode[0] != '+' { - return nil, errs.ErrArgs.Wrap("area code must start with +") + req.AreaCode = "+" + req.AreaCode } if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { return nil, errs.ErrArgs.Wrap("area code must be number") @@ -306,7 +306,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) ( } if req.User.PhoneNumber != "" { if req.User.AreaCode[0] != '+' { - return nil, errs.ErrArgs.Wrap("area code must start with +") + req.User.AreaCode = "+" + req.User.AreaCode } if _, err := strconv.ParseUint(req.User.AreaCode[1:], 10, 64); err != nil { return nil, errs.ErrArgs.Wrap("area code must be number") @@ -393,8 +393,14 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes if req.Account != "" { attribute, err = o.Database.GetAttributeByAccount(ctx, req.Account) } else if req.PhoneNumber != "" { - if req.AreaCode == "" || req.AreaCode[0] != '+' { - return nil, errs.ErrArgs.Wrap("area code must start with +") + if req.AreaCode == "" { + return nil, errs.ErrArgs.Wrap("area code must") + } + if req.AreaCode[0] != '+' { + req.AreaCode = "+" + req.AreaCode + } + if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { + return nil, errs.ErrArgs.Wrap("area code must be number") } attribute, err = o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber) } else { diff --git a/pkg/common/mw/gin_log.go b/pkg/common/mw/gin_log.go index d8bb56717..ed96a6086 100644 --- a/pkg/common/mw/gin_log.go +++ b/pkg/common/mw/gin_log.go @@ -43,7 +43,7 @@ func GinLog() gin.HandlerFunc { return } start := time.Now() - log.ZDebug(c, "gin request", "method", c.Request.Method, "uri", c.Request.RequestURI, "req header", c.Request.Header, "req body", string(req)) + log.ZDebug(c, "gin request", "method", c.Request.Method, "uri", c.Request.RequestURI, "req", string(req)) c.Request.Body = io.NopCloser(bytes.NewReader(req)) writer := &responseWriter{ ResponseWriter: c.Writer, @@ -52,6 +52,6 @@ func GinLog() gin.HandlerFunc { c.Writer = writer c.Next() resp := writer.buf.Bytes() - log.ZDebug(c, "gin response", "time", time.Since(start), "status", c.Writer.Status(), "resp header", c.Writer.Header(), "resp", string(resp)) + log.ZDebug(c, "gin response", "time", time.Since(start), "status", c.Writer.Status(), "resp", string(resp)) } } diff --git a/pkg/proto/chat/chat.go b/pkg/proto/chat/chat.go index d6e8cd056..d7c0a2458 100644 --- a/pkg/proto/chat/chat.go +++ b/pkg/proto/chat/chat.go @@ -245,10 +245,10 @@ func EmailCheck(email string) error { } func AreaCodeCheck(areaCode string) error { - pattern := `\+[1-9][0-9]{1,2}` - if err := regexMatch(pattern, areaCode); err != nil { - return errs.Wrap(err, "AreaCode is invalid") - } + //pattern := `\+[1-9][0-9]{1,2}` + //if err := regexMatch(pattern, areaCode); err != nil { + // return errs.Wrap(err, "AreaCode is invalid") + //} return nil }