Browse Source

add err for RegisterByOauth

Tao Chen 1 year ago
parent
commit
da7b70c471
2 changed files with 9 additions and 8 deletions
  1. 3 3
      http/controller/api/ouath.go
  2. 6 5
      service/user.go

+ 3 - 3
http/controller/api/ouath.go

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

+ 6 - 5
service/user.go

@@ -11,6 +11,7 @@ import (
11 11
 	"strconv"
12 12
 	"time"
13 13
 	"strings"
14
+	"errors"
14 15
 )
15 16
 
16 17
 type UserService struct {
@@ -276,18 +277,18 @@ func (us *UserService) InfoByOauthId(op string, openId string) *model.User {
276 277
 }
277 278
 
278 279
 // RegisterByOauth 注册
279
-func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) *model.User {
280
+func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) (error, *model.User) {
280 281
 	global.Lock.Lock("registerByOauth")
281 282
 	defer global.Lock.UnLock("registerByOauth")
282 283
 	ut := AllService.OauthService.UserThirdInfo(op, oauthUser.OpenId)
283 284
 	if ut.Id != 0 {
284
-		return us.InfoById(ut.UserId)
285
+		return nil, us.InfoById(ut.UserId)
285 286
 	}
286 287
 	//check if this email has been registered 
287 288
 	email := oauthUser.Email
288 289
 	err, oauthType := AllService.OauthService.GetTypeByOp(op)
289 290
 	if err != nil {
290
-		return nil
291
+		return err, nil
291 292
 	}
292 293
 	// if email is empty, use username and op as email
293 294
 	if email == "" {
@@ -314,13 +315,13 @@ func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser , op string) *
314 315
 		tx.Create(user)
315 316
 		if user.Id == 0 {
316 317
 			tx.Rollback()
317
-			return user
318
+			return errors.New("OauthRegisterFailed"), user
318 319
 		}
319 320
 		ut.UserId = user.Id
320 321
 	}
321 322
 	tx.Create(ut)
322 323
 	tx.Commit()
323
-	return user
324
+	return nil, user
324 325
 }
325 326
 
326 327
 // GenerateUsernameByOauth 生成用户名