Skip to content

Commit

Permalink
Merge pull request #147 from decert-me/feature/social-rebinding
Browse files Browse the repository at this point in the history
Feature  换绑社交账号
  • Loading branch information
0xdwong authored Jul 19, 2024
2 parents e46124e + 2c5443d commit 8695aba
Show file tree
Hide file tree
Showing 26 changed files with 421 additions and 146 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/bits-and-blooms/bitset v1.5.0 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGn
github.com/alicebob/miniredis/v2 v2.30.0 h1:uA3uhDbCxfO9+DI/DuGeAMr9qI+noVWwGPNTFuKID5M=
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129/go.mod h1:rFgpPQZYZ8vdbc+48xibu8ALc3yeyd64IhHS+PU6Yyg=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
Expand Down
80 changes: 71 additions & 9 deletions internal/app/api/v1/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"backend-go/internal/app/model/request"
"fmt"
"github.com/gin-gonic/gin"
)

Expand All @@ -27,14 +26,15 @@ func WechatBindAddress(c *gin.Context) {
type WechatBind struct {
Address string `json:"address" form:"address" binding:"required"`
Code string `json:"code" form:"code" binding:"required"`
Replace bool `json:"replace" form:"replace"`
}
var wechatBind WechatBind
err := c.ShouldBindJSON(&wechatBind)
if err != nil {
FailWithMessage(GetMessage(c, "ParameterError"), c)
return
}
if err := srv.WechatBindAddress(c, wechatBind.Address, wechatBind.Code); err != nil {
if err := srv.WechatBindAddress(c, wechatBind.Address, wechatBind.Code, wechatBind.Replace); err != nil {
FailWithMessage(err.Error(), c)
} else {
Ok(c)
Expand All @@ -56,14 +56,15 @@ func DiscordBindAddress(c *gin.Context) {
type DiscordCallback struct {
Code string `json:"code" form:"code"`
Callback string `json:"callback" form:"callback"`
Replace bool `json:"replace" form:"replace"`
}
var discordCallback DiscordCallback
_ = c.ShouldBindJSON(&discordCallback)
address := c.GetString("address")
if err := srv.DiscordCallback(address, discordCallback); err != nil {
if data, err := srv.DiscordCallback(address, discordCallback, discordCallback.Replace); err != nil {
OkWithMessage(GetMessage(c, err.Error()), c)
} else {
OkWithMessage("", c)
OkWithData(data, c)
}
}

Expand Down Expand Up @@ -91,7 +92,6 @@ func GetEmailBindCode(c *gin.Context) {
func EmailBindAddress(c *gin.Context) {
var r request.EmailBindAddressRequest
if err := c.ShouldBindJSON(&r); err != nil {
fmt.Println(err)
FailWithMessage(GetMessage(c, "ParameterError"), c)
return
}
Expand All @@ -100,10 +100,14 @@ func EmailBindAddress(c *gin.Context) {
FailWithMessage(GetMessage(c, "SignatureExpired"), c)
return
}
if err := srv.EmailBindAddress(address, r.Email, r.Code); err != nil {
if data, err := srv.EmailBindAddress(address, r.Email, r.Code, r.Replace); err != nil {
FailWithMessage(GetMessage(c, err.Error()), c)
} else {
Ok(c)
message := "操作成功"
if data.Success == false {
message = "绑定失败"
}
OkWithDetailed(data, message, c)
}
}

Expand All @@ -122,13 +126,71 @@ func GithubBindAddress(c *gin.Context) {
type GithubCallback struct {
Code string `json:"code" form:"code"`
Callback string `json:"callback" form:"callback"`
Replace bool `json:"replace" form:"replace"`
}
var githubCallback GithubCallback
_ = c.ShouldBindJSON(&githubCallback)
address := c.GetString("address")
if err := srv.GithubCallback(address, githubCallback); err != nil {
if data, err := srv.GithubCallback(address, githubCallback, githubCallback.Replace); err != nil {
OkWithMessage(GetMessage(c, err.Error()), c)
} else {
OkWithMessage("", c)
OkWithData(data, c)
}
}

// UnbindSocial 解绑
func UnbindSocial(c *gin.Context) {
var r request.UnbindRequest
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.UnbindSocial(address, r.Type); err != nil {
FailWithMessage(GetMessage(c, err.Error()), c)
} else {
Ok(c)
}
}

// BindSocialResult 查询绑定结果
func BindSocialResult(c *gin.Context) {
var r request.BindSocialResultRequest
if err := c.ShouldBindJSON(&r); err != nil {
FailWithMessage(GetMessage(c, "ParameterError"), c)
return
}
address := c.GetString("address")
if address == "" {
Fail(c)
return
}
if data, err := srv.BindSocialResult(address, r.Type); err != nil {
FailWithMessage(GetMessage(c, err.Error()), c)
} else {
OkWithData(data, c)
}
}

// ConfirmBindChange 确认绑定变更
func ConfirmBindChange(c *gin.Context) {
var r request.ConfirmBindChangeRequest
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.ConfirmBindChange(address, r.Type); err != nil {
FailWithMessage(GetMessage(c, err.Error()), c)
} else {
Ok(c)
}
}
2 changes: 0 additions & 2 deletions internal/app/api/v1/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1
import (
"backend-go/internal/app/model/request"
"backend-go/internal/app/model/response"
"fmt"
"github.com/gin-gonic/gin"
)

Expand All @@ -14,7 +13,6 @@ func GetProgressList(c *gin.Context) {
return
}
userID := c.GetUint("userID")
fmt.Println("userID", userID)
if data, err := srv.GetProgressList(userID, req); err != nil {
FailWithMessage(GetMessage(c, "FetchFailed"), c)
} else {
Expand Down
2 changes: 0 additions & 2 deletions internal/app/api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"backend-go/internal/app/model/request"
"backend-go/internal/app/model/response"
"backend-go/internal/app/utils"
"fmt"
"github.com/gin-gonic/gin"
"path"
"strings"
Expand Down Expand Up @@ -156,7 +155,6 @@ func AuthLoginSign(c *gin.Context) {
FailWithMessage(GetMessage(c, "ParameterError"), c)
return
}
fmt.Println(request)
var token string
var err error
if utils.IsValidAddress(request.Address) {
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 @@ -37,7 +37,8 @@
"AddressAlreadyLinkedEmail": "The wallet address has already been linked to Email. Please do not repeat the operation.",
"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."
"AddressAlreadyLinkedGithub": "The wallet address has already been linked to Github. Please do not repeat the operation.",
"FailedObtainGithubInfo": "Failed to obtain Github information!"
},
"zh-CN": {
"OperationSuccess": "操作成功!",
Expand Down Expand Up @@ -77,6 +78,7 @@
"AddressAlreadyLinkedEmail": "钱包地址已绑定邮箱,请勿重复操作",
"EmailAlreadyLinked": "邮箱已绑定其他钱包地址",
"GithubAlreadyLinked": "Github 已绑定其他钱包地址",
"AddressAlreadyLinkedGithub": "钱包地址已绑定Github,请勿重复操作"
"AddressAlreadyLinkedGithub": "钱包地址已绑定Github,请勿重复操作",
"FailedObtainGithubInfo": "绑定 Github 信息失败!"
}
}
2 changes: 0 additions & 2 deletions internal/app/dao/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"backend-go/internal/app/model/request"
"backend-go/internal/app/model/response"
"errors"
"fmt"
"github.com/spf13/cast"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -356,6 +355,5 @@ func (d *Dao) GetCollectionHolderRank(address string, id int64, page, pageSize i
Order("user_challenges.add_ts ASC,user_challenges.id ASC").
Limit(limit).Offset(offset).
Find(&res).Error
fmt.Println(res)
return res, total, err
}
4 changes: 0 additions & 4 deletions internal/app/dao/quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ func (d *Dao) GetQuestList(req *request.GetQuestListRequest) (questList []respon
var estimateTimeTotal int64
for _, quest := range collectionQuestList {
estimateTimeTotal += gjson.Get(string(quest.QuestData), "estimateTime").Int()
fmt.Println("estimateTimeTotal", estimateTimeTotal)
}

fmt.Println("estimateTimeTotal", estimateTimeTotal)
if estimateTimeTotal != 0 {
questList[i].EstimateTime = estimateTimeTotal
}
Expand Down Expand Up @@ -603,7 +600,6 @@ func (d *Dao) GetQuestHolderRankByTokenID(address string, tokenId string, page,
Order("user_challenges.add_ts ASC,user_challenges.id ASC").
Limit(limit).Offset(offset).
Find(&res).Error
fmt.Println(res)
return res, total, err
}

Expand Down
86 changes: 57 additions & 29 deletions internal/app/dao/social.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"gorm.io/gorm"
"math/rand"
"time"
)
Expand All @@ -18,16 +19,19 @@ func (d *Dao) WechatQueryByAddress(address string) (wechatData string, err error
}

// WechatIsBinding 判断是否已经绑定过
func (d *Dao) WechatIsBinding(fromUserName string) (bool, error) {
var count int
err := d.db.Raw("SELECT count(1) FROM users WHERE socials->'wechat'->>'openid' = ?", fromUserName).Scan(&count).Error
func (d *Dao) WechatIsBinding(fromUserName string) (string, bool, error) {
var address string
err := d.db.Raw("SELECT address FROM users WHERE socials->'wechat'->>'openid' = ? LIMIT 1", fromUserName).Scan(&address).Error
if err != nil {
return false, err
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", false, nil
}
return "", false, err
}
if count == 0 {
return false, nil
if address == "" {
return "", false, nil
}
return true, nil
return address, true, nil
}

// WechatBindAddress 处理地址绑定
Expand Down Expand Up @@ -95,37 +99,43 @@ func (d *Dao) DiscordBindAddress(discordID, username, address string) (err error

// DiscordQueryByAddress 查询地址绑定的discord信息
func (d *Dao) DiscordQueryByAddress(address string) (discordData string, err error) {
err = d.db.Raw("SELECT COALESCE(socials->'discord', '{}') FROM users WHERE address = ? LIMIT 1", address).Scan(&discordData).Error
err = d.db.Raw("SELECT COALESCE(socials->'discord', '{}') FROM users WHERE address = ? LIMIT 1", address).First(&discordData).Error
if err != nil {
return discordData, err
}
return discordData, err
}

// DiscordIsBinding 判断Discord是否已经绑定过
func (d *Dao) DiscordIsBinding(discordID string) (bool, error) {
var count int
err := d.db.Raw("SELECT count(1) FROM users WHERE socials->'discord'->>'id' = ?", discordID).Scan(&count).Error
func (d *Dao) DiscordIsBinding(discordID string) (string, bool, error) {
var address string
err := d.db.Raw("SELECT address FROM users WHERE socials->'discord'->>'id' = ? LIMIT 1", discordID).Scan(&address).Error
if err != nil {
return false, err
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", false, nil
}
return "", false, err
}
if count == 0 {
return false, nil
if address == "" {
return "", false, nil
}
return true, nil
return address, true, nil
}

// EmailIsBinding 判断邮箱是否已经绑定过
func (d *Dao) EmailIsBinding(email string) (bool, error) {
var count int
err := d.db.Raw("SELECT count(1) FROM users WHERE socials->>'email' = ?", email).Scan(&count).Error
func (d *Dao) EmailIsBinding(email string) (string, bool, error) {
var address string
err := d.db.Raw("SELECT address FROM users WHERE socials->>'email' = ? LIMIT 1", email).Scan(&address).Error
if err != nil {
return false, err
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", false, nil
}
return "", false, err
}
if count == 0 {
return false, nil
if address == "" {
return "", false, nil
}
return true, nil
return address, true, nil
}

// EmailQueryByAddress 查询地址绑定的邮箱信息
Expand Down Expand Up @@ -192,16 +202,19 @@ func (d *Dao) GithubQueryByAddress(address string) (githubData string, err error
}

// GithubIsBinding 判断Github是否已经绑定过
func (d *Dao) GithubIsBinding(githubID string) (bool, error) {
var count int
err := d.db.Raw("SELECT count(1) FROM users WHERE socials->'github'->>'id' = ?", githubID).Scan(&count).Error
func (d *Dao) GithubIsBinding(githubID string) (string, bool, error) {
var address string
err := d.db.Raw("SELECT address FROM users WHERE socials->'github'->>'id' = ? LIMIT 1", githubID).Scan(&address).Error
if err != nil {
return false, err
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", false, nil
}
return "", false, err
}
if count == 0 {
return false, nil
if address == "" {
return "", false, nil
}
return true, nil
return address, true, nil
}

// GithubBindAddress 处理地址绑定
Expand Down Expand Up @@ -234,3 +247,18 @@ func (d *Dao) GithubBindAddress(githubID, username, address string) (err error)
return err
}
}

// UnbindSocial 解绑
func (d *Dao) UnbindSocial(address, social string) error {
return d.db.Exec("UPDATE users SET socials = socials - ? WHERE address = ?", social, address).Error
}

func (d *Dao) SaveRebindingInfo(token, info interface{}) (err error) {
key := fmt.Sprintf("%s_rebinding_%s", d.c.Redis.Prefix, token)
return d.redis.Set(context.Background(), key, info, time.Minute*30).Err()
}

func (d *Dao) GetRebindingInfo(token string) (info string, err error) {
key := fmt.Sprintf("%s_rebinding_%s", d.c.Redis.Prefix, token)
return d.redis.Get(context.Background(), key).Result()
}
Loading

0 comments on commit 8695aba

Please sign in to comment.