Tao Chen 1 год назад
Родитель
Сommit
da7b70c471
2 измененных файлов с 9 добавлено и 8 удалено
  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
 				return
214
 				return
215
 			}
215
 			}
216
 		}
216
 		}

+ 6 - 5
service/user.go

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