From d21b420765258342ccac9e0e7bba95855c495dfb Mon Sep 17 00:00:00 2001 From: mlogclub Date: Sat, 23 May 2026 22:12:13 +0800 Subject: [PATCH] feat: restructure API routing and remove ginx package - Added new routing functions for API and dashboard endpoints in controller_routes.go. - Replaced previous ginx.HandleController calls with explicit route registrations. - Removed the ginx package as it is no longer needed for handling controllers. - Improved organization and readability of route definitions. --- internal/bootstrap/controller_routes.go | 1003 +++++++++++++++++++++++ internal/bootstrap/server.go | 72 +- internal/pkg/ginx/mvc.go | 137 ---- 3 files changed, 1037 insertions(+), 175 deletions(-) create mode 100644 internal/bootstrap/controller_routes.go delete mode 100644 internal/pkg/ginx/mvc.go diff --git a/internal/bootstrap/controller_routes.go b/internal/bootstrap/controller_routes.go new file mode 100644 index 0000000..1cb595c --- /dev/null +++ b/internal/bootstrap/controller_routes.go @@ -0,0 +1,1003 @@ +package bootstrap + +import ( + "net/http" + "strconv" + + "cs-agent/internal/controllers/api" + "cs-agent/internal/controllers/dashboard" + "cs-agent/internal/controllers/third" + + "github.com/gin-gonic/gin" + "github.com/mlogclub/simple/web" +) + +func writeJSON(ctx *gin.Context, result *web.JsonResult) { + if result == nil { + return + } + ctx.JSON(http.StatusOK, result) +} + +func pathInt64(ctx *gin.Context, name string) (int64, bool) { + value, err := strconv.ParseInt(ctx.Param(name), 10, 64) + if err != nil { + ctx.JSON(http.StatusBadRequest, web.JsonErrorMsg("路径参数错误")) + return 0, false + } + return value, true +} + +func registerApiAuthRoutes(group *gin.RouterGroup) { + group.POST("/login", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + writeJSON(ctx, controller.PostLogin()) + }) + group.POST("/logout", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + writeJSON(ctx, controller.PostLogout()) + }) + group.GET("/profile", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + writeJSON(ctx, controller.GetProfile()) + }) + group.GET("/wxwork_callback", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + controller.GetWxwork_callback() + }) + group.POST("/wxwork_exchange", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + writeJSON(ctx, controller.PostWxwork_exchange()) + }) + group.GET("/wxwork_login", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + controller.GetWxwork_login() + }) + group.GET("/wxwork_qr_login", func(ctx *gin.Context) { + controller := &api.AuthController{Ctx: ctx} + controller.GetWxwork_qr_login() + }) +} + +func registerApiChannelRoutes(group *gin.RouterGroup) { + group.Any("/config", func(ctx *gin.Context) { + controller := &api.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.AnyConfig()) + }) +} + +func registerApiConversationRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &api.ConversationController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/close", func(ctx *gin.Context) { + controller := &api.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostClose()) + }) + group.POST("/create_or_match", func(ctx *gin.Context) { + controller := &api.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate_or_match()) + }) +} + +func registerApiCustomerRoutes(group *gin.RouterGroup) { + group.POST("/session_exchange", func(ctx *gin.Context) { + controller := &api.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostSession_exchange()) + }) +} + +func registerApiMessageRoutes(group *gin.RouterGroup) { + group.Any("/list", func(ctx *gin.Context) { + controller := &api.MessageController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/read", func(ctx *gin.Context) { + controller := &api.MessageController{Ctx: ctx} + writeJSON(ctx, controller.PostRead()) + }) + group.POST("/send", func(ctx *gin.Context) { + controller := &api.MessageController{Ctx: ctx} + writeJSON(ctx, controller.PostSend()) + }) + group.POST("/upload_attachment", func(ctx *gin.Context) { + controller := &api.MessageController{Ctx: ctx} + writeJSON(ctx, controller.PostUpload_attachment()) + }) + group.POST("/upload_image", func(ctx *gin.Context) { + controller := &api.MessageController{Ctx: ctx} + writeJSON(ctx, controller.PostUpload_image()) + }) +} + +func registerDashboardAIAgentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_sort", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_sort()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.AIAgentController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardAIConfigRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.Any("/list_all", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.AnyList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_sort", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_sort()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.AIConfigController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardAgentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.AgentController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardAgentRunLogRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AgentRunLogController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AgentRunLogController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) +} + +func registerDashboardAgentTeamRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardAgentTeamScheduleRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/batch_generate", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.PostBatch_generate()) + }) + group.POST("/batch_preview", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.PostBatch_preview()) + }) + group.Any("/calendar", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.AnyCalendar()) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.AgentTeamScheduleController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardAssetRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.AssetController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.AssetController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.AssetController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.AssetController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) +} + +func registerDashboardChannelRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/reset_user_token_secret", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.PostReset_user_token_secret()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) + group.Any("/wxwork/kf/accounts", func(ctx *gin.Context) { + controller := &dashboard.ChannelController{Ctx: ctx} + writeJSON(ctx, controller.AnyWxworkKfAccounts()) + }) +} + +func registerDashboardCompanyRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.CompanyController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardConversationRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/add_tag", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostAdd_tag()) + }) + group.POST("/assign", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostAssign()) + }) + group.POST("/close", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostClose()) + }) + group.Any("/conversations", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.AnyConversations()) + }) + group.POST("/dispatch", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostDispatch()) + }) + group.POST("/link_customer", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostLink_customer()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.Any("/message_list", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.AnyMessage_list()) + }) + group.POST("/read", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostRead()) + }) + group.POST("/recall_message", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostRecall_message()) + }) + group.POST("/remove_tag", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostRemove_tag()) + }) + group.POST("/send_message", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostSend_message()) + }) + group.POST("/transfer", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostTransfer()) + }) + group.POST("/upload_attachment", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostUpload_attachment()) + }) + group.POST("/upload_image", func(ctx *gin.Context) { + controller := &dashboard.ConversationController{Ctx: ctx} + writeJSON(ctx, controller.PostUpload_image()) + }) +} + +func registerDashboardCustomerContactRoutes(group *gin.RouterGroup) { + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.CustomerContactController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.CustomerContactController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.CustomerContactController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.CustomerContactController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardCustomerRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.POST("/list", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostList()) + }) + group.POST("/save_profile", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostSave_profile()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.CustomerController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardDashboardRoutes(group *gin.RouterGroup) { + group.GET("/overview", func(ctx *gin.Context) { + controller := &dashboard.DashboardController{Ctx: ctx} + writeJSON(ctx, controller.GetOverview()) + }) +} + +func registerDashboardKnowledgeBaseRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.Any("/list_all", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.AnyList_all()) + }) + group.POST("/rebuild_index", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.PostRebuild_index()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_sort", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeBaseController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_sort()) + }) +} + +func registerDashboardKnowledgeDocumentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeDocumentController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeDocumentController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeDocumentController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeDocumentController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeDocumentController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardKnowledgeFAQRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeFAQController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeFAQController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeFAQController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeFAQController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeFAQController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardKnowledgeRetrieveRoutes(group *gin.RouterGroup) { + group.POST("/build", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeRetrieveController{Ctx: ctx} + writeJSON(ctx, controller.PostBuild()) + }) + group.POST("/debug/answer", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeRetrieveController{Ctx: ctx} + writeJSON(ctx, controller.PostDebugAnswer()) + }) + group.POST("/debug/search", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeRetrieveController{Ctx: ctx} + writeJSON(ctx, controller.PostDebugSearch()) + }) +} + +func registerDashboardKnowledgeRetrieveLogRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeRetrieveLogController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.KnowledgeRetrieveLogController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) +} + +func registerDashboardMCPRoutes(group *gin.RouterGroup) { + group.POST("/call_tool", func(ctx *gin.Context) { + controller := &dashboard.MCPController{Ctx: ctx} + writeJSON(ctx, controller.PostCall_tool()) + }) + group.Any("/catalog", func(ctx *gin.Context) { + controller := &dashboard.MCPController{Ctx: ctx} + writeJSON(ctx, controller.AnyCatalog()) + }) + group.Any("/list_servers", func(ctx *gin.Context) { + controller := &dashboard.MCPController{Ctx: ctx} + writeJSON(ctx, controller.AnyList_servers()) + }) + group.POST("/list_tools", func(ctx *gin.Context) { + controller := &dashboard.MCPController{Ctx: ctx} + writeJSON(ctx, controller.PostList_tools()) + }) + group.POST("/test_connection", func(ctx *gin.Context) { + controller := &dashboard.MCPController{Ctx: ctx} + writeJSON(ctx, controller.PostTest_connection()) + }) +} + +func registerDashboardNotificationRoutes(group *gin.RouterGroup) { + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.NotificationController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/mark_all_read", func(ctx *gin.Context) { + controller := &dashboard.NotificationController{Ctx: ctx} + writeJSON(ctx, controller.PostMark_all_read()) + }) + group.POST("/mark_read", func(ctx *gin.Context) { + controller := &dashboard.NotificationController{Ctx: ctx} + writeJSON(ctx, controller.PostMark_read()) + }) + group.GET("/unread_count", func(ctx *gin.Context) { + controller := &dashboard.NotificationController{Ctx: ctx} + writeJSON(ctx, controller.GetUnread_count()) + }) +} + +func registerDashboardPermissionRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.PermissionController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.PermissionController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) +} + +func registerDashboardQuickReplyRoutes(group *gin.RouterGroup) { + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.QuickReplyController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.QuickReplyController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.QuickReplyController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.QuickReplyController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.QuickReplyController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) +} + +func registerDashboardRoleRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/assign_permission", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostAssign_permission()) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_sort", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_sort()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.RoleController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardSessionRoutes(group *gin.RouterGroup) { + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.SessionController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/revoke", func(ctx *gin.Context) { + controller := &dashboard.SessionController{Ctx: ctx} + writeJSON(ctx, controller.PostRevoke()) + }) + group.POST("/revoke/by/user", func(ctx *gin.Context) { + controller := &dashboard.SessionController{Ctx: ctx} + writeJSON(ctx, controller.PostRevokeByUser()) + }) +} + +func registerDashboardSkillDefinitionRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/debug_resume", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostDebug_resume()) + }) + group.POST("/debug_run", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostDebug_run()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/restore", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostRestore()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.SkillDefinitionController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardTagRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.GET("/list_all", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.GetList_all()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_sort", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_sort()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.TagController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerDashboardTicketRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/assign", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostAssign()) + }) + group.POST("/change_status", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostChange_status()) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/create_from_conversation", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate_from_conversation()) + }) + group.POST("/delete_view", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete_view()) + }) + group.POST("/link_customer", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostLink_customer()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.POST("/progress/create", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostProgressCreate()) + }) + group.Any("/progress/list", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.AnyProgressList()) + }) + group.POST("/save_view", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostSave_view()) + }) + group.Any("/summary", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.AnySummary()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.Any("/view_list", func(ctx *gin.Context) { + controller := &dashboard.TicketController{Ctx: ctx} + writeJSON(ctx, controller.AnyView_list()) + }) +} + +func registerDashboardUserRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + writeJSON(ctx, controller.GetBy(id)) + }) + group.POST("/assign_role", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostAssign_role()) + }) + group.POST("/change_password", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostChange_password()) + }) + group.POST("/create", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostCreate()) + }) + group.POST("/delete", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostDelete()) + }) + group.Any("/list", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.AnyList()) + }) + group.Any("/list_all", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.AnyList_all()) + }) + group.POST("/reset_password", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostReset_password()) + }) + group.POST("/update", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate()) + }) + group.POST("/update_status", func(ctx *gin.Context) { + controller := &dashboard.UserController{Ctx: ctx} + writeJSON(ctx, controller.PostUpdate_status()) + }) +} + +func registerThirdWechatRoutes(group *gin.RouterGroup) { + group.GET("/callback", func(ctx *gin.Context) { + controller := &third.WechatController{Ctx: ctx} + controller.GetCallback() + }) + group.POST("/callback", func(ctx *gin.Context) { + controller := &third.WechatController{Ctx: ctx} + controller.PostCallback() + }) +} diff --git a/internal/bootstrap/server.go b/internal/bootstrap/server.go index 58a1a04..73f55b4 100644 --- a/internal/bootstrap/server.go +++ b/internal/bootstrap/server.go @@ -10,12 +10,8 @@ import ( "cs-agent/internal/ai/mcps" _ "cs-agent/internal/ai/runtime" - "cs-agent/internal/controllers/api" - "cs-agent/internal/controllers/dashboard" - "cs-agent/internal/controllers/third" "cs-agent/internal/middleware" "cs-agent/internal/pkg/config" - "cs-agent/internal/pkg/ginx" "cs-agent/internal/services" "github.com/gin-gonic/gin" @@ -94,11 +90,11 @@ func addRouter(app *gin.Engine) { app.Any("/api/mcp", gin.WrapH(mcps.NewHTTPHandler())) apiGroup := app.Group("/api") - ginx.HandleController(apiGroup, "/auth", new(api.AuthController)) - ginx.HandleController(apiGroup, "/channel", new(api.ChannelController)) - ginx.HandleController(apiGroup, "/customer", new(api.CustomerController)) - ginx.HandleController(apiGroup, "/conversation", new(api.ConversationController), middleware.ExternalUserMiddleware) - ginx.HandleController(apiGroup, "/message", new(api.MessageController), middleware.ExternalUserMiddleware) + registerApiAuthRoutes(apiGroup.Group("/auth")) + registerApiChannelRoutes(apiGroup.Group("/channel")) + registerApiCustomerRoutes(apiGroup.Group("/customer")) + registerApiConversationRoutes(apiGroup.Group("/conversation", middleware.ExternalUserMiddleware)) + registerApiMessageRoutes(apiGroup.Group("/message", middleware.ExternalUserMiddleware)) wsGroup := app.Group("/api/ws") wsGroup.GET("/dashboard", middleware.AuthMiddleware, services.WsService.HandleDashboardWS) @@ -106,37 +102,37 @@ func addRouter(app *gin.Engine) { wsGroup.GET("/open", services.WsService.HandleOpenWS) dashboardGroup := app.Group("/api/dashboard", middleware.AuthMiddleware) - ginx.HandleController(dashboardGroup, "/dashboard", new(dashboard.DashboardController)) - ginx.HandleController(dashboardGroup, "/user", new(dashboard.UserController)) - ginx.HandleController(dashboardGroup, "/company", new(dashboard.CompanyController)) - ginx.HandleController(dashboardGroup, "/customer", new(dashboard.CustomerController)) - ginx.HandleController(dashboardGroup, "/customer-contact", new(dashboard.CustomerContactController)) - ginx.HandleController(dashboardGroup, "/role", new(dashboard.RoleController)) - ginx.HandleController(dashboardGroup, "/permission", new(dashboard.PermissionController)) - ginx.HandleController(dashboardGroup, "/session", new(dashboard.SessionController)) - ginx.HandleController(dashboardGroup, "/tag", new(dashboard.TagController)) - ginx.HandleController(dashboardGroup, "/conversation", new(dashboard.ConversationController)) - ginx.HandleController(dashboardGroup, "/ticket", new(dashboard.TicketController)) - ginx.HandleController(dashboardGroup, "/notification", new(dashboard.NotificationController)) - ginx.HandleController(dashboardGroup, "/quick-reply", new(dashboard.QuickReplyController)) - ginx.HandleController(dashboardGroup, "/channel", new(dashboard.ChannelController)) - ginx.HandleController(dashboardGroup, "/agent", new(dashboard.AgentController)) - ginx.HandleController(dashboardGroup, "/agent-team", new(dashboard.AgentTeamController)) - ginx.HandleController(dashboardGroup, "/agent-team-schedule", new(dashboard.AgentTeamScheduleController)) - ginx.HandleController(dashboardGroup, "/ai-agent", new(dashboard.AIAgentController)) - ginx.HandleController(dashboardGroup, "/ai-config", new(dashboard.AIConfigController)) - ginx.HandleController(dashboardGroup, "/asset", new(dashboard.AssetController)) - ginx.HandleController(dashboardGroup, "/knowledge-base", new(dashboard.KnowledgeBaseController)) - ginx.HandleController(dashboardGroup, "/knowledge-document", new(dashboard.KnowledgeDocumentController)) - ginx.HandleController(dashboardGroup, "/knowledge-faq", new(dashboard.KnowledgeFAQController)) - ginx.HandleController(dashboardGroup, "/knowledge-retrieve", new(dashboard.KnowledgeRetrieveController)) - ginx.HandleController(dashboardGroup, "/knowledge-retrieve-log", new(dashboard.KnowledgeRetrieveLogController)) - ginx.HandleController(dashboardGroup, "/agent-run-log", new(dashboard.AgentRunLogController)) - ginx.HandleController(dashboardGroup, "/skill-definition", new(dashboard.SkillDefinitionController)) - ginx.HandleController(dashboardGroup, "/mcp", new(dashboard.MCPController)) + registerDashboardDashboardRoutes(dashboardGroup.Group("/dashboard")) + registerDashboardUserRoutes(dashboardGroup.Group("/user")) + registerDashboardCompanyRoutes(dashboardGroup.Group("/company")) + registerDashboardCustomerRoutes(dashboardGroup.Group("/customer")) + registerDashboardCustomerContactRoutes(dashboardGroup.Group("/customer-contact")) + registerDashboardRoleRoutes(dashboardGroup.Group("/role")) + registerDashboardPermissionRoutes(dashboardGroup.Group("/permission")) + registerDashboardSessionRoutes(dashboardGroup.Group("/session")) + registerDashboardTagRoutes(dashboardGroup.Group("/tag")) + registerDashboardConversationRoutes(dashboardGroup.Group("/conversation")) + registerDashboardTicketRoutes(dashboardGroup.Group("/ticket")) + registerDashboardNotificationRoutes(dashboardGroup.Group("/notification")) + registerDashboardQuickReplyRoutes(dashboardGroup.Group("/quick-reply")) + registerDashboardChannelRoutes(dashboardGroup.Group("/channel")) + registerDashboardAgentRoutes(dashboardGroup.Group("/agent")) + registerDashboardAgentTeamRoutes(dashboardGroup.Group("/agent-team")) + registerDashboardAgentTeamScheduleRoutes(dashboardGroup.Group("/agent-team-schedule")) + registerDashboardAIAgentRoutes(dashboardGroup.Group("/ai-agent")) + registerDashboardAIConfigRoutes(dashboardGroup.Group("/ai-config")) + registerDashboardAssetRoutes(dashboardGroup.Group("/asset")) + registerDashboardKnowledgeBaseRoutes(dashboardGroup.Group("/knowledge-base")) + registerDashboardKnowledgeDocumentRoutes(dashboardGroup.Group("/knowledge-document")) + registerDashboardKnowledgeFAQRoutes(dashboardGroup.Group("/knowledge-faq")) + registerDashboardKnowledgeRetrieveRoutes(dashboardGroup.Group("/knowledge-retrieve")) + registerDashboardKnowledgeRetrieveLogRoutes(dashboardGroup.Group("/knowledge-retrieve-log")) + registerDashboardAgentRunLogRoutes(dashboardGroup.Group("/agent-run-log")) + registerDashboardSkillDefinitionRoutes(dashboardGroup.Group("/skill-definition")) + registerDashboardMCPRoutes(dashboardGroup.Group("/mcp")) thirdGroup := app.Group("/api/third") - ginx.HandleController(thirdGroup, "/wechat", new(third.WechatController)) + registerThirdWechatRoutes(thirdGroup.Group("/wechat")) } func registerDashboardStatic(app *gin.Engine, root string) { diff --git a/internal/pkg/ginx/mvc.go b/internal/pkg/ginx/mvc.go deleted file mode 100644 index 8088380..0000000 --- a/internal/pkg/ginx/mvc.go +++ /dev/null @@ -1,137 +0,0 @@ -package ginx - -import ( - "net/http" - "reflect" - "strconv" - "strings" - "unicode" - - "github.com/gin-gonic/gin" - "github.com/mlogclub/simple/web" -) - -var jsonResultType = reflect.TypeOf((*web.JsonResult)(nil)) - -func HandleController(group *gin.RouterGroup, relativePath string, prototype any, handlers ...gin.HandlerFunc) { - router := group.Group(relativePath, handlers...) - t := reflect.TypeOf(prototype) - if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { - panic("ginx.HandleController requires a pointer to a controller struct") - } - for i := 0; i < t.NumMethod(); i++ { - method := t.Method(i) - httpMethods, path, ok := parseAction(method) - if !ok { - continue - } - handler := buildHandler(t, method) - for _, httpMethod := range httpMethods { - router.Handle(httpMethod, path, handler) - } - } -} - -func parseAction(method reflect.Method) ([]string, string, bool) { - prefixes := []struct { - name string - methods []string - }{ - {"Any", []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions}}, - {"Get", []string{http.MethodGet}}, - {"Post", []string{http.MethodPost}}, - {"Put", []string{http.MethodPut}}, - {"Delete", []string{http.MethodDelete}}, - } - for _, prefix := range prefixes { - if !strings.HasPrefix(method.Name, prefix.name) { - continue - } - suffix := strings.TrimPrefix(method.Name, prefix.name) - return prefix.methods, actionPath(suffix, method.Type.NumIn()-1), true - } - return nil, "", false -} - -func actionPath(suffix string, argCount int) string { - if suffix == "" { - return "/" - } - if suffix == "By" && argCount == 1 { - return "/:id" - } - if strings.HasSuffix(suffix, "By") && argCount == 1 { - base := strings.TrimSuffix(suffix, "By") - return "/" + actionSegmentPath(base) + "/:id" - } - return "/" + actionSegmentPath(suffix) -} - -func actionSegmentPath(s string) string { - if s == "" { - return "" - } - parts := strings.Split(s, "_") - for i, part := range parts { - parts[i] = camelToPath(part) - } - return strings.Join(parts, "_") -} - -func camelToPath(s string) string { - var b strings.Builder - for i, r := range s { - if unicode.IsUpper(r) { - if i > 0 { - b.WriteByte('/') - } - r = unicode.ToLower(r) - } - b.WriteRune(r) - } - return b.String() -} - -func buildHandler(controllerType reflect.Type, method reflect.Method) gin.HandlerFunc { - return func(ctx *gin.Context) { - controller := reflect.New(controllerType.Elem()) - if field := controller.Elem().FieldByName("Ctx"); field.IsValid() && field.CanSet() { - field.Set(reflect.ValueOf(ctx)) - } - - args := []reflect.Value{controller} - for i := 1; i < method.Type.NumIn(); i++ { - argType := method.Type.In(i) - raw := ctx.Param("id") - value, ok := convertPathArg(raw, argType) - if !ok { - ctx.JSON(http.StatusBadRequest, web.JsonErrorMsg("路径参数错误")) - return - } - args = append(args, value) - } - - results := method.Func.Call(args) - if len(results) == 0 || results[0].IsNil() { - return - } - if result, ok := results[0].Interface().(*web.JsonResult); ok { - ctx.JSON(http.StatusOK, result) - } - } -} - -func convertPathArg(raw string, t reflect.Type) (reflect.Value, bool) { - switch t.Kind() { - case reflect.Int64: - v, err := strconv.ParseInt(raw, 10, 64) - if err != nil { - return reflect.Value{}, false - } - return reflect.ValueOf(v), true - case reflect.String: - return reflect.ValueOf(raw), true - default: - return reflect.Zero(t), false - } -}