fix oauth register #26 #23

This commit is contained in:
ljw
2024-10-29 15:31:27 +08:00
parent 991fc86fa6
commit 001884a4d8
4 changed files with 30 additions and 33 deletions

View File

@@ -101,7 +101,7 @@ func main() {
}
func DatabaseAutoUpdate() {
version := 241
version := 242
db := global.DB

View File

@@ -51,30 +51,22 @@ func TestLocal_GetLock(t *testing.T) {
func TestLocal_Lock(t *testing.T) {
l := NewLocal()
wg := sync.WaitGroup{}
wg.Add(3)
m := 10
wg.Add(m)
i := 0
go func() {
l.Lock("key")
fmt.Println("l1", i)
i++
l.UnLock("key")
wg.Done()
}()
go func() {
l.Lock("key")
fmt.Println("l2", i)
i++
l.UnLock("key")
wg.Done()
}()
go func() {
l.Lock("key")
fmt.Println("l3", i)
i++
l.UnLock("key")
wg.Done()
}()
for j := 0; j < m; j++ {
go func() {
l.Lock("key")
//fmt.Println(j, i)
i++
fmt.Println(j, i)
l.UnLock("key")
wg.Done()
}()
}
wg.Wait()
fmt.Println(i)
}
func TestSyncMap(t *testing.T) {

View File

@@ -2,7 +2,7 @@ package model
type User struct {
IdModel
Username string `json:"username" gorm:"default:'';not null;index,unique"`
Username string `json:"username" gorm:"default:'';not null;uniqueIndex"`
Password string `json:"-" gorm:"default:'';not null;"`
Nickname string `json:"nickname" gorm:"default:'';not null;"`
Avatar string `json:"avatar" gorm:"default:'';not null;"`

View File

@@ -221,20 +221,21 @@ func (us *UserService) RegisterByGoogle(name string, email string) *model.User {
// RegisterByOauth 注册
func (us *UserService) RegisterByOauth(thirdType, thirdName, uid string) *model.User {
global.Lock.Lock("registerByOauth")
defer global.Lock.UnLock("registerByOauth")
ut := AllService.OauthService.UserThirdInfo(thirdType, uid)
if ut.Id != 0 {
u := &model.User{}
global.DB.Where("id = ?", ut.UserId).First(u)
return u
}
tx := global.DB.Begin()
ut := &model.UserThird{
ut = &model.UserThird{
OpenId: uid,
ThirdName: thirdName,
ThirdType: thirdType,
}
//global.DB.Where("open_id = ?", githubId).First(ut)
//这种情况不应该出现如果出现说明有bug
//if ut.Id != 0 {
// u := &model.User{}
// global.DB.Where("id = ?", ut.UserId).First(u)
// tx.Commit()
// return u
//}
username := us.GenerateUsernameByOauth(thirdName)
u := &model.User{
@@ -242,6 +243,10 @@ func (us *UserService) RegisterByOauth(thirdType, thirdName, uid string) *model.
GroupId: 1,
}
global.DB.Create(u)
if u.Id == 0 {
tx.Rollback()
return u
}
ut.UserId = u.Id
global.DB.Create(ut)