From aab8b65f996ddfad1fd7f0471d9ff5c9718922bb Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 28 Apr 2025 18:26:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E7=99=BB=E5=BD=95=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/user.go | 14 ++++++++++++++ service/toolService.go | 6 ++++++ worker/github.go | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/dao/user.go b/dao/user.go index 5530e4a..ef1a917 100644 --- a/dao/user.go +++ b/dao/user.go @@ -266,3 +266,17 @@ func DeleteThirdPartyLoginByID(id int, userID int) error { } return nil } + +// 更新第三方登录用户信息 +func UpdateThirdPartyUserInfoByThirdPartyID(thirdPartyID int, thirdPartyPlatform, thirdPartyUserName, thirdPartyUserAvatar, thirdPartyUserUrl string) error { + db2 := DB + if proto.Config.SERVER_SQL_LOG { + db2 = DB.Debug() + } + + res := db2.Model(&ThirdPartyUserInfo{}).Where("third_party_id = ? ", thirdPartyID).Updates(ThirdPartyUserInfo{ThirdPartyUserName: thirdPartyUserName, ThirdPartyUserAvatar: thirdPartyUserAvatar, ThirdPartyUserUrl: thirdPartyUserUrl, ThirdPartyPlatform: thirdPartyPlatform}) + if res.Error != nil { + return res.Error + } + return nil +} diff --git a/service/toolService.go b/service/toolService.go index 420fb09..e380f0d 100644 --- a/service/toolService.go +++ b/service/toolService.go @@ -223,6 +223,7 @@ func DoGithubCallBack(state *proto.ThirdPartyLoginState, code string) { return } log.Println("get github user info:", userInfo) + var thirdPartyLoginStatus proto.ThirdPartyLoginStatus thirdPartyLoginStatus.Type = state.Platform HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) //处理第三方登录状态 @@ -394,6 +395,11 @@ func HandleThirdPartyLoginStatus(state *proto.ThirdPartyLoginState, thirdPartyLo log.Println("DoGithubCallBack state type error:", state.Type) thirdPartyLoginStatus.Status = proto.ParameterError //参数错误 } + //更新userInfo到数据库 + err := dao.UpdateThirdPartyUserInfoByThirdPartyID(userInfo.UserID, state.Platform, userInfo.LoginUserName, userInfo.AvatarUrl, userInfo.Url) + if err != nil { + log.Println("update third party user info error:", err) + } } func DoThirdPartyCallBack(state *proto.ThirdPartyLoginState, code string) { diff --git a/worker/github.go b/worker/github.go index c3d5092..ed60a6b 100644 --- a/worker/github.go +++ b/worker/github.go @@ -4,6 +4,7 @@ import ( "StuAcaWorksAI/proto" "bytes" "encoding/json" + "errors" "io" "net/http" ) @@ -64,5 +65,11 @@ func GetGitHubUserInfo(accessToken string) (proto.GitHubUserInfo, error) { return resp, err } err = json.Unmarshal(data, &resp) + if err != nil { + return resp, err + } + if resp.UserID == 0 { + return resp, errors.New("获取用户信息失败,请检查access_token是否正确") + } return resp, err }