第三方信息每次登录更新

This commit is contained in:
junleea 2025-04-28 18:26:46 +08:00
parent 84231a5fe8
commit aab8b65f99
3 changed files with 27 additions and 0 deletions

View File

@ -266,3 +266,17 @@ func DeleteThirdPartyLoginByID(id int, userID int) error {
} }
return nil 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
}

View File

@ -223,6 +223,7 @@ func DoGithubCallBack(state *proto.ThirdPartyLoginState, code string) {
return return
} }
log.Println("get github user info:", userInfo) log.Println("get github user info:", userInfo)
var thirdPartyLoginStatus proto.ThirdPartyLoginStatus var thirdPartyLoginStatus proto.ThirdPartyLoginStatus
thirdPartyLoginStatus.Type = state.Platform thirdPartyLoginStatus.Type = state.Platform
HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) //处理第三方登录状态 HandleThirdPartyLoginStatus(state, &thirdPartyLoginStatus, &userInfo) //处理第三方登录状态
@ -394,6 +395,11 @@ func HandleThirdPartyLoginStatus(state *proto.ThirdPartyLoginState, thirdPartyLo
log.Println("DoGithubCallBack state type error:", state.Type) log.Println("DoGithubCallBack state type error:", state.Type)
thirdPartyLoginStatus.Status = proto.ParameterError //参数错误 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) { func DoThirdPartyCallBack(state *proto.ThirdPartyLoginState, code string) {

View File

@ -4,6 +4,7 @@ import (
"StuAcaWorksAI/proto" "StuAcaWorksAI/proto"
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"io" "io"
"net/http" "net/http"
) )
@ -64,5 +65,11 @@ func GetGitHubUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
return resp, err return resp, err
} }
err = json.Unmarshal(data, &resp) err = json.Unmarshal(data, &resp)
if err != nil {
return resp, err
}
if resp.UserID == 0 {
return resp, errors.New("获取用户信息失败,请检查access_token是否正确")
}
return resp, err return resp, err
} }