From 32b8efe5ad545ab8d128b298848acfb5bc87089f Mon Sep 17 00:00:00 2001 From: Rogee Date: Thu, 19 Dec 2024 10:22:25 +0800 Subject: [PATCH] fix: issues --- backend/modules/medias/controller.go | 13 +++++++++++++ backend/modules/users/controller.go | 13 +++++++++++++ backend/pkg/errorx/error.go | 1 + backend/providers/http/engine.go | 2 +- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/modules/medias/controller.go b/backend/modules/medias/controller.go index ce6d0d8..03cb563 100644 --- a/backend/modules/medias/controller.go +++ b/backend/modules/medias/controller.go @@ -2,6 +2,7 @@ package medias import ( "backend/pkg/consts" + "backend/pkg/errorx" "backend/pkg/pg" "backend/providers/jwt" @@ -48,6 +49,9 @@ func (c *Controller) Show(ctx fiber.Ctx) error { hash := ctx.Params("hash") claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash) if err != nil { @@ -75,6 +79,9 @@ func (c *Controller) MediaIndex(ctx fiber.Ctx) error { hash := ctx.Params("hash") claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash) if err != nil { @@ -113,6 +120,9 @@ func (c *Controller) MediaSegment(ctx fiber.Ctx) error { hash := ctx.Params("hash") claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash) if err != nil { @@ -129,6 +139,9 @@ func (c *Controller) Checkout(ctx fiber.Ctx) error { hash := ctx.Params("hash") claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash) if err != nil { diff --git a/backend/modules/users/controller.go b/backend/modules/users/controller.go index f38057f..f22f302 100644 --- a/backend/modules/users/controller.go +++ b/backend/modules/users/controller.go @@ -26,6 +26,9 @@ func (c *Controller) List(ctx fiber.Ctx) error { func (c *Controller) Charge(ctx fiber.Ctx) error { claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } // [tenantId, chargeAmount, timestamp] code := ctx.Params("code") @@ -41,6 +44,10 @@ func (c *Controller) Info(ctx fiber.Ctx) error { claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } + info := &UserInfo{} balance, err := c.svc.GetTenantUserBalance(ctx.Context(), claim.TenantID, claim.UserID) @@ -62,6 +69,9 @@ func (c *Controller) Info(ctx fiber.Ctx) error { func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error { claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } tenant, err := c.svc.GetTenantByID(ctx.Context(), claim.TenantID) if err != nil { @@ -98,6 +108,9 @@ func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error { func (c *Controller) BalanceHistory(ctx fiber.Ctx) error { claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim) log.Debug(claim) + if claim == nil { + return errorx.RequestUnAuthorized + } histories, err := c.svc.GetBalanceHistory(ctx.Context(), claim.TenantID, claim.UserID) if err != nil { diff --git a/backend/pkg/errorx/error.go b/backend/pkg/errorx/error.go index 797b45a..b0c7eb0 100644 --- a/backend/pkg/errorx/error.go +++ b/backend/pkg/errorx/error.go @@ -27,6 +27,7 @@ func (r Response) Response(ctx fiber.Ctx) error { var ( RequestParseError = Response{http.StatusBadRequest, http.StatusBadRequest, "请求解析错误"} + RequestUnAuthorized = Response{http.StatusUnauthorized, http.StatusUnauthorized, "未授权"} InternalError = Response{http.StatusInternalServerError, http.StatusInternalServerError, "内部错误"} UserBalanceNotEnough = Response{http.StatusPaymentRequired, 1001, "余额不足,请充值"} InvalidChargeCode = Response{http.StatusPaymentRequired, 1002, "无效的充值码"} diff --git a/backend/providers/http/engine.go b/backend/providers/http/engine.go index a5763aa..19a4996 100644 --- a/backend/providers/http/engine.go +++ b/backend/providers/http/engine.go @@ -70,7 +70,7 @@ func Provide(opts ...opt.Option) error { engine.Use(recover.New(recover.Config{ EnableStackTrace: true, StackTraceHandler: func(c fiber.Ctx, e any) { - log.WithError(e.(error)).Error(fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack())) + log.Error(fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack())) }, }))