package server import ( "path/filepath" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/rovina/zsp-backend/internal/handler" ) func SetupRouter(userHandler *handler.UserHandler, contractHandler *handler.ContractHandler, zspContractHandler *handler.ZSPContractHandler, zspFinancesHandler *handler.ZSPFinancesHandler, companyHandler *handler.CompanyHandler, deliveryHandler *handler.DeliveryHandler, authMiddleware gin.HandlerFunc, ) *gin.Engine { router := gin.Default() router.Use(gin.Logger()) router.Use(gin.Recovery()) // 允许跨域,并放行 Authorization 等请求头(解决前端直连 8080 时的 CORS 报错) router.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"}, AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization", "X-Requested-With"}, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: false, MaxAge: 12 * 3600, })) // user routes api := router.Group("/api") { router.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) }) auth := api.Group("/auth") { auth.POST("/register", userHandler.Register) auth.POST("/login", userHandler.Login) } // 静态文件资源(公开访问,供前端预览/下载) api.Static("/contract-files", filepath.Join(".", "data", "contract")) api.Static("/zsp-contract-files", filepath.Join(".", "data", "zsp-contract")) api.Static("/company-files", filepath.Join(".", "data", "company")) // 以下所有路由需要 JWT 认证 authorized := api.Group("/") authorized.Use(authMiddleware) { users := authorized.Group("/users") { users.GET("/:id", userHandler.GetUser) } contracts := authorized.Group("/contract") { contracts.GET("", contractHandler.List) contracts.POST("/upload", contractHandler.Upload) contracts.POST("/batch-upload", contractHandler.BatchUpload) contracts.GET("/folders", contractHandler.ListFolders) contracts.POST("/folders", contractHandler.CreateFolder) contracts.GET("/folders/:id", contractHandler.GetFolder) contracts.PUT("/folders/:id", contractHandler.UpdateFolder) contracts.DELETE("/folders/:id", contractHandler.DeleteFolder) contracts.GET("/batches", contractHandler.ListBatches) contracts.POST("/batches", contractHandler.CreateBatch) contracts.GET("/batches/:id", contractHandler.GetBatch) contracts.DELETE("/batches/:id", contractHandler.DeleteBatch) contracts.POST("/:id/update-files", contractHandler.UpdateWithFiles) contracts.GET("/:id", contractHandler.Get) contracts.PUT("/:id", contractHandler.Update) contracts.DELETE("/:id", contractHandler.Delete) } zspContracts := authorized.Group("/zsp-contract") { zspContracts.POST("/folders", zspContractHandler.CreateFolder) zspContracts.GET("/folders", zspContractHandler.GetFolders) zspContracts.GET("/folders/:id", zspContractHandler.GetFolder) zspContracts.DELETE("/folders/:id", zspContractHandler.DeleteFolder) zspContracts.PUT("/folders/:id", zspContractHandler.UpdateFolder) zspContracts.POST("/contracts", zspContractHandler.UploadContract) zspContracts.GET("/contracts/:id", zspContractHandler.GetContractWithDetails) zspContracts.GET("/contracts", zspContractHandler.ListContractsWithDetails) zspContracts.PUT("/contracts/:id", zspContractHandler.UpdateContract) zspContracts.DELETE("/contracts/:id", zspContractHandler.DeleteContract) zspContracts.POST("/contracts/:id/details", zspContractHandler.CreateContractDetail) zspContracts.GET("/contracts/:id/details", zspContractHandler.ListContractDetails) zspContracts.POST("/details/batch", zspContractHandler.BatchCreateContractDetails) zspContracts.GET("/details/:id", zspContractHandler.GetContractDetail) zspContracts.PUT("/details/:id", zspContractHandler.UpdateContractDetail) zspContracts.DELETE("/details/:id", zspContractHandler.DeleteContractDetail) zspContracts.POST("/contracts/:id/details/import", zspContractHandler.ImportContractDetailsFromExcel) zspContracts.GET("/contracts/:id/details/export", zspContractHandler.ExportContractDetailsToExcel) } company := authorized.Group("/company") { company.GET("/folders", companyHandler.ListFolders) company.POST("/folders", companyHandler.CreateFolder) company.PUT("/folders/:id", companyHandler.UpdateFolder) company.DELETE("/folders/:id", companyHandler.DeleteFolder) company.GET("/files", companyHandler.ListFiles) company.POST("/files/upload", companyHandler.UploadFiles) company.PUT("/files/:id", companyHandler.UpdateFile) company.DELETE("/files/:id", companyHandler.DeleteFile) } // 提货管理 (RESTful) applyDelivery := authorized.Group("/apply-delivery") { applyDelivery.GET("", deliveryHandler.ListApply) applyDelivery.GET("/:id", deliveryHandler.GetApply) applyDelivery.POST("", deliveryHandler.CreateApply) applyDelivery.PUT("/:id", deliveryHandler.UpdateApply) applyDelivery.PUT("/:id/cancel", deliveryHandler.CancelApply) applyDelivery.DELETE("/:id", deliveryHandler.DeleteApply) } deliveryDetails := authorized.Group("/delivery-details") { deliveryDetails.GET("", deliveryHandler.ListOutbound) deliveryDetails.GET("/:id", deliveryHandler.GetOutbound) deliveryDetails.POST("", deliveryHandler.CreateOutbound) deliveryDetails.PUT("/:id", deliveryHandler.UpdateOutbound) deliveryDetails.DELETE("/:id", deliveryHandler.DeleteOutbound) } inventory := authorized.Group("/inventory") { inventory.GET("/warehouses", deliveryHandler.ListWarehouses) inventory.POST("/warehouses", deliveryHandler.CreateWarehouse) inventory.PUT("/warehouses/:id", deliveryHandler.UpdateWarehouse) inventory.DELETE("/warehouses/:id", deliveryHandler.DeleteWarehouse) inventory.GET("/inbound", deliveryHandler.ListInbound) inventory.POST("/inbound", deliveryHandler.CreateInbound) inventory.PUT("/inbound/:id", deliveryHandler.UpdateInbound) inventory.DELETE("/inbound/:id", deliveryHandler.DeleteInbound) inventory.GET("/summary", deliveryHandler.InventorySummary) } ownershipTransfer := authorized.Group("/ownership-transfer") { ownershipTransfer.GET("", deliveryHandler.ListOwnership) ownershipTransfer.GET("/:id", deliveryHandler.GetOwnership) ownershipTransfer.POST("", deliveryHandler.CreateOwnership) ownershipTransfer.POST("/with-files", deliveryHandler.CreateOwnershipWithFiles) ownershipTransfer.DELETE("/:id", deliveryHandler.DeleteOwnership) } zspFinances := authorized.Group("/zsp-finances") { // 货代费用 zspFinances.POST("/freight-charges", zspFinancesHandler.CreateFreightCharge) zspFinances.GET("/freight-charges/:id", zspFinancesHandler.GetFreightCharge) zspFinances.GET("/freight-charges", zspFinancesHandler.ListFreightCharges) zspFinances.PUT("/freight-charges/:id", zspFinancesHandler.UpdateFreightCharge) zspFinances.DELETE("/freight-charges/:id", zspFinancesHandler.DeleteFreightCharge) // 杂费 zspFinances.POST("/misc-charges", zspFinancesHandler.CreateMiscCharge) zspFinances.GET("/misc-charges/:id", zspFinancesHandler.GetMiscCharge) zspFinances.GET("/misc-charges", zspFinancesHandler.ListMiscCharges) zspFinances.PUT("/misc-charges/:id", zspFinancesHandler.UpdateMiscCharge) zspFinances.DELETE("/misc-charges/:id", zspFinancesHandler.DeleteMiscCharge) // 服务费 zspFinances.POST("/service-fees", zspFinancesHandler.CreateServiceFee) zspFinances.GET("/service-fees/:id", zspFinancesHandler.GetServiceFee) zspFinances.GET("/service-fees", zspFinancesHandler.ListServiceFees) zspFinances.PUT("/service-fees/:id", zspFinancesHandler.UpdateServiceFee) zspFinances.DELETE("/service-fees/:id", zspFinancesHandler.DeleteServiceFee) // 成本构成 zspFinances.POST("/cost-breakdowns", zspFinancesHandler.CreateCostBreakdown) zspFinances.GET("/cost-breakdowns/:id", zspFinancesHandler.GetCostBreakdown) zspFinances.GET("/cost-breakdowns", zspFinancesHandler.ListCostBreakdowns) zspFinances.PUT("/cost-breakdowns/:id", zspFinancesHandler.UpdateCostBreakdown) zspFinances.DELETE("/cost-breakdowns/:id", zspFinancesHandler.DeleteCostBreakdown) // 利润记录 zspFinances.POST("/profit-records", zspFinancesHandler.CreateProfitRecord) zspFinances.GET("/profit-records/:id", zspFinancesHandler.GetProfitRecord) zspFinances.GET("/profit-records", zspFinancesHandler.ListProfitRecords) zspFinances.PUT("/profit-records/:id", zspFinancesHandler.UpdateProfitRecord) zspFinances.DELETE("/profit-records/:id", zspFinancesHandler.DeleteProfitRecord) } } // end authorized } // end api return router }