package routers import ( "net/http" "git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/middleware" v1 "git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/routers/api/v1" "github.com/gin-gonic/gin" ) // NewRouter 构建 gin 引擎:注册中间件链与 /api/v1 路由。 func NewRouter() *gin.Engine { r := gin.New() // 中间件链:Recovery -> RequestID -> OTel -> Slog -> CORS r.Use( gin.Recovery(), middleware.RequestID(), middleware.GinOTelMiddleware(), middleware.GinSlogMiddleware(), middleware.CORS(), ) // 健康检查 r.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) }) // handler 实例 adminHandler := v1.NewAdminHandler() dashboardHandler := v1.NewDashboardHandler() userHandler := v1.NewUserHandler() goodsHandler := v1.NewGoodsHandler() orderHandler := v1.NewOrderHandler() contentHandler := v1.NewContentHandler() notificationHandler := v1.NewNotificationHandler() apiV1 := r.Group("/api/v1") // ===== 公开路由(无需登录)===== { apiV1.POST("/admin/login", adminHandler.Login) apiV1.POST("/admin/refresh-token", adminHandler.RefreshToken) } // ===== 鉴权路由(需登录)===== auth := apiV1.Group("") auth.Use(middleware.JWTAuth()) { // 管理员(自己) auth.POST("/admin/logout", adminHandler.Logout) auth.PUT("/admin/password", adminHandler.ChangePassword) // 仪表盘 auth.GET("/dashboard/summary", dashboardHandler.Summary) auth.GET("/dashboard/trends", dashboardHandler.Trend) // 用户管理 auth.GET("/users", userHandler.List) auth.GET("/users/:uuid", userHandler.Detail) auth.PUT("/users/:uuid/ban", userHandler.Ban) auth.PUT("/users/:uuid/unban", userHandler.Unban) auth.PUT("/users/:uuid/quota", userHandler.QuotaAdjust) auth.PUT("/users/:uuid/vip", userHandler.VIPAdjust) auth.DELETE("/users/:uuid/grants/:id", userHandler.GrantRevoke) // 商品管理 auth.GET("/goods", goodsHandler.List) auth.POST("/goods", goodsHandler.Create) auth.PUT("/goods/:id", goodsHandler.Update) auth.PUT("/goods/:id/status", goodsHandler.UpdateStatus) auth.DELETE("/goods/:id", goodsHandler.Delete) // 订单与退款 auth.GET("/orders", orderHandler.List) auth.GET("/orders/:orderNo", orderHandler.Get) auth.GET("/refunds", orderHandler.RefundList) auth.POST("/refunds/:refundNo/approve", orderHandler.RefundApprove) auth.POST("/refunds/:refundNo/reject", orderHandler.RefundReject) // 内容审核 auth.GET("/outfits", contentHandler.OutfitList) auth.GET("/outfits/:uuid", contentHandler.OutfitDetail) auth.POST("/outfits/:uuid/audit", contentHandler.OutfitAudit) auth.POST("/outfits/:uuid/ban", contentHandler.OutfitBan) auth.POST("/outfits/:uuid/unban", contentHandler.OutfitUnban) auth.GET("/clothes", contentHandler.ClothesList) auth.GET("/clothes/:uuid", contentHandler.ClothesDetail) auth.GET("/closet-shares", contentHandler.ShareList) auth.POST("/closet-shares/:uuid/ban", contentHandler.ShareBan) auth.POST("/closet-shares/:uuid/unban", contentHandler.ShareUnban) // 通知推送 auth.POST("/notifications", notificationHandler.Send) auth.POST("/notifications/broadcast", notificationHandler.Broadcast) } // ===== 超管专属路由 ===== super := apiV1.Group("") super.Use(middleware.JWTAuth(), middleware.AdminSuperAuth()) { super.GET("/admins", adminHandler.List) super.POST("/admins", adminHandler.Create) super.PUT("/admins/:id", adminHandler.Update) super.DELETE("/admins/:id", adminHandler.Delete) } return r }