diff --git a/internal/app/api/v1/social.go b/internal/app/api/v1/social.go index 68293ac..3536788 100644 --- a/internal/app/api/v1/social.go +++ b/internal/app/api/v1/social.go @@ -194,3 +194,22 @@ func ConfirmBindChange(c *gin.Context) { Ok(c) } } + +// CancelBindChange 取消绑定变更 +func CancelBindChange(c *gin.Context) { + var r request.CancelBindChangeRequest + if err := c.ShouldBindJSON(&r); err != nil { + FailWithMessage(GetMessage(c, "ParameterError"), c) + return + } + address := c.GetString("address") + if address == "" { + Fail(c) + return + } + if err := srv.CancelBindChange(address, r.Type); err != nil { + FailWithMessage(GetMessage(c, err.Error()), c) + } else { + Ok(c) + } +} diff --git a/internal/app/api/v2/quest.go b/internal/app/api/v2/quest.go index ddc268b..759f7ff 100644 --- a/internal/app/api/v2/quest.go +++ b/internal/app/api/v2/quest.go @@ -13,7 +13,7 @@ func AddQuestV2(c *gin.Context) { } address := c.GetString("address") if list, err := srv.AddQuestV2(address, add); err != nil { - FailWithMessage(GetMessage(c, "FetchFailed"), c) + FailWithMessage(GetMessage(c, err.Error()), c) } else { OkWithData(list, c) } diff --git a/internal/app/assets/locale.json b/internal/app/assets/locale.json index d081707..3833598 100644 --- a/internal/app/assets/locale.json +++ b/internal/app/assets/locale.json @@ -38,7 +38,8 @@ "EmailAlreadyLinked": "The email has already been linked to another address!", "GithubAlreadyLinked": "Github has been linked to another wallet address!", "AddressAlreadyLinkedGithub": "The wallet address has already been linked to Github. Please do not repeat the operation.", - "FailedObtainGithubInfo": "Failed to obtain Github information!" + "FailedObtainGithubInfo": "Failed to obtain Github information!", + "PermissionDenied": "Permission Denied!" }, "zh-CN": { "OperationSuccess": "操作成功!", @@ -79,6 +80,7 @@ "EmailAlreadyLinked": "邮箱已绑定其他钱包地址", "GithubAlreadyLinked": "Github 已绑定其他钱包地址", "AddressAlreadyLinkedGithub": "钱包地址已绑定Github,请勿重复操作", - "FailedObtainGithubInfo": "绑定 Github 信息失败!" + "FailedObtainGithubInfo": "绑定 Github 信息失败!", + "PermissionDenied": "权限不足!" } } \ No newline at end of file diff --git a/internal/app/dao/users.go b/internal/app/dao/users.go index b5aa1d4..e3379f2 100644 --- a/internal/app/dao/users.go +++ b/internal/app/dao/users.go @@ -162,6 +162,17 @@ func (d *Dao) ConfirmBindChange(address string, bindType string) (err error) { return d.redis.Del(context.Background(), key).Err() } +// CancelBindChange 取消绑定变更 +func (d *Dao) CancelBindChange(address string, bindType string) (err error) { + key := getRebindKey(bindType, address) + // 从 Redis 删除 + err = d.redis.Del(context.Background(), key).Err() + if err != nil { + return + } + return nil +} + // ParticleUpdateSocialsInfo 更新社交信息 func (d *Dao) ParticleUpdateSocialsInfo(address string, particleUserinfo datatypes.JSON) (err error) { provider := gjson.Get(particleUserinfo.String(), "thirdparty_user_info.provider").String() diff --git a/internal/app/model/request/social.go b/internal/app/model/request/social.go index 673d518..ea429ab 100644 --- a/internal/app/model/request/social.go +++ b/internal/app/model/request/social.go @@ -21,3 +21,7 @@ type BindSocialResultRequest struct { type ConfirmBindChangeRequest struct { Type string `json:"type" binding:"required"` } + +type CancelBindChangeRequest struct { + Type string `json:"type" binding:"required"` +} diff --git a/internal/app/model/response/user.go b/internal/app/model/response/user.go index 855fb9b..fa5ac7c 100644 --- a/internal/app/model/response/user.go +++ b/internal/app/model/response/user.go @@ -14,3 +14,8 @@ type QuestWithClaimed struct { func (QuestWithClaimed) TableName() string { return "quest" } + +type GetUserInfoRes struct { + model.Users + IsAdmin bool `json:"is_admin"` +} diff --git a/internal/app/router/social.go b/internal/app/router/social.go index 9bf166d..ade5dee 100644 --- a/internal/app/router/social.go +++ b/internal/app/router/social.go @@ -27,7 +27,8 @@ func InitSocialRouter(Router *gin.RouterGroup) { } { routerAuth.POST("/bindSocialResult", v1.BindSocialResult) // 查询绑定结果 - routerAuth.POST("/confirmBindChange", v1.ConfirmBindChange) // 确认绑定 + routerAuth.POST("/confirmBindChange", v1.ConfirmBindChange) // 确认换绑 + routerAuth.POST("/cancelBindChange", v1.CancelBindChange) // 取消换绑 routerAuth.POST("/unbindSocial", v1.UnbindSocial) // 解绑 } } diff --git a/internal/app/service/quest_v2.go b/internal/app/service/quest_v2.go index 607439c..831412b 100644 --- a/internal/app/service/quest_v2.go +++ b/internal/app/service/quest_v2.go @@ -10,6 +10,14 @@ import ( ) func (s *Service) AddQuestV2(address string, add request.AddQuestV2Request) (res string, err error) { + // 是否管理员 + isAdmin, err := s.dao.IsAdmin(address) + if err != nil { + return + } + if !isAdmin { + return "", errors.New("PermissionDenied") + } privateKey, err := crypto.HexToECDSA(s.c.BlockChain.SignPrivateKey) if err != nil { return diff --git a/internal/app/service/social.go b/internal/app/service/social.go index 8bdaae2..41e9d69 100644 --- a/internal/app/service/social.go +++ b/internal/app/service/social.go @@ -55,10 +55,11 @@ func (s *Service) WechatBindAddress(c *gin.Context, address, fromUserName string if err != nil { return errors.New("服务器内部错误") } + s.dao.CancelBindChange(address, "wechat") if isBinding { // 替换绑定 if replace { - err = s.dao.UnbindSocial(address, "wechat") + err = s.dao.UnbindSocial(bindingAddress, "wechat") if err != nil { return errors.New("UnexpectedError") } @@ -120,6 +121,7 @@ func (s *Service) DiscordCallback(address string, discordCallback interface{}, r if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "discord") if Binding { err = s.dao.SaveRebindInfo(address, "discord", bindingAddress) if err != nil { @@ -216,6 +218,7 @@ func (s *Service) EmailBindAddress(address, emailAddress, code string, replace b if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "email") if isBinding { err = s.dao.SaveRebindInfo(address, "email", bindingAddress) if err != nil { @@ -280,6 +283,7 @@ func (s *Service) GithubCallback(address string, githubCallback interface{}, rep if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "github") if binding { err = s.dao.SaveRebindInfo(address, "github", bindingAddress) if err != nil { @@ -333,3 +337,8 @@ func (s *Service) BindSocialResult(address string, bindType string) (res respons func (s *Service) ConfirmBindChange(address, bindType string) (err error) { return s.dao.ConfirmBindChange(address, bindType) } + +// CancelBindChange 取消绑定变更 +func (s *Service) CancelBindChange(address, bindType string) (err error) { + return s.dao.CancelBindChange(address, bindType) +} diff --git a/internal/app/service/user.go b/internal/app/service/user.go index 1c4d028..eefeb31 100644 --- a/internal/app/service/user.go +++ b/internal/app/service/user.go @@ -3,6 +3,7 @@ package service import ( "backend-go/internal/app/model" "backend-go/internal/app/model/request" + "backend-go/internal/app/model/response" "backend-go/internal/app/utils" "backend-go/pkg/auth" "backend-go/pkg/log" @@ -20,7 +21,7 @@ import ( "strings" ) -func (s *Service) GetUserInfo(address string, loginAddress string) (res interface{}, err error) { +func (s *Service) GetUserInfo(address string, loginAddress string) (res response.GetUserInfoRes, err error) { var user model.Users if user, err = s.dao.GetUser(address); err != nil { return @@ -48,7 +49,9 @@ func (s *Service) GetUserInfo(address string, loginAddress string) (res interfac } } user.NickName = &showStr - return user, err + res.IsAdmin = true + res.Users = user + return res, err } // default nickname if user.NickName == nil || *user.NickName == "" { @@ -57,7 +60,8 @@ func (s *Service) GetUserInfo(address string, loginAddress string) (res interfac user.NickName = &nickName } } - return user, err + res.Users = user + return res, err } func (s *Service) UpdateUserInfo(address string, user request.UpdateUserInfo) (err error) {