From e6a8e3f321787ece572b5765f58ff1bb0e1b241d Mon Sep 17 00:00:00 2001 From: Rogee Date: Tue, 30 Dec 2025 23:01:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E5=A4=84=E7=90=86=EF=BC=8C=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=96=B9=E6=B3=95=E6=98=BE=E5=BC=8F=E6=8E=A5=E5=8F=97?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=82=E6=95=B0=EF=BC=8C=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/http/v1/common.go | 14 ++-- backend/app/http/v1/creator.go | 95 ++++++++++++---------- backend/app/http/v1/routes.gen.go | 108 ++++++++++++++++--------- backend/app/http/v1/tenant.go | 23 +++--- backend/app/http/v1/transaction.go | 24 ++++-- backend/app/http/v1/user.go | 98 +++++++++++----------- backend/app/middlewares/middlewares.go | 5 +- 7 files changed, 211 insertions(+), 156 deletions(-) diff --git a/backend/app/http/v1/common.go b/backend/app/http/v1/common.go index a0e74ba..44fe5ee 100644 --- a/backend/app/http/v1/common.go +++ b/backend/app/http/v1/common.go @@ -5,10 +5,9 @@ import ( "quyun/v2/app/http/v1/dto" "quyun/v2/app/services" - "quyun/v2/pkg/consts" + "quyun/v2/database/models" "github.com/gofiber/fiber/v3" - "github.com/spf13/cast" ) // @provider @@ -25,13 +24,18 @@ type Common struct{} // @Param file formData file true "File" // @Param type formData string false "Type enum(image, video, audio)" // @Success 200 {object} dto.UploadResult +// @Bind user local key(__ctx_user) // @Bind file file // @Bind typeArg body key(type) -func (c *Common) Upload(ctx fiber.Ctx, file *multipart.FileHeader, typeArg *string) (*dto.UploadResult, error) { +func (c *Common) Upload( + ctx fiber.Ctx, + user *models.User, + file *multipart.FileHeader, + typeArg *string, +) (*dto.UploadResult, error) { val := "" if typeArg != nil { val = *typeArg } - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Common.Upload(ctx.Context(), uid, file, val) + return services.Common.Upload(ctx.Context(), user.ID, file, val) } diff --git a/backend/app/http/v1/creator.go b/backend/app/http/v1/creator.go index c0d9b1b..28752da 100644 --- a/backend/app/http/v1/creator.go +++ b/backend/app/http/v1/creator.go @@ -3,10 +3,9 @@ package v1 import ( "quyun/v2/app/http/v1/dto" "quyun/v2/app/services" - "quyun/v2/pkg/consts" + "quyun/v2/database/models" "github.com/gofiber/fiber/v3" - "github.com/spf13/cast" ) // @provider @@ -22,10 +21,10 @@ type Creator struct{} // @Produce json // @Param form body dto.ApplyForm true "Apply form" // @Success 200 {string} string "Application submitted" +// @Bind user local key(__ctx_user) // @Bind form body -func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.Apply(ctx.Context(), uid, form) +func (c *Creator) Apply(ctx fiber.Ctx, user *models.User, form *dto.ApplyForm) error { + return services.Creator.Apply(ctx.Context(), user.ID, form) } // Get creator dashboard stats @@ -37,9 +36,9 @@ func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error { // @Accept json // @Produce json // @Success 200 {object} dto.DashboardStats -func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.Dashboard(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (c *Creator) Dashboard(ctx fiber.Ctx, user *models.User) (*dto.DashboardStats, error) { + return services.Creator.Dashboard(ctx.Context(), user.ID) } // List creator contents @@ -54,10 +53,14 @@ func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) { // @Param genre query string false "Genre" // @Param keyword query string false "Keyword" // @Success 200 {array} dto.ContentItem +// @Bind user local key(__ctx_user) // @Bind filter query -func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilter) ([]dto.ContentItem, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.ListContents(ctx.Context(), uid, filter) +func (c *Creator) ListContents( + ctx fiber.Ctx, + user *models.User, + filter *dto.CreatorContentListFilter, +) ([]dto.ContentItem, error) { + return services.Creator.ListContents(ctx.Context(), user.ID, filter) } // Create/Publish content @@ -70,10 +73,10 @@ func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilt // @Produce json // @Param form body dto.ContentCreateForm true "Content form" // @Success 200 {string} string "Created" +// @Bind user local key(__ctx_user) // @Bind form body -func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.CreateContent(ctx.Context(), uid, form) +func (c *Creator) CreateContent(ctx fiber.Ctx, user *models.User, form *dto.ContentCreateForm) error { + return services.Creator.CreateContent(ctx.Context(), user.ID, form) } // Update content @@ -87,11 +90,11 @@ func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) erro // @Param id path string true "Content ID" // @Param form body dto.ContentUpdateForm true "Update form" // @Success 200 {string} string "Updated" +// @Bind user local key(__ctx_user) // @Bind id path // @Bind form body -func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdateForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.UpdateContent(ctx.Context(), uid, id, form) +func (c *Creator) UpdateContent(ctx fiber.Ctx, user *models.User, id string, form *dto.ContentUpdateForm) error { + return services.Creator.UpdateContent(ctx.Context(), user.ID, id, form) } // Delete content @@ -104,10 +107,10 @@ func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdat // @Produce json // @Param id path string true "Content ID" // @Success 200 {string} string "Deleted" +// @Bind user local key(__ctx_user) // @Bind id path -func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.DeleteContent(ctx.Context(), uid, id) +func (c *Creator) DeleteContent(ctx fiber.Ctx, user *models.User, id string) error { + return services.Creator.DeleteContent(ctx.Context(), user.ID, id) } // List sales orders @@ -121,10 +124,14 @@ func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error { // @Param status query string false "Status" // @Param keyword query string false "Keyword" // @Success 200 {array} dto.Order +// @Bind user local key(__ctx_user) // @Bind filter query -func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter) ([]dto.Order, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.ListOrders(ctx.Context(), uid, filter) +func (c *Creator) ListOrders( + ctx fiber.Ctx, + user *models.User, + filter *dto.CreatorOrderListFilter, +) ([]dto.Order, error) { + return services.Creator.ListOrders(ctx.Context(), user.ID, filter) } // Process refund @@ -138,11 +145,11 @@ func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter) // @Param id path string true "Order ID" // @Param form body dto.RefundForm true "Refund form" // @Success 200 {string} string "Processed" +// @Bind user local key(__ctx_user) // @Bind id path // @Bind form body -func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.ProcessRefund(ctx.Context(), uid, id, form) +func (c *Creator) Refund(ctx fiber.Ctx, user *models.User, id string, form *dto.RefundForm) error { + return services.Creator.ProcessRefund(ctx.Context(), user.ID, id, form) } // Get channel settings @@ -154,9 +161,9 @@ func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error { // @Accept json // @Produce json // @Success 200 {object} dto.Settings -func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.GetSettings(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (c *Creator) GetSettings(ctx fiber.Ctx, user *models.User) (*dto.Settings, error) { + return services.Creator.GetSettings(ctx.Context(), user.ID) } // Update channel settings @@ -169,10 +176,10 @@ func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) { // @Produce json // @Param form body dto.Settings true "Settings form" // @Success 200 {string} string "Updated" +// @Bind user local key(__ctx_user) // @Bind form body -func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.UpdateSettings(ctx.Context(), uid, form) +func (c *Creator) UpdateSettings(ctx fiber.Ctx, user *models.User, form *dto.Settings) error { + return services.Creator.UpdateSettings(ctx.Context(), user.ID, form) } // List payout accounts @@ -184,9 +191,9 @@ func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error { // @Accept json // @Produce json // @Success 200 {array} dto.PayoutAccount -func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.ListPayoutAccounts(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx, user *models.User) ([]dto.PayoutAccount, error) { + return services.Creator.ListPayoutAccounts(ctx.Context(), user.ID) } // Add payout account @@ -199,10 +206,10 @@ func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error) // @Produce json // @Param form body dto.PayoutAccount true "Account form" // @Success 200 {string} string "Added" +// @Bind user local key(__ctx_user) // @Bind form body -func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.AddPayoutAccount(ctx.Context(), uid, form) +func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, user *models.User, form *dto.PayoutAccount) error { + return services.Creator.AddPayoutAccount(ctx.Context(), user.ID, form) } // Remove payout account @@ -215,10 +222,10 @@ func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error // @Produce json // @Param id query string true "Account ID" // @Success 200 {string} string "Removed" +// @Bind user local key(__ctx_user) // @Bind id query -func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.RemovePayoutAccount(ctx.Context(), uid, id) +func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, user *models.User, id string) error { + return services.Creator.RemovePayoutAccount(ctx.Context(), user.ID, id) } // Request withdrawal @@ -231,8 +238,8 @@ func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error { // @Produce json // @Param form body dto.WithdrawForm true "Withdraw form" // @Success 200 {string} string "Withdrawal requested" +// @Bind user local key(__ctx_user) // @Bind form body -func (c *Creator) Withdraw(ctx fiber.Ctx, form *dto.WithdrawForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Creator.Withdraw(ctx.Context(), uid, form) +func (c *Creator) Withdraw(ctx fiber.Ctx, user *models.User, form *dto.WithdrawForm) error { + return services.Creator.Withdraw(ctx.Context(), user.ID, form) } diff --git a/backend/app/http/v1/routes.gen.go b/backend/app/http/v1/routes.gen.go index 1a91d8c..50228f8 100644 --- a/backend/app/http/v1/routes.gen.go +++ b/backend/app/http/v1/routes.gen.go @@ -51,8 +51,9 @@ func (r *Routes) Name() string { func (r *Routes) Register(router fiber.Router) { // Register routes for controller: Common r.log.Debugf("Registering route: Post /v1/upload -> common.Upload") - router.Post("/v1/upload"[len(r.Path()):], DataFunc2( + router.Post("/v1/upload"[len(r.Path()):], DataFunc3( r.common.Upload, + Local[*models.User]("__ctx_user"), File[multipart.FileHeader]("file"), Body[string]("type"), )) @@ -90,72 +91,86 @@ func (r *Routes) Register(router fiber.Router) { )) // Register routes for controller: Creator r.log.Debugf("Registering route: Delete /v1/creator/contents/:id -> creator.DeleteContent") - router.Delete("/v1/creator/contents/:id"[len(r.Path()):], Func1( + router.Delete("/v1/creator/contents/:id"[len(r.Path()):], Func2( r.creator.DeleteContent, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), )) r.log.Debugf("Registering route: Delete /v1/creator/payout-accounts -> creator.RemovePayoutAccount") - router.Delete("/v1/creator/payout-accounts"[len(r.Path()):], Func1( + router.Delete("/v1/creator/payout-accounts"[len(r.Path()):], Func2( r.creator.RemovePayoutAccount, + Local[*models.User]("__ctx_user"), QueryParam[string]("id"), )) r.log.Debugf("Registering route: Get /v1/creator/contents -> creator.ListContents") - router.Get("/v1/creator/contents"[len(r.Path()):], DataFunc1( + router.Get("/v1/creator/contents"[len(r.Path()):], DataFunc2( r.creator.ListContents, + Local[*models.User]("__ctx_user"), Query[dto.CreatorContentListFilter]("filter"), )) r.log.Debugf("Registering route: Get /v1/creator/dashboard -> creator.Dashboard") - router.Get("/v1/creator/dashboard"[len(r.Path()):], DataFunc0( + router.Get("/v1/creator/dashboard"[len(r.Path()):], DataFunc1( r.creator.Dashboard, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/creator/orders -> creator.ListOrders") - router.Get("/v1/creator/orders"[len(r.Path()):], DataFunc1( + router.Get("/v1/creator/orders"[len(r.Path()):], DataFunc2( r.creator.ListOrders, + Local[*models.User]("__ctx_user"), Query[dto.CreatorOrderListFilter]("filter"), )) r.log.Debugf("Registering route: Get /v1/creator/payout-accounts -> creator.ListPayoutAccounts") - router.Get("/v1/creator/payout-accounts"[len(r.Path()):], DataFunc0( + router.Get("/v1/creator/payout-accounts"[len(r.Path()):], DataFunc1( r.creator.ListPayoutAccounts, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/creator/settings -> creator.GetSettings") - router.Get("/v1/creator/settings"[len(r.Path()):], DataFunc0( + router.Get("/v1/creator/settings"[len(r.Path()):], DataFunc1( r.creator.GetSettings, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Post /v1/creator/apply -> creator.Apply") - router.Post("/v1/creator/apply"[len(r.Path()):], Func1( + router.Post("/v1/creator/apply"[len(r.Path()):], Func2( r.creator.Apply, + Local[*models.User]("__ctx_user"), Body[dto.ApplyForm]("form"), )) r.log.Debugf("Registering route: Post /v1/creator/contents -> creator.CreateContent") - router.Post("/v1/creator/contents"[len(r.Path()):], Func1( + router.Post("/v1/creator/contents"[len(r.Path()):], Func2( r.creator.CreateContent, + Local[*models.User]("__ctx_user"), Body[dto.ContentCreateForm]("form"), )) r.log.Debugf("Registering route: Post /v1/creator/orders/:id/refund -> creator.Refund") - router.Post("/v1/creator/orders/:id/refund"[len(r.Path()):], Func2( + router.Post("/v1/creator/orders/:id/refund"[len(r.Path()):], Func3( r.creator.Refund, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), Body[dto.RefundForm]("form"), )) r.log.Debugf("Registering route: Post /v1/creator/payout-accounts -> creator.AddPayoutAccount") - router.Post("/v1/creator/payout-accounts"[len(r.Path()):], Func1( + router.Post("/v1/creator/payout-accounts"[len(r.Path()):], Func2( r.creator.AddPayoutAccount, + Local[*models.User]("__ctx_user"), Body[dto.PayoutAccount]("form"), )) r.log.Debugf("Registering route: Post /v1/creator/withdraw -> creator.Withdraw") - router.Post("/v1/creator/withdraw"[len(r.Path()):], Func1( + router.Post("/v1/creator/withdraw"[len(r.Path()):], Func2( r.creator.Withdraw, + Local[*models.User]("__ctx_user"), Body[dto.WithdrawForm]("form"), )) r.log.Debugf("Registering route: Put /v1/creator/contents/:id -> creator.UpdateContent") - router.Put("/v1/creator/contents/:id"[len(r.Path()):], Func2( + router.Put("/v1/creator/contents/:id"[len(r.Path()):], Func3( r.creator.UpdateContent, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), Body[dto.ContentUpdateForm]("form"), )) r.log.Debugf("Registering route: Put /v1/creator/settings -> creator.UpdateSettings") - router.Put("/v1/creator/settings"[len(r.Path()):], Func1( + router.Put("/v1/creator/settings"[len(r.Path()):], Func2( r.creator.UpdateSettings, + Local[*models.User]("__ctx_user"), Body[dto.Settings]("form"), )) // Register routes for controller: Storage @@ -175,18 +190,21 @@ func (r *Routes) Register(router fiber.Router) { )) // Register routes for controller: Tenant r.log.Debugf("Registering route: Delete /v1/tenants/:id/follow -> tenant.Unfollow") - router.Delete("/v1/tenants/:id/follow"[len(r.Path()):], Func1( + router.Delete("/v1/tenants/:id/follow"[len(r.Path()):], Func2( r.tenant.Unfollow, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), )) r.log.Debugf("Registering route: Get /v1/tenants/:id -> tenant.Get") - router.Get("/v1/tenants/:id"[len(r.Path()):], DataFunc1( + router.Get("/v1/tenants/:id"[len(r.Path()):], DataFunc2( r.tenant.Get, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), )) r.log.Debugf("Registering route: Post /v1/tenants/:id/follow -> tenant.Follow") - router.Post("/v1/tenants/:id/follow"[len(r.Path()):], Func1( + router.Post("/v1/tenants/:id/follow"[len(r.Path()):], Func2( r.tenant.Follow, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), )) // Register routes for controller: Transaction @@ -196,13 +214,15 @@ func (r *Routes) Register(router fiber.Router) { PathParam[string]("id"), )) r.log.Debugf("Registering route: Post /v1/orders -> transaction.Create") - router.Post("/v1/orders"[len(r.Path()):], DataFunc1( + router.Post("/v1/orders"[len(r.Path()):], DataFunc2( r.transaction.Create, + Local[*models.User]("__ctx_user"), Body[dto.OrderCreateForm]("form"), )) r.log.Debugf("Registering route: Post /v1/orders/:id/pay -> transaction.Pay") - router.Post("/v1/orders/:id/pay"[len(r.Path()):], DataFunc2( + router.Post("/v1/orders/:id/pay"[len(r.Path()):], DataFunc3( r.transaction.Pay, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), Body[dto.OrderPayForm]("form"), )) @@ -213,13 +233,15 @@ func (r *Routes) Register(router fiber.Router) { )) // Register routes for controller: User r.log.Debugf("Registering route: Delete /v1/me/favorites/:contentId -> user.RemoveFavorite") - router.Delete("/v1/me/favorites/:contentId"[len(r.Path()):], Func1( + router.Delete("/v1/me/favorites/:contentId"[len(r.Path()):], Func2( r.user.RemoveFavorite, + Local[*models.User]("__ctx_user"), PathParam[string]("contentId"), )) r.log.Debugf("Registering route: Delete /v1/me/likes/:contentId -> user.RemoveLike") - router.Delete("/v1/me/likes/:contentId"[len(r.Path()):], Func1( + router.Delete("/v1/me/likes/:contentId"[len(r.Path()):], Func2( r.user.RemoveLike, + Local[*models.User]("__ctx_user"), PathParam[string]("contentId"), )) r.log.Debugf("Registering route: Get /v1/me -> user.Me") @@ -228,69 +250,83 @@ func (r *Routes) Register(router fiber.Router) { Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/me/coupons -> user.MyCoupons") - router.Get("/v1/me/coupons"[len(r.Path()):], DataFunc1( + router.Get("/v1/me/coupons"[len(r.Path()):], DataFunc2( r.user.MyCoupons, + Local[*models.User]("__ctx_user"), QueryParam[string]("status"), )) r.log.Debugf("Registering route: Get /v1/me/favorites -> user.Favorites") - router.Get("/v1/me/favorites"[len(r.Path()):], DataFunc0( + router.Get("/v1/me/favorites"[len(r.Path()):], DataFunc1( r.user.Favorites, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/me/following -> user.Following") - router.Get("/v1/me/following"[len(r.Path()):], DataFunc0( + router.Get("/v1/me/following"[len(r.Path()):], DataFunc1( r.user.Following, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/me/library -> user.Library") - router.Get("/v1/me/library"[len(r.Path()):], DataFunc0( + router.Get("/v1/me/library"[len(r.Path()):], DataFunc1( r.user.Library, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/me/likes -> user.Likes") - router.Get("/v1/me/likes"[len(r.Path()):], DataFunc0( + router.Get("/v1/me/likes"[len(r.Path()):], DataFunc1( r.user.Likes, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Get /v1/me/notifications -> user.Notifications") - router.Get("/v1/me/notifications"[len(r.Path()):], DataFunc2( + router.Get("/v1/me/notifications"[len(r.Path()):], DataFunc3( r.user.Notifications, + Local[*models.User]("__ctx_user"), QueryParam[string]("type"), QueryParam[int]("page"), )) r.log.Debugf("Registering route: Get /v1/me/orders -> user.ListOrders") - router.Get("/v1/me/orders"[len(r.Path()):], DataFunc1( + router.Get("/v1/me/orders"[len(r.Path()):], DataFunc2( r.user.ListOrders, + Local[*models.User]("__ctx_user"), QueryParam[string]("status"), )) r.log.Debugf("Registering route: Get /v1/me/orders/:id -> user.GetOrder") - router.Get("/v1/me/orders/:id"[len(r.Path()):], DataFunc1( + router.Get("/v1/me/orders/:id"[len(r.Path()):], DataFunc2( r.user.GetOrder, + Local[*models.User]("__ctx_user"), PathParam[string]("id"), )) r.log.Debugf("Registering route: Get /v1/me/wallet -> user.Wallet") - router.Get("/v1/me/wallet"[len(r.Path()):], DataFunc0( + router.Get("/v1/me/wallet"[len(r.Path()):], DataFunc1( r.user.Wallet, + Local[*models.User]("__ctx_user"), )) r.log.Debugf("Registering route: Post /v1/me/favorites -> user.AddFavorite") - router.Post("/v1/me/favorites"[len(r.Path()):], Func1( + router.Post("/v1/me/favorites"[len(r.Path()):], Func2( r.user.AddFavorite, + Local[*models.User]("__ctx_user"), QueryParam[string]("contentId"), )) r.log.Debugf("Registering route: Post /v1/me/likes -> user.AddLike") - router.Post("/v1/me/likes"[len(r.Path()):], Func1( + router.Post("/v1/me/likes"[len(r.Path()):], Func2( r.user.AddLike, + Local[*models.User]("__ctx_user"), QueryParam[string]("contentId"), )) r.log.Debugf("Registering route: Post /v1/me/realname -> user.RealName") - router.Post("/v1/me/realname"[len(r.Path()):], Func1( + router.Post("/v1/me/realname"[len(r.Path()):], Func2( r.user.RealName, + Local[*models.User]("__ctx_user"), Body[dto.RealNameForm]("form"), )) r.log.Debugf("Registering route: Post /v1/me/wallet/recharge -> user.Recharge") - router.Post("/v1/me/wallet/recharge"[len(r.Path()):], DataFunc1( + router.Post("/v1/me/wallet/recharge"[len(r.Path()):], DataFunc2( r.user.Recharge, + Local[*models.User]("__ctx_user"), Body[dto.RechargeForm]("form"), )) r.log.Debugf("Registering route: Put /v1/me -> user.Update") - router.Put("/v1/me"[len(r.Path()):], Func1( + router.Put("/v1/me"[len(r.Path()):], Func2( r.user.Update, + Local[*models.User]("__ctx_user"), Body[dto.UserUpdate]("form"), )) diff --git a/backend/app/http/v1/tenant.go b/backend/app/http/v1/tenant.go index 701a23a..380a470 100644 --- a/backend/app/http/v1/tenant.go +++ b/backend/app/http/v1/tenant.go @@ -3,10 +3,9 @@ package v1 import ( "quyun/v2/app/http/v1/dto" "quyun/v2/app/services" - "quyun/v2/pkg/consts" + "quyun/v2/database/models" "github.com/gofiber/fiber/v3" - "github.com/spf13/cast" ) // @provider @@ -22,9 +21,13 @@ type Tenant struct{} // @Produce json // @Param id path string true "Tenant ID" // @Success 200 {object} dto.TenantProfile +// @Bind user local key(__ctx_user) // @Bind id path -func (t *Tenant) Get(ctx fiber.Ctx, id string) (*dto.TenantProfile, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) +func (t *Tenant) Get(ctx fiber.Ctx, user *models.User, id string) (*dto.TenantProfile, error) { + uid := int64(0) + if user != nil { + uid = user.ID + } return services.Tenant.GetPublicProfile(ctx.Context(), uid, id) } @@ -38,10 +41,10 @@ func (t *Tenant) Get(ctx fiber.Ctx, id string) (*dto.TenantProfile, error) { // @Produce json // @Param id path string true "Tenant ID" // @Success 200 {string} string "Followed" +// @Bind user local key(__ctx_user) // @Bind id path -func (t *Tenant) Follow(ctx fiber.Ctx, id string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Tenant.Follow(ctx.Context(), uid, id) +func (t *Tenant) Follow(ctx fiber.Ctx, user *models.User, id string) error { + return services.Tenant.Follow(ctx.Context(), user.ID, id) } // Unfollow a tenant @@ -54,8 +57,8 @@ func (t *Tenant) Follow(ctx fiber.Ctx, id string) error { // @Produce json // @Param id path string true "Tenant ID" // @Success 200 {string} string "Unfollowed" +// @Bind user local key(__ctx_user) // @Bind id path -func (t *Tenant) Unfollow(ctx fiber.Ctx, id string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Tenant.Unfollow(ctx.Context(), uid, id) +func (t *Tenant) Unfollow(ctx fiber.Ctx, user *models.User, id string) error { + return services.Tenant.Unfollow(ctx.Context(), user.ID, id) } diff --git a/backend/app/http/v1/transaction.go b/backend/app/http/v1/transaction.go index bcc1eab..edf3188 100644 --- a/backend/app/http/v1/transaction.go +++ b/backend/app/http/v1/transaction.go @@ -3,10 +3,9 @@ package v1 import ( "quyun/v2/app/http/v1/dto" "quyun/v2/app/services" - "quyun/v2/pkg/consts" + "quyun/v2/database/models" "github.com/gofiber/fiber/v3" - "github.com/spf13/cast" ) // @provider @@ -22,10 +21,14 @@ type Transaction struct{} // @Produce json // @Param form body dto.OrderCreateForm true "Create form" // @Success 200 {object} dto.OrderCreateResponse +// @Bind user local key(__ctx_user) // @Bind form body -func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.OrderCreateResponse, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Order.Create(ctx.Context(), uid, form) +func (t *Transaction) Create( + ctx fiber.Ctx, + user *models.User, + form *dto.OrderCreateForm, +) (*dto.OrderCreateResponse, error) { + return services.Order.Create(ctx.Context(), user.ID, form) } // Pay for order @@ -39,11 +42,16 @@ func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.Ord // @Param id path string true "Order ID" // @Param form body dto.OrderPayForm true "Pay form" // @Success 200 {object} dto.OrderPayResponse +// @Bind user local key(__ctx_user) // @Bind id path // @Bind form body -func (t *Transaction) Pay(ctx fiber.Ctx, id string, form *dto.OrderPayForm) (*dto.OrderPayResponse, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Order.Pay(ctx.Context(), uid, id, form) +func (t *Transaction) Pay( + ctx fiber.Ctx, + user *models.User, + id string, + form *dto.OrderPayForm, +) (*dto.OrderPayResponse, error) { + return services.Order.Pay(ctx.Context(), user.ID, id, form) } // Check order payment status diff --git a/backend/app/http/v1/user.go b/backend/app/http/v1/user.go index 473b2e5..5887fe5 100644 --- a/backend/app/http/v1/user.go +++ b/backend/app/http/v1/user.go @@ -6,10 +6,8 @@ import ( "quyun/v2/app/requests" "quyun/v2/app/services" "quyun/v2/database/models" - "quyun/v2/pkg/consts" "github.com/gofiber/fiber/v3" - "github.com/spf13/cast" ) // @provider @@ -40,10 +38,10 @@ func (u *User) Me(ctx fiber.Ctx, user *models.User) (*auth_dto.User, error) { // @Produce json // @Param form body dto.UserUpdate true "Update form" // @Success 200 {string} string "Updated" +// @Bind user local key(__ctx_user) // @Bind form body -func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.User.Update(ctx.Context(), uid, form) +func (u *User) Update(ctx fiber.Ctx, user *models.User, form *dto.UserUpdate) error { + return services.User.Update(ctx.Context(), user.ID, form) } // Submit real-name authentication @@ -56,10 +54,10 @@ func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error { // @Produce json // @Param form body dto.RealNameForm true "Realname form" // @Success 200 {string} string "Submitted" +// @Bind user local key(__ctx_user) // @Bind form body -func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.User.RealName(ctx.Context(), uid, form) +func (u *User) RealName(ctx fiber.Ctx, user *models.User, form *dto.RealNameForm) error { + return services.User.RealName(ctx.Context(), user.ID, form) } // Get wallet balance and transactions @@ -71,9 +69,9 @@ func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error { // @Accept json // @Produce json // @Success 200 {object} dto.WalletResponse -func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Wallet.GetWallet(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (u *User) Wallet(ctx fiber.Ctx, user *models.User) (*dto.WalletResponse, error) { + return services.Wallet.GetWallet(ctx.Context(), user.ID) } // Recharge wallet @@ -86,10 +84,10 @@ func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) { // @Produce json // @Param form body dto.RechargeForm true "Recharge form" // @Success 200 {object} dto.RechargeResponse +// @Bind user local key(__ctx_user) // @Bind form body -func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeResponse, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Wallet.Recharge(ctx.Context(), uid, form) +func (u *User) Recharge(ctx fiber.Ctx, user *models.User, form *dto.RechargeForm) (*dto.RechargeResponse, error) { + return services.Wallet.Recharge(ctx.Context(), user.ID, form) } // List user orders @@ -102,10 +100,10 @@ func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeRes // @Produce json // @Param status query string false "Status enum(all, unpaid, completed, refund)" // @Success 200 {array} dto.Order +// @Bind user local key(__ctx_user) // @Bind status query -func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Order.ListUserOrders(ctx.Context(), uid, status) +func (u *User) ListOrders(ctx fiber.Ctx, user *models.User, status string) ([]dto.Order, error) { + return services.Order.ListUserOrders(ctx.Context(), user.ID, status) } // Get user order detail @@ -118,10 +116,10 @@ func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) { // @Produce json // @Param id path string true "Order ID" // @Success 200 {object} dto.Order +// @Bind user local key(__ctx_user) // @Bind id path -func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Order.GetUserOrder(ctx.Context(), uid, id) +func (u *User) GetOrder(ctx fiber.Ctx, user *models.User, id string) (*dto.Order, error) { + return services.Order.GetUserOrder(ctx.Context(), user.ID, id) } // Get purchased content @@ -133,9 +131,9 @@ func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) { // @Accept json // @Produce json // @Success 200 {array} dto.ContentItem -func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.GetLibrary(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (u *User) Library(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) { + return services.Content.GetLibrary(ctx.Context(), user.ID) } // Get favorites @@ -147,9 +145,9 @@ func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) { // @Accept json // @Produce json // @Success 200 {array} dto.ContentItem -func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.GetFavorites(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (u *User) Favorites(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) { + return services.Content.GetFavorites(ctx.Context(), user.ID) } // Add to favorites @@ -162,10 +160,10 @@ func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) { // @Produce json // @Param contentId query string true "Content ID" // @Success 200 {string} string "Added" +// @Bind user local key(__ctx_user) // @Bind contentId query -func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.AddFavorite(ctx.Context(), uid, contentId) +func (u *User) AddFavorite(ctx fiber.Ctx, user *models.User, contentId string) error { + return services.Content.AddFavorite(ctx.Context(), user.ID, contentId) } // Remove from favorites @@ -178,10 +176,10 @@ func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error { // @Produce json // @Param contentId path string true "Content ID" // @Success 200 {string} string "Removed" +// @Bind user local key(__ctx_user) // @Bind contentId path -func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.RemoveFavorite(ctx.Context(), uid, contentId) +func (u *User) RemoveFavorite(ctx fiber.Ctx, user *models.User, contentId string) error { + return services.Content.RemoveFavorite(ctx.Context(), user.ID, contentId) } // Get liked contents @@ -193,9 +191,9 @@ func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error { // @Accept json // @Produce json // @Success 200 {array} dto.ContentItem -func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.GetLikes(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (u *User) Likes(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) { + return services.Content.GetLikes(ctx.Context(), user.ID) } // Like content @@ -208,10 +206,10 @@ func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) { // @Produce json // @Param contentId query string true "Content ID" // @Success 200 {string} string "Liked" +// @Bind user local key(__ctx_user) // @Bind contentId query -func (u *User) AddLike(ctx fiber.Ctx, contentId string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.AddLike(ctx.Context(), uid, contentId) +func (u *User) AddLike(ctx fiber.Ctx, user *models.User, contentId string) error { + return services.Content.AddLike(ctx.Context(), user.ID, contentId) } // Unlike content @@ -224,10 +222,10 @@ func (u *User) AddLike(ctx fiber.Ctx, contentId string) error { // @Produce json // @Param contentId path string true "Content ID" // @Success 200 {string} string "Unliked" +// @Bind user local key(__ctx_user) // @Bind contentId path -func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Content.RemoveLike(ctx.Context(), uid, contentId) +func (u *User) RemoveLike(ctx fiber.Ctx, user *models.User, contentId string) error { + return services.Content.RemoveLike(ctx.Context(), user.ID, contentId) } // Get following tenants @@ -239,9 +237,9 @@ func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error { // @Accept json // @Produce json // @Success 200 {array} dto.TenantProfile -func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Tenant.ListFollowed(ctx.Context(), uid) +// @Bind user local key(__ctx_user) +func (u *User) Following(ctx fiber.Ctx, user *models.User) ([]dto.TenantProfile, error) { + return services.Tenant.ListFollowed(ctx.Context(), user.ID) } // Get notifications @@ -255,11 +253,11 @@ func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) { // @Param type query string false "Type enum(all, system, order, audit, interaction)" // @Param page query int false "Page number" // @Success 200 {object} requests.Pager{items=[]dto.Notification} +// @Bind user local key(__ctx_user) // @Bind typeArg query key(type) // @Bind page query -func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests.Pager, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Notification.List(ctx.Context(), uid, page, typeArg) +func (u *User) Notifications(ctx fiber.Ctx, user *models.User, typeArg string, page int) (*requests.Pager, error) { + return services.Notification.List(ctx.Context(), user.ID, page, typeArg) } // List my coupons @@ -272,8 +270,8 @@ func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests // @Produce json // @Param status query string false "Status (unused, used, expired)" // @Success 200 {array} dto.UserCouponItem +// @Bind user local key(__ctx_user) // @Bind status query -func (u *User) MyCoupons(ctx fiber.Ctx, status string) ([]dto.UserCouponItem, error) { - uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) - return services.Coupon.ListUserCoupons(ctx.Context(), uid, status) +func (u *User) MyCoupons(ctx fiber.Ctx, user *models.User, status string) ([]dto.UserCouponItem, error) { + return services.Coupon.ListUserCoupons(ctx.Context(), user.ID, status) } diff --git a/backend/app/middlewares/middlewares.go b/backend/app/middlewares/middlewares.go index e5b2a14..be546e2 100644 --- a/backend/app/middlewares/middlewares.go +++ b/backend/app/middlewares/middlewares.go @@ -3,7 +3,6 @@ package middlewares import ( "quyun/v2/app/errorx" "quyun/v2/app/services" - "quyun/v2/pkg/consts" "quyun/v2/providers/jwt" "github.com/gofiber/fiber/v3" @@ -43,13 +42,13 @@ func (m *Middlewares) Auth(ctx fiber.Ctx) error { } // Set Context - ctx.Locals(consts.CtxKeyUser, user) + ctx.Locals("__ctx_user", user) if claims.TenantID > 0 { tenant, err := services.Tenant.GetModelByID(ctx, claims.TenantID) if err != nil { return errorx.ErrUnauthorized.WithCause(err).WithMsg("TenantNotFound") } - ctx.Locals(consts.CtxKeyTenant, tenant) + ctx.Locals("__ctx_tenant", tenant) } return ctx.Next()