add err for RegisterByOauth

This commit is contained in:
Tao Chen
2024-11-03 16:49:03 +08:00
parent 3cd90c8f74
commit da7b70c471
2 changed files with 9 additions and 8 deletions

View File

@@ -208,9 +208,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
} }
//自动注册 //自动注册
user = service.AllService.UserService.RegisterByOauth(oauthUser, op) err, user = service.AllService.UserService.RegisterByOauth(oauthUser, op)
if user.Id == 0 { if err != nil {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed")) c.String(http.StatusInternalServerError, response.TranslateMsg(c, err.Error()))
return return
} }
} }

View File

@@ -11,6 +11,7 @@ import (
"strconv" "strconv"
"time" "time"
"strings" "strings"
"errors"
) )
type UserService struct { type UserService struct {
@@ -276,18 +277,18 @@ func (us *UserService) InfoByOauthId(op string, openId string) *model.User {
} }
// RegisterByOauth 注册 // 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") global.Lock.Lock("registerByOauth")
defer global.Lock.UnLock("registerByOauth") defer global.Lock.UnLock("registerByOauth")
ut := AllService.OauthService.UserThirdInfo(op, oauthUser.OpenId) ut := AllService.OauthService.UserThirdInfo(op, oauthUser.OpenId)
if ut.Id != 0 { if ut.Id != 0 {
return us.InfoById(ut.UserId) return nil, us.InfoById(ut.UserId)
} }
//check if this email has been registered //check if this email has been registered
email := oauthUser.Email email := oauthUser.Email
err, oauthType := AllService.OauthService.GetTypeByOp(op) err, oauthType := AllService.OauthService.GetTypeByOp(op)
if err != nil { if err != nil {
return nil return err, nil
} }
// if email is empty, use username and op as email // if email is empty, use username and op as email
if email == "" { if email == "" {
@@ -314,13 +315,13 @@ func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) *
tx.Create(user) tx.Create(user)
if user.Id == 0 { if user.Id == 0 {
tx.Rollback() tx.Rollback()
return user return errors.New("OauthRegisterFailed"), user
} }
ut.UserId = user.Id ut.UserId = user.Id
} }
tx.Create(ut) tx.Create(ut)
tx.Commit() tx.Commit()
return user return nil, user
} }
// GenerateUsernameByOauth 生成用户名 // GenerateUsernameByOauth 生成用户名