From da7b70c471762857185493eab512eb2ffdb30c5e Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Sun, 3 Nov 2024 16:49:03 +0800 Subject: [PATCH] add err for RegisterByOauth --- http/controller/api/ouath.go | 6 +++--- service/user.go | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/http/controller/api/ouath.go b/http/controller/api/ouath.go index b3a3ba3..3d7ab67 100644 --- a/http/controller/api/ouath.go +++ b/http/controller/api/ouath.go @@ -208,9 +208,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) { } //自动注册 - user = service.AllService.UserService.RegisterByOauth(oauthUser, op) - if user.Id == 0 { - c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed")) + err, user = service.AllService.UserService.RegisterByOauth(oauthUser, op) + if err != nil { + c.String(http.StatusInternalServerError, response.TranslateMsg(c, err.Error())) return } } diff --git a/service/user.go b/service/user.go index 358240e..4a9fa66 100644 --- a/service/user.go +++ b/service/user.go @@ -11,6 +11,7 @@ import ( "strconv" "time" "strings" + "errors" ) type UserService struct { @@ -276,18 +277,18 @@ func (us *UserService) InfoByOauthId(op string, openId string) *model.User { } // RegisterByOauth 注册 -func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) *model.User { +func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) (error, *model.User) { global.Lock.Lock("registerByOauth") defer global.Lock.UnLock("registerByOauth") ut := AllService.OauthService.UserThirdInfo(op, oauthUser.OpenId) if ut.Id != 0 { - return us.InfoById(ut.UserId) + return nil, us.InfoById(ut.UserId) } //check if this email has been registered email := oauthUser.Email err, oauthType := AllService.OauthService.GetTypeByOp(op) if err != nil { - return nil + return err, nil } // if email is empty, use username and op as email if email == "" { @@ -314,13 +315,13 @@ func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) * tx.Create(user) if user.Id == 0 { tx.Rollback() - return user + return errors.New("OauthRegisterFailed"), user } ut.UserId = user.Id } tx.Create(ut) tx.Commit() - return user + return nil, user } // GenerateUsernameByOauth 生成用户名