增加:查询castile代币余额

This commit is contained in:
yuming88
2025-05-06 20:34:01 +08:00
parent 95a4337ab3
commit 9809981fef
5 changed files with 103 additions and 0 deletions

View File

@@ -13,6 +13,10 @@ service novatask {
@doc "获取提取castile到游戏的记录"
@handler TransferCastileToGameList
post /list (TransferCastileToGameListReq) returns (TransferCastileToGameListResp)
@doc "查询castile代币余额"
@handler GetCastileBalance
post /getBalance returns (UserCastileBalanceResp)
}
type TransferCastileToGameReq {
@@ -39,3 +43,8 @@ type TransferCastileToGameListResp {
List []TransferCastileToGameResp `json:"list"` // 列表
}
type UserCastileBalanceResp {
TotalCastile int `json:total_castile` //总数
TransferAmount int `json:transfer_amount` //已转回游戏内的数量
}

View File

@@ -239,6 +239,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
// 查询castile代币余额
Method: http.MethodPost,
Path: "/getBalance",
Handler: transfercastile.GetCastileBalanceHandler(serverCtx),
},
{
// 获取提取castile到游戏的记录
Method: http.MethodPost,

View File

@@ -0,0 +1,22 @@
package transfercastile
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"nova_task/internal/logic/transfercastile"
"nova_task/internal/svc"
)
// 查询castile代币余额
func GetCastileBalanceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := transfercastile.NewGetCastileBalanceLogic(r.Context(), svcCtx)
resp, err := l.GetCastileBalance()
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,61 @@
package transfercastile
import (
"context"
"errors"
"nova_task/internal/model"
"nova_task/internal/pkg/errs"
"nova_task/internal/pkg/utils"
"nova_task/internal/svc"
"nova_task/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCastileBalanceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 查询castile代币余额
func NewGetCastileBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCastileBalanceLogic {
return &GetCastileBalanceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCastileBalanceLogic) GetCastileBalance() (resp *types.UserCastileBalanceResp, err error) {
//获取JWT中的UID
uid := utils.GetUidUint(l.ctx)
u, err := l.svcCtx.UserModel.FindOne(l.ctx, uid)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return nil, errs.New(errs.ErrUserNotFound, "user not found")
}
l.Errorw("find user error", logx.Field("err", err), logx.Field("uid", uid))
return nil, errs.New(errs.ErrUserNotFound, err)
}
res := &types.UserCastileBalanceResp{
TotalCastile: 0,
TransferAmount: 0,
}
cToken, err := l.svcCtx.CastileTokenModel.FindOneByEmail(l.ctx, u.Email)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return res, nil
}
return nil, errs.New(errs.ErrDatabaseOperate, err)
}
return &types.UserCastileBalanceResp{
TotalCastile: int(cToken.Total),
TransferAmount: int(cToken.Transfer),
}, nil
}

View File

@@ -276,6 +276,11 @@ type UnlockChapterReq struct {
ApiKey string `header:"x-api-key"` // x-api-key
}
type UserCastileBalanceResp struct {
TotalCastile int `json:total_castile` //总数
TransferAmount int `json:transfer_amount` //已转回游戏内的数量
}
type UserNft struct {
TokenId string `json:"token_id"` // nftID
Image string `json:"image"` // nft图片