From b4f752492000f17eb5963c57fbc82748d4b3163b Mon Sep 17 00:00:00 2001 From: MaxtuneLee Date: Sun, 28 Jul 2024 21:54:02 +0800 Subject: [PATCH] feat(profile): get third party bind status --- src/api/v1/profile.go | 22 ++++++++++++++++++++++ src/model/oauth.go | 11 +++++++++++ src/router/router.go | 1 + src/service/profile.go | 10 ++++++++++ 4 files changed, 44 insertions(+) diff --git a/src/api/v1/profile.go b/src/api/v1/profile.go index cf122eb..a45546e 100644 --- a/src/api/v1/profile.go +++ b/src/api/v1/profile.go @@ -141,3 +141,25 @@ func DealCensorRes(ctx *gin.Context) { } ctx.JSON(http.StatusOK, result.Success(nil)) } + +// BindStatus : get third party login bind status +func BindStatus(ctx *gin.Context) { + token := ctx.GetHeader("TOKEN") + if token == "" { + ctx.JSON(http.StatusBadRequest, result.Failed(result.RequestParamError)) + return + } + uid, err := util.IdentityFromToken(token, model.LOGIN_TOKEN_SUB) + if uid == "" || err != nil { + controllerLogger.Errorln("Can`t get username by token", err) + ctx.JSON(http.StatusOK, result.Failed(result.TokenError)) + return + } + bindList, serErr := service.GetBindList(uid) + if serErr != nil { + controllerLogger.Errorln("GetBindStatus service wrong", serErr) + ctx.JSON(http.StatusOK, result.Failed(result.HandleError(serErr))) + return + } + ctx.JSON(http.StatusOK, result.Success(bindList)) +} diff --git a/src/model/oauth.go b/src/model/oauth.go index 98dc805..72a607d 100644 --- a/src/model/oauth.go +++ b/src/model/oauth.go @@ -63,3 +63,14 @@ func UpsetOauthInfo(oauthInfo OAuth2Info) { Db.Exec(stmt, oauthInfo.Client, oauthInfo.Info, oauthInfo.OauthID, oauthInfo.UserID) } + +// GetOauthBindStatusByUID get oauth bind status by uid +func GetOauthBindStatusByUID(uid string) ([]string, error) { + var oauthBindStatus []string + err := Db.Table("oauth2_info").Where("user_id = ?", uid).Pluck("client", &oauthBindStatus).Error + if err != nil { + log.Errorf("model.getOauthBindStatusByUID ::: %s", err.Error()) + return nil, result.InternalErr + } + return oauthBindStatus, nil +} diff --git a/src/router/router.go b/src/router/router.go index a4449a5..d932a96 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -68,6 +68,7 @@ func InitRouter() *gin.Engine { profile := apiV1.Group("/profile") { profile.GET("/getProfile", v1.GetProfile) + profile.GET("/bindStatus", v1.BindStatus) profile.POST("/changeProfile", v1.ChangeProfile) profile.POST("/uploadAvatar", v1.UploadAvatar) profile.POST("/changeEmail", v1.ChangeEmail) diff --git a/src/service/profile.go b/src/service/profile.go index 9c9f6a3..a809f9b 100644 --- a/src/service/profile.go +++ b/src/service/profile.go @@ -275,3 +275,13 @@ func DealWithFrozenImage(ctx *gin.Context, checkRes *model.CheckRes) error { } return nil } + +// GetBindList get bind list by uid +func GetBindList(uid string) ([]string, error) { + binds, err := model.GetOauthBindStatusByUID(uid) + if err != nil { + serviceLogger.Errorln("GetBindList Err,ErrMsg:", err) + return nil, err + } + return binds, nil +}