From 9809981fef7e1a8511165f9328b4578fc154f022 Mon Sep 17 00:00:00 2001 From: yuming88 Date: Tue, 6 May 2025 20:34:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A=E6=9F=A5=E8=AF=A2ca?= =?UTF-8?q?stile=E4=BB=A3=E5=B8=81=E4=BD=99=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api/transfercastile.api | 9 +++ internal/handler/routes.go | 6 ++ .../get_castile_balance_handler.go | 22 +++++++ .../get_castile_balance_logic.go | 61 +++++++++++++++++++ internal/types/types.go | 5 ++ 5 files changed, 103 insertions(+) create mode 100644 internal/handler/transfercastile/get_castile_balance_handler.go create mode 100644 internal/logic/transfercastile/get_castile_balance_logic.go diff --git a/doc/api/transfercastile.api b/doc/api/transfercastile.api index 9efa599..abd6766 100644 --- a/doc/api/transfercastile.api +++ b/doc/api/transfercastile.api @@ -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` //已转回游戏内的数量 +} + diff --git a/internal/handler/routes.go b/internal/handler/routes.go index f4c0b02..c48f352 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -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, diff --git a/internal/handler/transfercastile/get_castile_balance_handler.go b/internal/handler/transfercastile/get_castile_balance_handler.go new file mode 100644 index 0000000..41a44ca --- /dev/null +++ b/internal/handler/transfercastile/get_castile_balance_handler.go @@ -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) + } + } +} diff --git a/internal/logic/transfercastile/get_castile_balance_logic.go b/internal/logic/transfercastile/get_castile_balance_logic.go new file mode 100644 index 0000000..fb56f1e --- /dev/null +++ b/internal/logic/transfercastile/get_castile_balance_logic.go @@ -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 +} diff --git a/internal/types/types.go b/internal/types/types.go index acd34f5..16d8745 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -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图片