Skip to content

Commit

Permalink
Merge pull request #149 from decert-me/fix/social-bind
Browse files Browse the repository at this point in the history
Fix 微信绑定替换删除绑定信息
  • Loading branch information
0xdwong authored Jul 23, 2024
2 parents 8695aba + 463d803 commit d85ed34
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 8 deletions.
19 changes: 19 additions & 0 deletions internal/app/api/v1/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion internal/app/api/v2/quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/app/assets/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "操作成功!",
Expand Down Expand Up @@ -79,6 +80,7 @@
"EmailAlreadyLinked": "邮箱已绑定其他钱包地址",
"GithubAlreadyLinked": "Github 已绑定其他钱包地址",
"AddressAlreadyLinkedGithub": "钱包地址已绑定Github,请勿重复操作",
"FailedObtainGithubInfo": "绑定 Github 信息失败!"
"FailedObtainGithubInfo": "绑定 Github 信息失败!",
"PermissionDenied": "权限不足!"
}
}
11 changes: 11 additions & 0 deletions internal/app/dao/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions internal/app/model/request/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
5 changes: 5 additions & 0 deletions internal/app/model/response/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ type QuestWithClaimed struct {
func (QuestWithClaimed) TableName() string {
return "quest"
}

type GetUserInfoRes struct {
model.Users
IsAdmin bool `json:"is_admin"`
}
3 changes: 2 additions & 1 deletion internal/app/router/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) // 解绑
}
}
8 changes: 8 additions & 0 deletions internal/app/service/quest_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion internal/app/service/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
10 changes: 7 additions & 3 deletions internal/app/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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) {
Expand Down

0 comments on commit d85ed34

Please sign in to comment.