diff --git a/internal/bootstrap/controller_routes.go b/internal/bootstrap/controller_routes.go index d329703..cf06e94 100644 --- a/internal/bootstrap/controller_routes.go +++ b/internal/bootstrap/controller_routes.go @@ -13,10 +13,6 @@ import ( "github.com/mlogclub/simple/web" ) -func writeJSON(ctx *gin.Context, result *web.JsonResult) { - httpx.WriteJSON(ctx, result) -} - func pathInt64(ctx *gin.Context, name string) (int64, bool) { value, err := strconv.ParseInt(ctx.Param(name), 10, 64) if err != nil { @@ -37,944 +33,421 @@ func registerApiAuthRoutes(group *gin.RouterGroup) { } func registerApiChannelRoutes(group *gin.RouterGroup) { - group.Any("/config", func(ctx *gin.Context) { - controller := &api.ChannelController{Ctx: ctx} - writeJSON(ctx, controller.AnyConfig()) - }) + group.Any("/config", api.ChannelAnyConfig) +} + +func registerApiCustomerRoutes(group *gin.RouterGroup) { + group.POST("/session_exchange", api.CustomerPostSession_exchange) } 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()) + api.ConversationGetBy(ctx, id) }) + group.POST("/close", api.ConversationPostClose) + group.POST("/create_or_match", api.ConversationPostCreate_or_match) } 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()) - }) + group.Any("/list", api.MessageAnyList) + group.POST("/read", api.MessagePostRead) + group.POST("/send", api.MessagePostSend) + group.POST("/upload_attachment", api.MessagePostUpload_attachment) + group.POST("/upload_image", api.MessagePostUpload_image) } 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()) - }) + group.GET("/overview", dashboard.DashboardGetOverview) } 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)) + dashboard.UserGetBy(ctx, id) }) - group.POST("/assign_role", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostAssign_role()) + group.POST("/assign_role", dashboard.UserPostAssign_role) + group.POST("/change_password", dashboard.UserPostChange_password) + group.POST("/create", dashboard.UserPostCreate) + group.POST("/delete", dashboard.UserPostDelete) + group.Any("/list", dashboard.UserAnyList) + group.Any("/list_all", dashboard.UserAnyList_all) + group.POST("/reset_password", dashboard.UserPostReset_password) + group.POST("/update", dashboard.UserPostUpdate) + group.POST("/update_status", dashboard.UserPostUpdate_status) +} + +func registerDashboardCompanyRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.CompanyGetBy(ctx, id) }) - group.POST("/change_password", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostChange_password()) + group.POST("/create", dashboard.CompanyPostCreate) + group.POST("/delete", dashboard.CompanyPostDelete) + group.Any("/list", dashboard.CompanyAnyList) + group.POST("/update", dashboard.CompanyPostUpdate) + group.POST("/update_status", dashboard.CompanyPostUpdate_status) +} + +func registerDashboardCustomerRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.CustomerGetBy(ctx, id) }) - group.POST("/create", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostCreate()) + group.POST("/create", dashboard.CustomerPostCreate) + group.POST("/delete", dashboard.CustomerPostDelete) + group.POST("/list", dashboard.CustomerPostList) + group.POST("/save_profile", dashboard.CustomerPostSave_profile) + group.POST("/update", dashboard.CustomerPostUpdate) + group.POST("/update_status", dashboard.CustomerPostUpdate_status) +} + +func registerDashboardCustomerContactRoutes(group *gin.RouterGroup) { + group.POST("/create", dashboard.CustomerContactPostCreate) + group.POST("/delete", dashboard.CustomerContactPostDelete) + group.Any("/list", dashboard.CustomerContactAnyList) + group.POST("/update", dashboard.CustomerContactPostUpdate) +} + +func registerDashboardRoleRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.RoleGetBy(ctx, id) }) - group.POST("/delete", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostDelete()) + group.POST("/assign_permission", dashboard.RolePostAssign_permission) + group.POST("/create", dashboard.RolePostCreate) + group.POST("/delete", dashboard.RolePostDelete) + group.Any("/list", dashboard.RoleAnyList) + group.GET("/list_all", dashboard.RoleGetList_all) + group.POST("/update", dashboard.RolePostUpdate) + group.POST("/update_sort", dashboard.RolePostUpdate_sort) + group.POST("/update_status", dashboard.RolePostUpdate_status) +} + +func registerDashboardPermissionRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.PermissionGetBy(ctx, id) }) - group.Any("/list", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.AnyList()) + group.Any("/list", dashboard.PermissionAnyList) +} + +func registerDashboardSessionRoutes(group *gin.RouterGroup) { + group.Any("/list", dashboard.SessionAnyList) + group.POST("/revoke", dashboard.SessionPostRevoke) + group.POST("/revoke/by/user", dashboard.SessionPostRevokeByUser) +} + +func registerDashboardTagRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.TagGetBy(ctx, id) }) - group.Any("/list_all", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.AnyList_all()) + group.POST("/create", dashboard.TagPostCreate) + group.POST("/delete", dashboard.TagPostDelete) + group.Any("/list", dashboard.TagAnyList) + group.GET("/list_all", dashboard.TagGetList_all) + group.POST("/update", dashboard.TagPostUpdate) + group.POST("/update_sort", dashboard.TagPostUpdate_sort) + group.POST("/update_status", dashboard.TagPostUpdate_status) +} + +func registerDashboardConversationRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.ConversationGetBy(ctx, id) }) - group.POST("/reset_password", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostReset_password()) + group.POST("/add_tag", dashboard.ConversationPostAdd_tag) + group.POST("/assign", dashboard.ConversationPostAssign) + group.POST("/close", dashboard.ConversationPostClose) + group.Any("/conversations", dashboard.ConversationAnyConversations) + group.POST("/dispatch", dashboard.ConversationPostDispatch) + group.POST("/link_customer", dashboard.ConversationPostLink_customer) + group.Any("/list", dashboard.ConversationAnyList) + group.Any("/message_list", dashboard.ConversationAnyMessage_list) + group.POST("/read", dashboard.ConversationPostRead) + group.POST("/recall_message", dashboard.ConversationPostRecall_message) + group.POST("/remove_tag", dashboard.ConversationPostRemove_tag) + group.POST("/send_message", dashboard.ConversationPostSend_message) + group.POST("/transfer", dashboard.ConversationPostTransfer) + group.POST("/upload_attachment", dashboard.ConversationPostUpload_attachment) + group.POST("/upload_image", dashboard.ConversationPostUpload_image) +} + +func registerDashboardTicketRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.TicketGetBy(ctx, id) }) - group.POST("/update", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostUpdate()) + group.POST("/assign", dashboard.TicketPostAssign) + group.POST("/change_status", dashboard.TicketPostChange_status) + group.POST("/create", dashboard.TicketPostCreate) + group.POST("/create_from_conversation", dashboard.TicketPostCreate_from_conversation) + group.POST("/delete_view", dashboard.TicketPostDelete_view) + group.POST("/link_customer", dashboard.TicketPostLink_customer) + group.Any("/list", dashboard.TicketAnyList) + group.POST("/progress/create", dashboard.TicketPostProgressCreate) + group.Any("/progress/list", dashboard.TicketAnyProgressList) + group.POST("/save_view", dashboard.TicketPostSave_view) + group.Any("/summary", dashboard.TicketAnySummary) + group.POST("/update", dashboard.TicketPostUpdate) + group.Any("/view_list", dashboard.TicketAnyView_list) +} + +func registerDashboardNotificationRoutes(group *gin.RouterGroup) { + group.Any("/list", dashboard.NotificationAnyList) + group.POST("/mark_all_read", dashboard.NotificationPostMark_all_read) + group.POST("/mark_read", dashboard.NotificationPostMark_read) + group.GET("/unread_count", dashboard.NotificationGetUnread_count) +} + +func registerDashboardQuickReplyRoutes(group *gin.RouterGroup) { + group.POST("/create", dashboard.QuickReplyPostCreate) + group.POST("/delete", dashboard.QuickReplyPostDelete) + group.Any("/list", dashboard.QuickReplyAnyList) + group.GET("/list_all", dashboard.QuickReplyGetList_all) + group.POST("/update", dashboard.QuickReplyPostUpdate) +} + +func registerDashboardChannelRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.ChannelGetBy(ctx, id) }) - group.POST("/update_status", func(ctx *gin.Context) { - controller := &dashboard.UserController{Ctx: ctx} - writeJSON(ctx, controller.PostUpdate_status()) + group.POST("/create", dashboard.ChannelPostCreate) + group.POST("/delete", dashboard.ChannelPostDelete) + group.Any("/list", dashboard.ChannelAnyList) + group.POST("/reset_user_token_secret", dashboard.ChannelPostReset_user_token_secret) + group.POST("/update", dashboard.ChannelPostUpdate) + group.POST("/update_status", dashboard.ChannelPostUpdate_status) + group.Any("/wxwork/kf/accounts", dashboard.ChannelAnyWxworkKfAccounts) +} + +func registerDashboardAgentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AgentGetBy(ctx, id) }) + group.POST("/create", dashboard.AgentPostCreate) + group.POST("/delete", dashboard.AgentPostDelete) + group.Any("/list", dashboard.AgentAnyList) + group.GET("/list_all", dashboard.AgentGetList_all) + group.POST("/update", dashboard.AgentPostUpdate) +} + +func registerDashboardAgentTeamRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AgentTeamGetBy(ctx, id) + }) + group.POST("/create", dashboard.AgentTeamPostCreate) + group.POST("/delete", dashboard.AgentTeamPostDelete) + group.Any("/list", dashboard.AgentTeamAnyList) + group.GET("/list_all", dashboard.AgentTeamGetList_all) + group.POST("/update", dashboard.AgentTeamPostUpdate) +} + +func registerDashboardAgentTeamScheduleRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AgentTeamScheduleGetBy(ctx, id) + }) + group.POST("/batch_generate", dashboard.AgentTeamSchedulePostBatch_generate) + group.POST("/batch_preview", dashboard.AgentTeamSchedulePostBatch_preview) + group.Any("/calendar", dashboard.AgentTeamScheduleAnyCalendar) + group.POST("/create", dashboard.AgentTeamSchedulePostCreate) + group.POST("/delete", dashboard.AgentTeamSchedulePostDelete) + group.Any("/list", dashboard.AgentTeamScheduleAnyList) + group.POST("/update", dashboard.AgentTeamSchedulePostUpdate) +} + +func registerDashboardAIAgentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AIAgentGetBy(ctx, id) + }) + group.POST("/create", dashboard.AIAgentPostCreate) + group.POST("/delete", dashboard.AIAgentPostDelete) + group.Any("/list", dashboard.AIAgentAnyList) + group.GET("/list_all", dashboard.AIAgentGetList_all) + group.POST("/update", dashboard.AIAgentPostUpdate) + group.POST("/update_sort", dashboard.AIAgentPostUpdate_sort) + group.POST("/update_status", dashboard.AIAgentPostUpdate_status) +} + +func registerDashboardAIConfigRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AIConfigGetBy(ctx, id) + }) + group.POST("/create", dashboard.AIConfigPostCreate) + group.POST("/delete", dashboard.AIConfigPostDelete) + group.Any("/list", dashboard.AIConfigAnyList) + group.Any("/list_all", dashboard.AIConfigAnyList_all) + group.POST("/update", dashboard.AIConfigPostUpdate) + group.POST("/update_sort", dashboard.AIConfigPostUpdate_sort) + group.POST("/update_status", dashboard.AIConfigPostUpdate_status) +} + +func registerDashboardAssetRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AssetGetBy(ctx, id) + }) + group.POST("/create", dashboard.AssetPostCreate) + group.POST("/delete", dashboard.AssetPostDelete) + group.Any("/list", dashboard.AssetAnyList) +} + +func registerDashboardKnowledgeBaseRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.KnowledgeBaseGetBy(ctx, id) + }) + group.POST("/create", dashboard.KnowledgeBasePostCreate) + group.POST("/delete", dashboard.KnowledgeBasePostDelete) + group.Any("/list", dashboard.KnowledgeBaseAnyList) + group.Any("/list_all", dashboard.KnowledgeBaseAnyList_all) + group.POST("/rebuild_index", dashboard.KnowledgeBasePostRebuild_index) + group.POST("/update", dashboard.KnowledgeBasePostUpdate) + group.POST("/update_sort", dashboard.KnowledgeBasePostUpdate_sort) +} + +func registerDashboardKnowledgeDocumentRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.KnowledgeDocumentGetBy(ctx, id) + }) + group.POST("/create", dashboard.KnowledgeDocumentPostCreate) + group.POST("/delete", dashboard.KnowledgeDocumentPostDelete) + group.Any("/list", dashboard.KnowledgeDocumentAnyList) + group.POST("/update", dashboard.KnowledgeDocumentPostUpdate) +} + +func registerDashboardKnowledgeFAQRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.KnowledgeFAQGetBy(ctx, id) + }) + group.POST("/create", dashboard.KnowledgeFAQPostCreate) + group.POST("/delete", dashboard.KnowledgeFAQPostDelete) + group.Any("/list", dashboard.KnowledgeFAQAnyList) + group.POST("/update", dashboard.KnowledgeFAQPostUpdate) +} + +func registerDashboardKnowledgeRetrieveRoutes(group *gin.RouterGroup) { + group.POST("/build", dashboard.KnowledgeRetrievePostBuild) + group.POST("/debug/answer", dashboard.KnowledgeRetrievePostDebugAnswer) + group.POST("/debug/search", dashboard.KnowledgeRetrievePostDebugSearch) +} + +func registerDashboardKnowledgeRetrieveLogRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.KnowledgeRetrieveLogGetBy(ctx, id) + }) + group.Any("/list", dashboard.KnowledgeRetrieveLogAnyList) +} + +func registerDashboardAgentRunLogRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.AgentRunLogGetBy(ctx, id) + }) + group.Any("/list", dashboard.AgentRunLogAnyList) +} + +func registerDashboardSkillDefinitionRoutes(group *gin.RouterGroup) { + group.GET("/:id", func(ctx *gin.Context) { + id, ok := pathInt64(ctx, "id") + if !ok { + return + } + dashboard.SkillDefinitionGetBy(ctx, id) + }) + group.POST("/create", dashboard.SkillDefinitionPostCreate) + group.POST("/debug_resume", dashboard.SkillDefinitionPostDebug_resume) + group.POST("/debug_run", dashboard.SkillDefinitionPostDebug_run) + group.POST("/delete", dashboard.SkillDefinitionPostDelete) + group.Any("/list", dashboard.SkillDefinitionAnyList) + group.GET("/list_all", dashboard.SkillDefinitionGetList_all) + group.POST("/restore", dashboard.SkillDefinitionPostRestore) + group.POST("/update", dashboard.SkillDefinitionPostUpdate) + group.POST("/update_status", dashboard.SkillDefinitionPostUpdate_status) +} + +func registerDashboardMCPRoutes(group *gin.RouterGroup) { + group.POST("/call_tool", dashboard.MCPPostCall_tool) + group.Any("/catalog", dashboard.MCPAnyCatalog) + group.Any("/list_servers", dashboard.MCPAnyList_servers) + group.POST("/list_tools", dashboard.MCPPostList_tools) + group.POST("/test_connection", dashboard.MCPPostTest_connection) } 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() - }) + group.GET("/callback", third.WechatGetCallback) + group.POST("/callback", third.WechatPostCallback) } diff --git a/internal/controllers/api/channel_controller.go b/internal/controllers/api/channel_controller.go index a029535..5279b4f 100644 --- a/internal/controllers/api/channel_controller.go +++ b/internal/controllers/api/channel_controller.go @@ -4,24 +4,23 @@ import ( "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" "cs-agent/internal/pkg/errorsx" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type ChannelController struct { - Ctx *gin.Context -} - -func (c *ChannelController) AnyConfig() *web.JsonResult { - channel := services.ChannelService.GetEnabledChannel(c.Ctx) +func ChannelAnyConfig(ctx *gin.Context) { + channel := services.ChannelService.GetEnabledChannel(ctx) if channel == nil { - return web.JsonErrorMsg("接入渠道未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } cfg, err := resolveWidgetConfig(channel.ChannelType, channel.ConfigJSON) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := response.WidgetConfigResponse{ @@ -33,7 +32,8 @@ func (c *ChannelController) AnyConfig() *web.JsonResult { Position: cfg.Position, Width: cfg.Width, } - return web.JsonData(ret) + httpx.WriteJSON(ctx, ret) + return } type webLikeWidgetConfig struct { diff --git a/internal/controllers/api/conversation_controller.go b/internal/controllers/api/conversation_controller.go index 9a33882..67ebb10 100644 --- a/internal/controllers/api/conversation_controller.go +++ b/internal/controllers/api/conversation_controller.go @@ -8,70 +8,81 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type ConversationController struct { - Ctx *gin.Context -} - -func (c *ConversationController) GetBy(id int64) *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func ConversationGetBy(ctx *gin.Context, id int64) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } item := services.ConversationService.Get(id) if item == nil { - return web.JsonErrorMsg("会话不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在")) + return } if !services.ConversationService.IsCustomerConversationOwner(item, *external) { - return web.JsonErrorMsg("无权访问该会话") + httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话")) + return } detail := response.ConversationDetailResponse{ ConversationResponse: builders.BuildConversation(item), Participants: builders.BuildParticipantResponses(id), } - return web.JsonData(detail) + httpx.WriteJSON(ctx, detail) + return } -func (c *ConversationController) PostCreate_or_match() *web.JsonResult { - channel := services.ChannelService.GetEnabledChannel(c.Ctx) +func ConversationPostCreate_or_match(ctx *gin.Context) { + channel := services.ChannelService.GetEnabledChannel(ctx) if channel == nil { - return web.JsonErrorMsg("接入渠道未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } item, err := services.ConversationService.Create(*external, channel.ID, channel.AIAgentID) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildConversation(item)) + httpx.WriteJSON(ctx, builders.BuildConversation(item)) + return } -func (c *ConversationController) PostClose() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func ConversationPostClose(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } req := request.CloseConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.CloseCustomerConversation(req.ConversationID, *external); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/api/customer_controller.go b/internal/controllers/api/customer_controller.go index 236bd09..4f47170 100644 --- a/internal/controllers/api/customer_controller.go +++ b/internal/controllers/api/customer_controller.go @@ -1,6 +1,7 @@ package api import ( + "cs-agent/internal/pkg/httpx" "cs-agent/internal/pkg/openidentity" "cs-agent/internal/services" @@ -8,22 +9,22 @@ import ( "github.com/mlogclub/simple/web" ) -type CustomerController struct { - Ctx *gin.Context -} - -func (c *CustomerController) PostSession_exchange() *web.JsonResult { - channel := services.ChannelService.GetEnabledChannel(c.Ctx) +func CustomerPostSession_exchange(ctx *gin.Context) { + channel := services.ChannelService.GetEnabledChannel(ctx) if channel == nil { - return web.JsonErrorMsg("接入渠道不存在或已停用") + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道不存在或已停用")) + return } - externalUser, err := openidentity.GetExternalUser(c.Ctx, services.ChannelService.GetUserTokenSecret(channel)) + externalUser, err := openidentity.GetExternalUser(ctx, services.ChannelService.GetUserTokenSecret(channel)) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } resp, err := services.CustomerSessionService.Exchange(channel, *externalUser) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } diff --git a/internal/controllers/api/message_controller.go b/internal/controllers/api/message_controller.go index 46545b9..427865d 100644 --- a/internal/controllers/api/message_controller.go +++ b/internal/controllers/api/message_controller.go @@ -10,160 +10,192 @@ import ( "strings" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" "github.com/spf13/cast" ) -type MessageController struct { - Ctx *gin.Context -} - -func (c *MessageController) AnyList() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func MessageAnyList(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } - conversationID, _ := params.GetInt64(c.Ctx, "conversationId") + conversationID, _ := params.GetInt64(ctx, "conversationId") if conversationID <= 0 { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversation := services.ConversationService.Get(conversationID) if conversation == nil { - return web.JsonErrorMsg("会话不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在")) + return } if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) { - return web.JsonErrorMsg("无权访问该会话") + httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话")) + return } var ( - senderType, _ = params.Get(c.Ctx, "senderType") - messageType, _ = params.Get(c.Ctx, "messageType") - cursor, _ = params.GetInt64(c.Ctx, "cursor") - limit, _ = params.GetInt(c.Ctx, "limit") + senderType, _ = params.Get(ctx, "senderType") + messageType, _ = params.Get(ctx, "messageType") + cursor, _ = params.GetInt64(ctx, "cursor") + limit, _ = params.GetInt(ctx, "limit") ) list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor( conversationID, cursor, limit, senderType, messageType, ) results := builders.BuildMessages(list) - return web.JsonCursorData(results, cast.ToString(nextCursor), hasMore) + httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore)) + return } -func (c *MessageController) PostSend() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func MessagePostSend(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } req := request.SendConversationMessageRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.MessageService.SendCustomerMessage(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildMessage(item)) + httpx.WriteJSON(ctx, builders.BuildMessage(item)) + return } -func (c *MessageController) PostRead() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func MessagePostRead(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } req := request.ReadConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *MessageController) PostUpload_image() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func MessagePostUpload_image(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } - rawConv := strings.TrimSpace(params.FormValue(c.Ctx, "conversationId")) + rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId")) if rawConv == "" { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversationID, err := strconv.ParseInt(rawConv, 10, 64) if err != nil || conversationID <= 0 { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversation := services.ConversationService.Get(conversationID) if conversation == nil { - return web.JsonErrorMsg("会话不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在")) + return } if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) { - return web.JsonErrorMsg("无权访问该会话") + httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话")) + return } if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - header, err := c.Ctx.FormFile("file") + header, err := ctx.FormFile("file") if err != nil { - return web.JsonErrorMsg("请选择上传图片") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片")) + return } if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") { - return web.JsonErrorMsg("仅支持上传图片文件") + httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件")) + return } item, err := services.AssetService.UploadFile(header, "images", nil) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } -func (c *MessageController) PostUpload_attachment() *web.JsonResult { - if services.ChannelService.GetEnabledChannel(c.Ctx) == nil { - return web.JsonErrorMsg("接入渠道未初始化") +func MessagePostUpload_attachment(ctx *gin.Context) { + if services.ChannelService.GetEnabledChannel(ctx) == nil { + httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) + return } - external := httpx.GetExternalUser(c.Ctx) + external := httpx.GetExternalUser(ctx) if external == nil { - return web.JsonErrorMsg("外部身份未初始化") + httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化")) + return } - rawConv := strings.TrimSpace(params.FormValue(c.Ctx, "conversationId")) + rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId")) if rawConv == "" { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversationID, err := strconv.ParseInt(rawConv, 10, 64) if err != nil || conversationID <= 0 { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - header, err := c.Ctx.FormFile("file") + header, err := ctx.FormFile("file") if err != nil { - return web.JsonErrorMsg("请选择上传附件") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件")) + return } item, err := services.AssetService.UploadFile(header, "attachments", nil) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } diff --git a/internal/controllers/dashboard/agent_controller.go b/internal/controllers/dashboard/agent_controller.go index 9f253e1..62c526b 100644 --- a/internal/controllers/dashboard/agent_controller.go +++ b/internal/controllers/dashboard/agent_controller.go @@ -4,22 +4,21 @@ import ( "cs-agent/internal/builders" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type AgentController struct { - Ctx *gin.Context -} - -func (c *AgentController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentView); err != nil { - return web.JsonError(err) +func AgentAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list, paging := services.AgentProfileService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx, + list, paging := services.AgentProfileService.FindPageByCnd(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "userId"}, params.QueryFilter{ParamName: "teamId"}, params.QueryFilter{ParamName: "serviceStatus"}, @@ -27,75 +26,93 @@ func (c *AgentController) AnyList() *web.JsonResult { params.QueryFilter{ParamName: "displayName", Op: params.Like}, ).Desc("id")) results := builders.BuildAgentProfileList(list) - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AgentController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentView); err != nil { - return web.JsonError(err) +func AgentGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list := services.AgentProfileService.Find(params.NewPagedSqlCnd(c.Ctx, + list := services.AgentProfileService.Find(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "userId"}, params.QueryFilter{ParamName: "teamId"}, params.QueryFilter{ParamName: "serviceStatus"}, params.QueryFilter{ParamName: "agentCode", Op: params.Like}, ).Desc("id")) - return web.JsonData(builders.BuildAgentProfileList(list)) + httpx.WriteJSON(ctx, builders.BuildAgentProfileList(list)) + return } -func (c *AgentController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentView); err != nil { - return web.JsonError(err) +func AgentGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AgentProfileService.Get(id) if item == nil { - return web.JsonErrorMsg("客服档案不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("客服档案不存在")) + return } - return web.JsonData(builders.BuildAgentProfileResponse(item)) + httpx.WriteJSON(ctx, builders.BuildAgentProfileResponse(item)) + return } -func (c *AgentController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentCreate) +func AgentPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAgentProfileRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.AgentProfileService.CreateAgentProfile(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAgentProfileResponse(item)) + httpx.WriteJSON(ctx, builders.BuildAgentProfileResponse(item)) + return } -func (c *AgentController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentUpdate) +func AgentPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAgentProfileRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentProfileService.UpdateAgentProfile(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AgentController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentDelete); err != nil { - return web.JsonError(err) +func AgentPostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAgentProfileRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentProfileService.DeleteAgentProfile(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/agent_run_log_controller.go b/internal/controllers/dashboard/agent_run_log_controller.go index 6382b26..c344994 100644 --- a/internal/controllers/dashboard/agent_run_log_controller.go +++ b/internal/controllers/dashboard/agent_run_log_controller.go @@ -4,23 +4,22 @@ import ( "cs-agent/internal/builders" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type AgentRunLogController struct { - Ctx *gin.Context -} - -func (c *AgentRunLogController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView); err != nil { - return web.JsonError(err) +func AgentRunLogAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "conversationId"}, params.QueryFilter{ParamName: "messageId"}, params.QueryFilter{ParamName: "aiAgentId"}, @@ -34,27 +33,31 @@ func (c *AgentRunLogController) AnyList() *web.JsonResult { params.QueryFilter{ParamName: "finalAction"}, params.QueryFilter{ParamName: "userMessage", Op: params.Like}, ).Desc("id") - if hitlStatus, _ := params.Get(c.Ctx, "hitlStatus"); hitlStatus != "" && hitlStatus != "all" { + if hitlStatus, _ := params.Get(ctx, "hitlStatus"); hitlStatus != "" && hitlStatus != "all" { cnd = services.AgentRunLogService.ApplyHITLStatusFilter(cnd, hitlStatus) } - queryParams := params.NewQueryParams(c.Ctx) + queryParams := params.NewQueryParams(ctx) queryParams.Cnd = *cnd list, paging := services.AgentRunLogService.FindPageByParams(queryParams) results := make([]response.AgentRunLogResponse, 0, len(list)) for _, item := range list { results = append(results, builders.BuildAgentRunLog(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AgentRunLogController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView); err != nil { - return web.JsonError(err) +func AgentRunLogGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AgentRunLogService.Get(id) if item == nil { - return web.JsonErrorMsg("Agent 运行日志不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Agent 运行日志不存在")) + return } - return web.JsonData(builders.BuildAgentRunLog(item)) + httpx.WriteJSON(ctx, builders.BuildAgentRunLog(item)) + return } diff --git a/internal/controllers/dashboard/agent_team_controller.go b/internal/controllers/dashboard/agent_team_controller.go index 844af79..3c0c1be 100644 --- a/internal/controllers/dashboard/agent_team_controller.go +++ b/internal/controllers/dashboard/agent_team_controller.go @@ -6,28 +6,27 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type AgentTeamController struct { - Ctx *gin.Context -} - -func (c *AgentTeamController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamView); err != nil { - return web.JsonError(err) +func AgentTeamAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewSqlCnd(c.Ctx, + cnd := params.NewSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "leaderUserId"}, params.QueryFilter{ParamName: "name", Op: params.Like}, ).Desc("id") - if _, ok := params.Get(c.Ctx, "status"); !ok { + if _, ok := params.Get(ctx, "status"); !ok { cnd.Where("status <> ?", enums.StatusDeleted) } list := services.AgentTeamService.Find(cnd) @@ -35,76 +34,94 @@ func (c *AgentTeamController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, buildAgentTeamResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *AgentTeamController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamView); err != nil { - return web.JsonError(err) +func AgentTeamGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamView); err != nil { + httpx.WriteJSON(ctx, err) + return } list := services.AgentTeamService.Find(sqls.NewCnd().Eq("status", enums.StatusOk)) results := make([]response.AgentTeamResponse, 0, len(list)) for _, item := range list { results = append(results, buildAgentTeamResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *AgentTeamController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamView); err != nil { - return web.JsonError(err) +func AgentTeamGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AgentTeamService.Get(id) if item == nil || item.Status == enums.StatusDeleted { - return web.JsonErrorMsg("客服组不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("客服组不存在")) + return } - return web.JsonData(buildAgentTeamResponse(item)) + httpx.WriteJSON(ctx, buildAgentTeamResponse(item)) + return } -func (c *AgentTeamController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamCreate) +func AgentTeamPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAgentTeamRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.AgentTeamService.CreateAgentTeam(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(buildAgentTeamResponse(item)) + httpx.WriteJSON(ctx, buildAgentTeamResponse(item)) + return } -func (c *AgentTeamController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamUpdate) +func AgentTeamPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAgentTeamRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentTeamService.UpdateAgentTeam(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AgentTeamController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamDelete) +func AgentTeamPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAgentTeamRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentTeamService.DeleteAgentTeam(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } func buildAgentTeamResponse(item *models.AgentTeam) response.AgentTeamResponse { diff --git a/internal/controllers/dashboard/agent_team_schedule_controller.go b/internal/controllers/dashboard/agent_team_schedule_controller.go index 57a451b..581dfc5 100644 --- a/internal/controllers/dashboard/agent_team_schedule_controller.go +++ b/internal/controllers/dashboard/agent_team_schedule_controller.go @@ -6,22 +6,21 @@ import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type AgentTeamScheduleController struct { - Ctx *gin.Context -} - -func (c *AgentTeamScheduleController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleView); err != nil { - return web.JsonError(err) +func AgentTeamScheduleAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "teamId"}, ).Desc("start_at").Desc("id") list, paging := services.AgentTeamScheduleService.FindPageByCnd(cnd) @@ -29,117 +28,144 @@ func (c *AgentTeamScheduleController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, buildAgentTeamScheduleResponse(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AgentTeamScheduleController) AnyCalendar() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleView); err != nil { - return web.JsonError(err) +func AgentTeamScheduleAnyCalendar(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleView); err != nil { + httpx.WriteJSON(ctx, err) + return } - startAt, _ := params.Get(c.Ctx, "startAt") - endAt, _ := params.Get(c.Ctx, "endAt") - teamID, _ := params.GetInt64(c.Ctx, "teamId") + startAt, _ := params.Get(ctx, "startAt") + endAt, _ := params.Get(ctx, "endAt") + teamID, _ := params.GetInt64(ctx, "teamId") list, err := services.AgentTeamScheduleService.FindCalendarSchedules(request.AgentTeamScheduleCalendarRequest{ StartAt: startAt, EndAt: endAt, TeamID: teamID, }) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } results := make([]response.AgentTeamScheduleResponse, 0, len(list)) for _, item := range list { results = append(results, buildAgentTeamScheduleResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *AgentTeamScheduleController) PostBatch_preview() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleBatchGenerate) +func AgentTeamSchedulePostBatch_preview(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleBatchGenerate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AgentTeamScheduleBatchRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } ret, err := services.AgentTeamScheduleService.BatchPreview(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAgentTeamScheduleBatchPreviewResponse(ret)) + httpx.WriteJSON(ctx, builders.BuildAgentTeamScheduleBatchPreviewResponse(ret)) + return } -func (c *AgentTeamScheduleController) PostBatch_generate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleBatchGenerate) +func AgentTeamSchedulePostBatch_generate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleBatchGenerate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AgentTeamScheduleBatchRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } ret, err := services.AgentTeamScheduleService.BatchGenerate(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAgentTeamScheduleBatchGenerateResponse(ret)) + httpx.WriteJSON(ctx, builders.BuildAgentTeamScheduleBatchGenerateResponse(ret)) + return } -func (c *AgentTeamScheduleController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleView); err != nil { - return web.JsonError(err) +func AgentTeamScheduleGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AgentTeamScheduleService.Get(id) if item == nil { - return web.JsonErrorMsg("客服组排班不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("客服组排班不存在")) + return } - return web.JsonData(buildAgentTeamScheduleResponse(item)) + httpx.WriteJSON(ctx, buildAgentTeamScheduleResponse(item)) + return } -func (c *AgentTeamScheduleController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleCreate) +func AgentTeamSchedulePostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAgentTeamScheduleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.AgentTeamScheduleService.CreateAgentTeamSchedule(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(buildAgentTeamScheduleResponse(item)) + httpx.WriteJSON(ctx, buildAgentTeamScheduleResponse(item)) + return } -func (c *AgentTeamScheduleController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleUpdate) +func AgentTeamSchedulePostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAgentTeamScheduleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentTeamScheduleService.UpdateAgentTeamSchedule(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AgentTeamScheduleController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAgentTeamScheduleDelete); err != nil { - return web.JsonError(err) +func AgentTeamSchedulePostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAgentTeamScheduleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AgentTeamScheduleService.DeleteAgentTeamSchedule(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } func buildAgentTeamScheduleResponse(item *models.AgentTeamSchedule) response.AgentTeamScheduleResponse { diff --git a/internal/controllers/dashboard/ai_agent_controller.go b/internal/controllers/dashboard/ai_agent_controller.go index ef13ee4..15d0dbe 100644 --- a/internal/controllers/dashboard/ai_agent_controller.go +++ b/internal/controllers/dashboard/ai_agent_controller.go @@ -1,6 +1,7 @@ package dashboard import ( + "cs-agent/internal/pkg/httpx" "encoding/json" "strings" @@ -14,20 +15,18 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type AIAgentController struct { - Ctx *gin.Context -} - -func (c *AIAgentController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentView); err != nil { - return web.JsonError(err) +func AIAgentAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "name", Op: params.Like}, params.QueryFilter{ParamName: "code", Op: params.Like}, @@ -37,105 +36,131 @@ func (c *AIAgentController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, buildAIAgentResponse(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AIAgentController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentView); err != nil { - return web.JsonError(err) +func AIAgentGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } list := services.AIAgentService.Find(sqls.NewCnd().Where("status = ?", enums.StatusOk).Desc("sort_no").Desc("id")) results := make([]response.AIAgentResponse, 0, len(list)) for _, item := range list { results = append(results, buildAIAgentResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *AIAgentController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentView); err != nil { - return web.JsonError(err) +func AIAgentGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AIAgentService.Get(id) if item == nil { - return web.JsonErrorMsg("AI Agent 不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("AI Agent 不存在")) + return } - return web.JsonData(buildAIAgentResponse(item)) + httpx.WriteJSON(ctx, buildAIAgentResponse(item)) + return } -func (c *AIAgentController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentCreate) +func AIAgentPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAIAgentRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.AIAgentService.CreateAIAgent(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(buildAIAgentResponse(item)) + httpx.WriteJSON(ctx, buildAIAgentResponse(item)) + return } -func (c *AIAgentController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentUpdate) +func AIAgentPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAIAgentRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIAgentService.UpdateAIAgent(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIAgentController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentDelete) +func AIAgentPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAIAgentRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIAgentService.DeleteAIAgent(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIAgentController) PostUpdate_sort() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentUpdate); err != nil { - return web.JsonError(err) +func AIAgentPostUpdate_sort(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentUpdate); err != nil { + httpx.WriteJSON(ctx, err) + return } var ids []int64 - if err := params.ReadJSON(c.Ctx, &ids); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &ids); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIAgentService.UpdateSort(ids); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIAgentController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIAgentUpdate) +func AIAgentPostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAIAgentStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIAgentService.UpdateStatus(req.ID, req.Status, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } func buildAIAgentResponse(item *models.AIAgent) response.AIAgentResponse { diff --git a/internal/controllers/dashboard/ai_config_controller.go b/internal/controllers/dashboard/ai_config_controller.go index fe59eeb..374815c 100644 --- a/internal/controllers/dashboard/ai_config_controller.go +++ b/internal/controllers/dashboard/ai_config_controller.go @@ -5,22 +5,21 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type AIConfigController struct { - Ctx *gin.Context -} - -func (c *AIConfigController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigView); err != nil { - return web.JsonError(err) +func AIConfigAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list, paging := services.AIConfigService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx, + list, paging := services.AIConfigService.FindPageByCnd(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "provider"}, params.QueryFilter{ParamName: "modelType"}, @@ -31,15 +30,17 @@ func (c *AIConfigController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, response.BuildAIConfigResponse(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AIConfigController) AnyList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigView); err != nil { - return web.JsonError(err) +func AIConfigAnyList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list := services.AIConfigService.Find(params.NewSqlCnd(c.Ctx, + list := services.AIConfigService.Find(params.NewSqlCnd(ctx, params.QueryFilter{ParamName: "modelType"}, ).Eq("status", enums.StatusOk).Desc("sort_no").Desc("id")) @@ -47,97 +48,121 @@ func (c *AIConfigController) AnyList_all() *web.JsonResult { for _, item := range list { results = append(results, response.BuildAIConfigResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *AIConfigController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigView); err != nil { - return web.JsonError(err) +func AIConfigGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AIConfigService.Get(id) if item == nil || item.Status == enums.StatusDeleted { - return web.JsonErrorMsg("AI配置不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("AI配置不存在")) + return } - return web.JsonData(response.BuildAIConfigResponse(item)) + httpx.WriteJSON(ctx, response.BuildAIConfigResponse(item)) + return } -func (c *AIConfigController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigCreate) +func AIConfigPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAIConfigRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.AIConfigService.CreateAIConfig(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(response.BuildAIConfigResponse(item)) + httpx.WriteJSON(ctx, response.BuildAIConfigResponse(item)) + return } -func (c *AIConfigController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigUpdate) +func AIConfigPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAIConfigRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIConfigService.UpdateAIConfig(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIConfigController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigDelete) +func AIConfigPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAIConfigRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIConfigService.DeleteAIConfig(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIConfigController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigUpdate) +func AIConfigPostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateAIConfigStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIConfigService.UpdateStatus(req.ID, req.Status, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *AIConfigController) PostUpdate_sort() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAIConfigUpdate); err != nil { - return web.JsonError(err) +func AIConfigPostUpdate_sort(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigUpdate); err != nil { + httpx.WriteJSON(ctx, err) + return } var ids []int64 - if err := params.ReadJSON(c.Ctx, &ids); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &ids); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AIConfigService.UpdateSort(ids); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/asset_controller.go b/internal/controllers/dashboard/asset_controller.go index 40828f5..334e43b 100644 --- a/internal/controllers/dashboard/asset_controller.go +++ b/internal/controllers/dashboard/asset_controller.go @@ -6,30 +6,29 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "strings" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type AssetController struct { - Ctx *gin.Context -} - -func (c *AssetController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAssetView); err != nil { - return web.JsonError(err) +func AssetAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAssetView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "provider"}, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "createUserId"}, params.QueryFilter{ParamName: "filename", Op: params.Like}, ).Desc("id") - if strings.TrimSpace(c.Ctx.Query("status")) == "" { + if strings.TrimSpace(ctx.Query("status")) == "" { cnd = cnd.Eq("status", enums.AssetStatusSuccess) } @@ -38,53 +37,66 @@ func (c *AssetController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildAsset(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *AssetController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAssetView); err != nil { - return web.JsonError(err) +func AssetGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAssetView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.AssetService.Get(id) if item == nil { - return web.JsonErrorMsg("文件不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("文件不存在")) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } -func (c *AssetController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAssetCreate) +func AssetPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAssetCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateAssetRequest{} - if err := params.ReadForm(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadForm(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } - header, err := c.Ctx.FormFile("file") + header, err := ctx.FormFile("file") if err != nil { - return web.JsonErrorMsg("请选择上传文件") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传文件")) + return } item, err := services.AssetService.UploadFile(header, req.Prefix, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } -func (c *AssetController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionAssetDelete) +func AssetPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAssetDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteAssetRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.AssetService.DeleteAsset(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/channel_controller.go b/internal/controllers/dashboard/channel_controller.go index f40076f..4f7394c 100644 --- a/internal/controllers/dashboard/channel_controller.go +++ b/internal/controllers/dashboard/channel_controller.go @@ -6,22 +6,21 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type ChannelController struct { - Ctx *gin.Context -} - -func (c *ChannelController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelView); err != nil { - return web.JsonError(err) +func ChannelAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list, paging := services.ChannelService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx, + list, paging := services.ChannelService.FindPageByCnd(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "name", Op: params.Like}, params.QueryFilter{ParamName: "channelType"}, @@ -31,106 +30,133 @@ func (c *ChannelController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, buildChannelResponse(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *ChannelController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelView); err != nil { - return web.JsonError(err) +func ChannelGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.ChannelService.Get(id) if item == nil || item.Status == enums.StatusDeleted { - return web.JsonErrorMsg("channel not found") + httpx.WriteJSON(ctx, web.JsonErrorMsg("channel not found")) + return } - return web.JsonData(buildChannelResponse(item)) + httpx.WriteJSON(ctx, buildChannelResponse(item)) + return } -func (c *ChannelController) AnyWxworkKfAccounts() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelView); err != nil { - return web.JsonError(err) +func ChannelAnyWxworkKfAccounts(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); err != nil { + httpx.WriteJSON(ctx, err) + return } list, err := services.ChannelService.ListWxWorkKFAccounts() if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(list) + httpx.WriteJSON(ctx, list) + return } -func (c *ChannelController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelCreate) +func ChannelPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateChannelRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.ChannelService.CreateChannel(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(buildChannelResponse(item)) + httpx.WriteJSON(ctx, buildChannelResponse(item)) + return } -func (c *ChannelController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelUpdate) +func ChannelPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateChannelRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ChannelService.UpdateChannel(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ChannelController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelUpdate) +func ChannelPostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateChannelStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ChannelService.UpdateStatus(req.ID, req.Status, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ChannelController) PostReset_user_token_secret() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelUpdate) +func ChannelPostReset_user_token_secret(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.ResetChannelUserTokenSecretRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } secret, err := services.ChannelService.ResetUserTokenSecret(req.ID, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(map[string]string{"userTokenSecret": secret}) + httpx.WriteJSON(ctx, map[string]string{"userTokenSecret": secret}) + return } -func (c *ChannelController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionChannelDelete) +func ChannelPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteChannelRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ChannelService.DeleteChannel(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } func buildChannelResponse(item *models.Channel) response.ChannelResponse { diff --git a/internal/controllers/dashboard/company_controller.go b/internal/controllers/dashboard/company_controller.go index 5a9372c..fb3e536 100644 --- a/internal/controllers/dashboard/company_controller.go +++ b/internal/controllers/dashboard/company_controller.go @@ -5,22 +5,21 @@ import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type CompanyController struct { - Ctx *gin.Context -} - -func (c *CompanyController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyView); err != nil { - return web.JsonError(err) +func CompanyAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list, paging := services.CompanyService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx, + list, paging := services.CompanyService.FindPageByCnd(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "name", Op: params.Like}, params.QueryFilter{ParamName: "code", Op: params.Like}, @@ -35,79 +34,99 @@ func (c *CompanyController) AnyList() *web.JsonResult { for i := range results { results[i].CustomerCount = countMap[results[i].ID] } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *CompanyController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyView); err != nil { - return web.JsonError(err) +func CompanyGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.CompanyService.Get(id) if item == nil || item.Status == enums.StatusDeleted { - return web.JsonData(nil) + httpx.WriteJSON(ctx, nil) + return } ret := builders.BuildCompany(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } -func (c *CompanyController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyCreate) +func CompanyPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateCompanyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.CompanyService.CreateCompany(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := builders.BuildCompany(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } -func (c *CompanyController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyUpdate) +func CompanyPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateCompanyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CompanyService.UpdateCompany(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *CompanyController) PostDelete() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyDelete) +func CompanyPostDelete(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteCompanyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CompanyService.DeleteCompany(req.ID, *user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *CompanyController) PostUpdate_status() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCompanyUpdate) +func CompanyPostUpdate_status(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateCompanyStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CompanyService.UpdateStatus(req.ID, req.Status, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/conversation_controller.go b/internal/controllers/dashboard/conversation_controller.go index 51f2870..81814c2 100644 --- a/internal/controllers/dashboard/conversation_controller.go +++ b/internal/controllers/dashboard/conversation_controller.go @@ -6,57 +6,58 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "strconv" "strings" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/common/strs" "github.com/mlogclub/simple/web" "github.com/spf13/cast" ) -type ConversationController struct { - Ctx *gin.Context -} - -func (c *ConversationController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView); err != nil { - return web.JsonError(err) +func ConversationAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "serviceMode"}, params.QueryFilter{ParamName: "currentAssigneeId"}, ).Desc("last_message_at").Desc("id") - paging := params.GetPaging(c.Ctx) + paging := params.GetPaging(ctx) - if keyword, _ := params.Get(c.Ctx, "keyword"); strs.IsNotBlank(keyword) { + if keyword, _ := params.Get(ctx, "keyword"); strs.IsNotBlank(keyword) { keywordLike := "%" + strings.TrimSpace(keyword) + "%" cnd.Where("customer_name LIKE ? OR last_message_summary LIKE ?", keywordLike, keywordLike) } // 标签搜索 - if tagID, _ := params.GetInt64(c.Ctx, "tagId"); tagID > 0 { + if tagID, _ := params.GetInt64(ctx, "tagId"); tagID > 0 { tagIDs := services.TagService.GetSelfAndDescendantIDs(tagID) if len(tagIDs) == 0 { - return web.JsonData(&web.PageResult{ + httpx.WriteJSON(ctx, &web.PageResult{ Results: []response.ConversationResponse{}, Page: paging, }) + return } cnd.Where("id IN (SELECT conversation_id FROM conversation_tag_rels WHERE tag_id IN (?))", tagIDs) } - if agentTeamID, _ := params.GetInt64(c.Ctx, "agentTeamId"); agentTeamID > 0 { + if agentTeamID, _ := params.GetInt64(ctx, "agentTeamId"); agentTeamID > 0 { userIDs := services.AgentProfileService.GetUserIDsByTeamID(agentTeamID) if len(userIDs) == 0 { - return web.JsonData(&web.PageResult{ + httpx.WriteJSON(ctx, &web.PageResult{ Results: []response.ConversationResponse{}, Page: paging, }) + return } cnd.In("current_assignee_id", userIDs) } @@ -66,18 +67,20 @@ func (c *ConversationController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildConversation(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *ConversationController) AnyConversations() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView) +func ConversationAnyConversations(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - filterValue, _ := params.Get(c.Ctx, "filter") - keyword, _ := params.Get(c.Ctx, "keyword") - paging := params.GetPaging(c.Ctx) + filterValue, _ := params.Get(ctx, "filter") + keyword, _ := params.Get(ctx, "keyword") + paging := params.GetPaging(ctx) list, paging, err := services.ConversationService.ListConversations( operator.UserID, @@ -86,47 +89,54 @@ func (c *ConversationController) AnyConversations() *web.JsonResult { paging, ) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } results := make([]response.ConversationResponse, 0, len(list)) for _, item := range list { results = append(results, builders.BuildConversation(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *ConversationController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView); err != nil { - return web.JsonError(err) +func ConversationGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.ConversationService.Get(id) if item == nil { - return web.JsonErrorMsg("会话不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在")) + return } detail := response.ConversationDetailResponse{ ConversationResponse: builders.BuildConversation(item), Participants: builders.BuildParticipantResponses(id), } - return web.JsonData(detail) + httpx.WriteJSON(ctx, detail) + return } -func (c *ConversationController) AnyMessage_list() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView); err != nil { - return web.JsonError(err) +func ConversationAnyMessage_list(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { + httpx.WriteJSON(ctx, err) + return } var ( - conversationID, _ = params.GetInt64(c.Ctx, "conversationId") - senderType, _ = params.Get(c.Ctx, "senderType") - messageType, _ = params.Get(c.Ctx, "messageType") - cursor, _ = params.GetInt64(c.Ctx, "cursor") - limit, _ = params.GetInt(c.Ctx, "limit") + conversationID, _ = params.GetInt64(ctx, "conversationId") + senderType, _ = params.Get(ctx, "senderType") + messageType, _ = params.Get(ctx, "messageType") + cursor, _ = params.GetInt64(ctx, "cursor") + limit, _ = params.GetInt(ctx, "limit") ) if conversation := services.ConversationService.Get(conversationID); conversation == nil { - return web.JsonErrorMsg("会话不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在")) + return } list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor( @@ -134,227 +144,283 @@ func (c *ConversationController) AnyMessage_list() *web.JsonResult { ) results := builders.BuildMessages(list) - return web.JsonCursorData(results, cast.ToString(nextCursor), hasMore) + httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore)) + return } -func (c *ConversationController) PostAssign() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationAssign) +func ConversationPostAssign(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationAssign) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AssignConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.AssignConversation(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostDispatch() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationAssign) +func ConversationPostDispatch(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationAssign) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DispatchConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.AutoAssignConversation(req.ConversationID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostTransfer() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationTransfer) +func ConversationPostTransfer(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationTransfer) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.TransferConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.TransferConversation(req.ConversationID, req.ToUserID, req.Reason, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostClose() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationClose) +func ConversationPostClose(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationClose) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CloseConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.CloseConversation(req.ConversationID, req.CloseReason, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostLink_customer() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationLinkCustomer) +func ConversationPostLink_customer(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationLinkCustomer) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.LinkConversationCustomerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.LinkConversationCustomer(req.ConversationID, req.CustomerID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostSend_message() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationSend) +func ConversationPostSend_message(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.SendConversationMessageRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.MessageService.SendAgentMessage(req.ConversationID, 0, req.ClientMsgID, req.MessageType, req.Content, req.Payload, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildMessage(item)) + httpx.WriteJSON(ctx, builders.BuildMessage(item)) + return } -func (c *ConversationController) PostRecall_message() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationSend) +func ConversationPostRecall_message(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.RecallConversationMessageRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.MessageService.RecallAgentMessage(req.MessageID, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildMessage(item)) + httpx.WriteJSON(ctx, builders.BuildMessage(item)) + return } -func (c *ConversationController) PostRead() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationView) +func ConversationPostRead(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.ReadConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationService.MarkAgentConversationReadToMessage(req.ConversationID, req.MessageID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostUpload_image() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationSend) +func ConversationPostUpload_image(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - rawConv := strings.TrimSpace(params.FormValue(c.Ctx, "conversationId")) + rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId")) if rawConv == "" { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversationID, err := strconv.ParseInt(rawConv, 10, 64) if err != nil || conversationID <= 0 { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeAgent, operator, nil); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - header, err := c.Ctx.FormFile("file") + header, err := ctx.FormFile("file") if err != nil { - return web.JsonErrorMsg("请选择上传图片") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片")) + return } if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") { - return web.JsonErrorMsg("仅支持上传图片文件") + httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件")) + return } item, err := services.AssetService.UploadFile(header, "images", operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } -func (c *ConversationController) PostUpload_attachment() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationSend) +func ConversationPostUpload_attachment(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationSend) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - rawConv := strings.TrimSpace(params.FormValue(c.Ctx, "conversationId")) + rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId")) if rawConv == "" { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } conversationID, err := strconv.ParseInt(rawConv, 10, 64) if err != nil || conversationID <= 0 { - return web.JsonErrorMsg("conversationId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空")) + return } if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeAgent, operator, nil); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - header, err := c.Ctx.FormFile("file") + header, err := ctx.FormFile("file") if err != nil { - return web.JsonErrorMsg("请选择上传附件") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件")) + return } item, err := services.AssetService.UploadFile(header, "attachments", operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildAsset(item)) + httpx.WriteJSON(ctx, builders.BuildAsset(item)) + return } -func (c *ConversationController) PostAdd_tag() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationTag) +func ConversationPostAdd_tag(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationTag) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AddConversationTagRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationTagService.AddTag(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *ConversationController) PostRemove_tag() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionConversationTag); err != nil { - return web.JsonError(err) +func ConversationPostRemove_tag(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationTag); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.RemoveConversationTagRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.ConversationTagService.RemoveTag(req); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/customer_contact_controller.go b/internal/controllers/dashboard/customer_contact_controller.go index 7ff8736..d1bbc3e 100644 --- a/internal/controllers/dashboard/customer_contact_controller.go +++ b/internal/controllers/dashboard/customer_contact_controller.go @@ -4,73 +4,86 @@ import ( "cs-agent/internal/builders" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type CustomerContactController struct { - Ctx *gin.Context -} - // AnyList GET/POST /customer-contact/list?customerId= -func (c *CustomerContactController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerView); err != nil { - return web.JsonError(err) +func CustomerContactAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil { + httpx.WriteJSON(ctx, err) + return } - customerID, _ := params.GetInt64(c.Ctx, "customerId") + customerID, _ := params.GetInt64(ctx, "customerId") if customerID <= 0 { - return web.JsonErrorMsg("customerId 必填") + httpx.WriteJSON(ctx, web.JsonErrorMsg("customerId 必填")) + return } list := services.CustomerContactService.FindActiveByCustomerID(customerID) - return web.JsonData(builders.BuildCustomerContactList(list)) + httpx.WriteJSON(ctx, builders.BuildCustomerContactList(list)) + return } -func (c *CustomerContactController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) +func CustomerContactPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateCustomerContactRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.CustomerContactService.CreateCustomerContact(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := builders.BuildCustomerContactResponse(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } -func (c *CustomerContactController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) +func CustomerContactPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateCustomerContactRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CustomerContactService.UpdateCustomerContact(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *CustomerContactController) PostDelete() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) +func CustomerContactPostDelete(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteCustomerContactRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CustomerContactService.DeleteCustomerContact(req.ID, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/customer_controller.go b/internal/controllers/dashboard/customer_controller.go index aaec6d9..41d8025 100644 --- a/internal/controllers/dashboard/customer_controller.go +++ b/internal/controllers/dashboard/customer_controller.go @@ -6,124 +6,148 @@ import ( "cs-agent/internal/pkg/dto" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type CustomerController struct { - Ctx *gin.Context -} - -func (c *CustomerController) PostList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerView); err != nil { - return web.JsonError(err) +func CustomerPostList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil { + httpx.WriteJSON(ctx, err) + return } var req request.CustomerListRequest - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } list, paging := services.CustomerService.ListCustomers(req) - return web.JsonData(&web.PageResult{Results: builders.BuildCustomerList(list), Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildCustomerList(list), Page: paging}) + return } -func (c *CustomerController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerView); err != nil { - return web.JsonError(err) +func CustomerGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.CustomerService.Get(id) if item == nil || item.Status == enums.StatusDeleted { - return web.JsonData(nil) + httpx.WriteJSON(ctx, nil) + return } ret := builders.BuildCustomer(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } // PostSave_profile POST /save_profile — 客户主信息与联系方式在同一事务中保存。 -func (c *CustomerController) PostSave_profile() *web.JsonResult { +func CustomerPostSave_profile(ctx *gin.Context) { req := request.SaveCustomerProfileRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } createMode := req.ID == nil || *req.ID <= 0 var user *dto.AuthPrincipal var err error if createMode { - user, err = services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerCreate) + user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate) } else { - user, err = services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) + user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) } if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } item, err := services.CustomerService.SaveCustomerProfile(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := builders.BuildCustomer(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } -func (c *CustomerController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerCreate) +func CustomerPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateCustomerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.CustomerService.CreateCustomer(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := builders.BuildCustomer(item) - return web.JsonData(&ret) + httpx.WriteJSON(ctx, &ret) + return } -func (c *CustomerController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) +func CustomerPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateCustomerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CustomerService.UpdateCustomer(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *CustomerController) PostDelete() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerDelete) +func CustomerPostDelete(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteCustomerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CustomerService.DeleteCustomer(req.ID, *user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *CustomerController) PostUpdate_status() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate) +func CustomerPostUpdate_status(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateCustomerStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.CustomerService.UpdateStatus(req.ID, req.Status, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/dashboard_controller.go b/internal/controllers/dashboard/dashboard_controller.go index a1c5239..45e472c 100644 --- a/internal/controllers/dashboard/dashboard_controller.go +++ b/internal/controllers/dashboard/dashboard_controller.go @@ -1,18 +1,16 @@ package dashboard import ( + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" - "github.com/mlogclub/simple/web" ) -type DashboardController struct { - Ctx *gin.Context -} - -func (c *DashboardController) GetOverview() *web.JsonResult { - rangeValue, _ := params.Get(c.Ctx, "range") - return web.JsonData(services.DashboardService.GetOverview(rangeValue)) +func DashboardGetOverview(ctx *gin.Context) { + rangeValue, _ := params.Get(ctx, "range") + httpx.WriteJSON(ctx, services.DashboardService.GetOverview(rangeValue)) + return } diff --git a/internal/controllers/dashboard/knowledge_base_controller.go b/internal/controllers/dashboard/knowledge_base_controller.go index 65cd7c9..90741f0 100644 --- a/internal/controllers/dashboard/knowledge_base_controller.go +++ b/internal/controllers/dashboard/knowledge_base_controller.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + "cs-agent/internal/pkg/httpx" "log/slog" "cs-agent/internal/ai/rag" @@ -13,21 +14,19 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type KnowledgeBaseController struct { - Ctx *gin.Context -} - -func (c *KnowledgeBaseController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseView); err != nil { - return web.JsonError(err) +func KnowledgeBaseAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "name", Op: params.Like}, ).Asc("sort_no").Desc("id") @@ -41,15 +40,17 @@ func (c *KnowledgeBaseController) AnyList() *web.JsonResult { resp.FAQCount = faqCount results = append(results, resp) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *KnowledgeBaseController) AnyList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseView); err != nil { - return web.JsonError(err) +func KnowledgeBaseAnyList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list := services.KnowledgeBaseService.Find(params.NewSqlCnd(c.Ctx, + list := services.KnowledgeBaseService.Find(params.NewSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, ).Asc("sort_no").Desc("id")) results := make([]response.KnowledgeBaseResponse, 0, len(list)) @@ -57,100 +58,122 @@ func (c *KnowledgeBaseController) AnyList_all() *web.JsonResult { resp := builders.BuildKnowledgeBase(&item) results = append(results, resp) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *KnowledgeBaseController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseView); err != nil { - return web.JsonError(err) +func KnowledgeBaseGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.KnowledgeBaseService.Get(id) if item == nil { - return web.JsonErrorMsg("知识库不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("知识库不存在")) + return } resp := builders.BuildKnowledgeBase(item) resp.DocumentCount = repositories.KnowledgeDocumentRepository.CountByKnowledgeBaseID(sqls.DB(), item.ID) resp.FAQCount = repositories.KnowledgeFAQRepository.CountByKnowledgeBaseID(sqls.DB(), item.ID) - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } -func (c *KnowledgeBaseController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseCreate) +func KnowledgeBasePostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateKnowledgeBaseRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.KnowledgeBaseService.CreateKnowledgeBase(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildKnowledgeBase(item)) + httpx.WriteJSON(ctx, builders.BuildKnowledgeBase(item)) + return } -func (c *KnowledgeBaseController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseUpdate) +func KnowledgeBasePostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateKnowledgeBaseRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeBaseService.UpdateKnowledgeBase(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *KnowledgeBaseController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseDelete); err != nil { - return web.JsonError(err) +func KnowledgeBasePostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } var req struct { ID int64 `json:"id"` } - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeBaseService.DeleteKnowledgeBase(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *KnowledgeBaseController) PostUpdate_sort() *web.JsonResult { +func KnowledgeBasePostUpdate_sort(ctx *gin.Context) { var ids []int64 - if err := params.ReadJSON(c.Ctx, &ids); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &ids); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeBaseService.UpdateSort(ids); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *KnowledgeBaseController) PostRebuild_index() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeBaseUpdate); err != nil { - return web.JsonError(err) +func KnowledgeBasePostRebuild_index(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseUpdate); err != nil { + httpx.WriteJSON(ctx, err) + return } var req struct { ID int64 `json:"id"` } - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } knowledgeBase := services.KnowledgeBaseService.Get(req.ID) if knowledgeBase == nil { - return web.JsonErrorMsg("知识库不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("知识库不存在")) + return } go func() { @@ -160,5 +183,6 @@ func (c *KnowledgeBaseController) PostRebuild_index() *web.JsonResult { } }() - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/knowledge_document_controller.go b/internal/controllers/dashboard/knowledge_document_controller.go index f451fe2..96096ae 100644 --- a/internal/controllers/dashboard/knowledge_document_controller.go +++ b/internal/controllers/dashboard/knowledge_document_controller.go @@ -6,35 +6,35 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type KnowledgeDocumentController struct { - Ctx *gin.Context -} - -func (c *KnowledgeDocumentController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil { - return web.JsonError(err) +func KnowledgeDocumentAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "knowledgeBaseId"}, params.QueryFilter{ParamName: "title", Op: params.Like}, ).Desc("id") - if status, ok := params.GetInt64(c.Ctx, "status"); ok { + if status, ok := params.GetInt64(ctx, "status"); ok { cnd.Where("status = ?", status) } else { cnd.Where("status != ?", enums.StatusDeleted) } - if indexStatus, ok := params.Get(c.Ctx, "indexStatus"); ok { + if indexStatus, ok := params.Get(ctx, "indexStatus"); ok { if !enums.IsValidKnowledgeDocumentIndexStatus(indexStatus) { - return web.JsonErrorMsg("indexStatus参数不合法") + httpx.WriteJSON(ctx, web.JsonErrorMsg("indexStatus参数不合法")) + return } cnd.Where("index_status = ?", indexStatus) } @@ -44,68 +44,84 @@ func (c *KnowledgeDocumentController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildKnowledgeDocumentList(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *KnowledgeDocumentController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil { - return web.JsonError(err) +func KnowledgeDocumentGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.KnowledgeDocumentService.Get(id) if item == nil { - return web.JsonErrorMsg("文档不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("文档不存在")) + return } - return web.JsonData(builders.BuildKnowledgeDocument(item)) + httpx.WriteJSON(ctx, builders.BuildKnowledgeDocument(item)) + return } -func (c *KnowledgeDocumentController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentCreate) +func KnowledgeDocumentPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateKnowledgeDocumentRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.KnowledgeDocumentService.CreateKnowledgeDocument(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildKnowledgeDocument(item)) + httpx.WriteJSON(ctx, builders.BuildKnowledgeDocument(item)) + return } -func (c *KnowledgeDocumentController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentUpdate) +func KnowledgeDocumentPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateKnowledgeDocumentRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeDocumentService.UpdateKnowledgeDocument(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *KnowledgeDocumentController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentDelete); err != nil { - return web.JsonError(err) +func KnowledgeDocumentPostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } var req struct { ID int64 `json:"id"` } - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeDocumentService.DeleteKnowledgeDocument(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/knowledge_faq_controller.go b/internal/controllers/dashboard/knowledge_faq_controller.go index ef87249..a3c6071 100644 --- a/internal/controllers/dashboard/knowledge_faq_controller.go +++ b/internal/controllers/dashboard/knowledge_faq_controller.go @@ -5,23 +5,22 @@ import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type KnowledgeFAQController struct { - Ctx *gin.Context -} - -func (c *KnowledgeFAQController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQView); err != nil { - return web.JsonError(err) +func KnowledgeFAQAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "knowledgeBaseId"}, params.QueryFilter{ParamName: "question", Op: params.Like}, params.QueryFilter{ParamName: "indexStatus"}, @@ -31,64 +30,80 @@ func (c *KnowledgeFAQController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildKnowledgeFAQ(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *KnowledgeFAQController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQView); err != nil { - return web.JsonError(err) +func KnowledgeFAQGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.KnowledgeFAQService.Get(id) if item == nil { - return web.JsonErrorMsg("FAQ不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("FAQ不存在")) + return } - return web.JsonData(builders.BuildKnowledgeFAQ(item)) + httpx.WriteJSON(ctx, builders.BuildKnowledgeFAQ(item)) + return } -func (c *KnowledgeFAQController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQCreate) +func KnowledgeFAQPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateKnowledgeFAQRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.KnowledgeFAQService.CreateKnowledgeFAQ(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildKnowledgeFAQ(item)) + httpx.WriteJSON(ctx, builders.BuildKnowledgeFAQ(item)) + return } -func (c *KnowledgeFAQController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQUpdate) +func KnowledgeFAQPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateKnowledgeFAQRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeFAQService.UpdateKnowledgeFAQ(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *KnowledgeFAQController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQDelete); err != nil { - return web.JsonError(err) +func KnowledgeFAQPostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } var req struct { ID int64 `json:"id"` } - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.KnowledgeFAQService.DeleteKnowledgeFAQ(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/knowledge_retrieve_controller.go b/internal/controllers/dashboard/knowledge_retrieve_controller.go index d8f81df..ee0c6aa 100644 --- a/internal/controllers/dashboard/knowledge_retrieve_controller.go +++ b/internal/controllers/dashboard/knowledge_retrieve_controller.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/ai/rag" "cs-agent/internal/pkg/constants" @@ -9,77 +10,90 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type KnowledgeRetrieveController struct { - Ctx *gin.Context -} - -func (c *KnowledgeRetrieveController) PostDebugSearch() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil { - return web.JsonError(err) +func KnowledgeRetrievePostDebugSearch(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.KnowledgeSearchRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } resp, err := rag.Answer.DebugSearch(context.Background(), req) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } -func (c *KnowledgeRetrieveController) PostDebugAnswer() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView) +func KnowledgeRetrievePostDebugAnswer(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.KnowledgeAnswerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } resp, err := rag.Answer.DebugAnswer(context.Background(), req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } -func (c *KnowledgeRetrieveController) PostBuild() *web.JsonResult { +func KnowledgeRetrievePostBuild(ctx *gin.Context) { req := struct { DocumentID int64 `json:"documentId"` FAQID int64 `json:"faqId"` }{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if req.DocumentID > 0 { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentUpdate); err != nil { - return web.JsonError(err) + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentUpdate); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := rag.Answer.BuildDocumentIndex(context.Background(), req.DocumentID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } if req.FAQID > 0 { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeFAQUpdate); err != nil { - return web.JsonError(err) + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQUpdate); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := rag.Index.IndexFAQByID(context.Background(), req.FAQID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } - return web.JsonErrorMsg("documentId或faqId不能为空") + httpx.WriteJSON(ctx, web.JsonErrorMsg("documentId或faqId不能为空")) + return } diff --git a/internal/controllers/dashboard/knowledge_retrieve_log_controller.go b/internal/controllers/dashboard/knowledge_retrieve_log_controller.go index 2cf5a98..ba2343a 100644 --- a/internal/controllers/dashboard/knowledge_retrieve_log_controller.go +++ b/internal/controllers/dashboard/knowledge_retrieve_log_controller.go @@ -4,23 +4,22 @@ import ( "cs-agent/internal/builders" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type KnowledgeRetrieveLogController struct { - Ctx *gin.Context -} - -func (c *KnowledgeRetrieveLogController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil { - return web.JsonError(err) +func KnowledgeRetrieveLogAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "knowledgeBaseId"}, params.QueryFilter{ParamName: "question", Op: params.Like}, params.QueryFilter{ParamName: "channel"}, @@ -28,14 +27,14 @@ func (c *KnowledgeRetrieveLogController) AnyList() *web.JsonResult { params.QueryFilter{ParamName: "chunkProvider"}, ).Desc("id") - if answerStatus, ok := params.GetInt64(c.Ctx, "answerStatus"); ok && answerStatus > 0 { + if answerStatus, ok := params.GetInt64(ctx, "answerStatus"); ok && answerStatus > 0 { cnd.Where("answer_status = ?", answerStatus) } - if rerankEnabled, ok := params.GetInt64(c.Ctx, "rerankEnabled"); ok { + if rerankEnabled, ok := params.GetInt64(ctx, "rerankEnabled"); ok { cnd.Where("rerank_enabled = ?", rerankEnabled > 0) } - queryParams := params.NewQueryParams(c.Ctx) + queryParams := params.NewQueryParams(ctx) queryParams.Cnd = *cnd list, paging := services.KnowledgeRetrieveLogService.FindPageByParams(queryParams) results := make([]response.KnowledgeRetrieveLogResponse, 0, len(list)) @@ -46,17 +45,20 @@ func (c *KnowledgeRetrieveLogController) AnyList() *web.JsonResult { } results = append(results, resp) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *KnowledgeRetrieveLogController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionKnowledgeDocumentView); err != nil { - return web.JsonError(err) +func KnowledgeRetrieveLogGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { + httpx.WriteJSON(ctx, err) + return } logItem := services.KnowledgeRetrieveLogService.Get(id) if logItem == nil { - return web.JsonErrorMsg("检索日志不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("检索日志不存在")) + return } logResp := builders.BuildKnowledgeRetrieveLog(logItem) @@ -70,8 +72,9 @@ func (c *KnowledgeRetrieveLogController) GetBy(id int64) *web.JsonResult { hitResults = append(hitResults, builders.BuildKnowledgeRetrieveHitResponse(&item)) } - return web.JsonData(response.KnowledgeRetrieveLogDetailResponse{ + httpx.WriteJSON(ctx, response.KnowledgeRetrieveLogDetailResponse{ Log: logResp, Hits: hitResults, }) + return } diff --git a/internal/controllers/dashboard/mcp_controller.go b/internal/controllers/dashboard/mcp_controller.go index 92e24e3..2b59479 100644 --- a/internal/controllers/dashboard/mcp_controller.go +++ b/internal/controllers/dashboard/mcp_controller.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" @@ -9,28 +10,28 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" - "github.com/mlogclub/simple/web" ) -type MCPController struct { - Ctx *gin.Context -} - -func (c *MCPController) AnyList_servers() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionMCPView); err != nil { - return web.JsonError(err) +func MCPAnyList_servers(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionMCPView); err != nil { + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(response.BuildMCPServerInfoResponses(services.MCPDebugService.ListServers())) + httpx.WriteJSON(ctx, response.BuildMCPServerInfoResponses(services.MCPDebugService.ListServers())) + return } -func (c *MCPController) AnyCatalog() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionMCPView); err != nil { - return web.JsonError(err) +func MCPAnyCatalog(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionMCPView); err != nil { + httpx.WriteJSON(ctx, err) + return } items, err := services.ToolCatalogService.ListMCPTools(context.Background()) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } ret := make([]response.MCPToolCatalogResponse, 0, len(items)) for _, item := range items { @@ -46,50 +47,63 @@ func (c *MCPController) AnyCatalog() *web.JsonResult { OutputSchema: item.OutputSchema, }) } - return web.JsonData(ret) + httpx.WriteJSON(ctx, ret) + return } -func (c *MCPController) PostTest_connection() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionMCPView); err != nil { - return web.JsonError(err) +func MCPPostTest_connection(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionMCPView); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.MCPServerDebugRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } result, err := services.MCPDebugService.TestConnection(context.Background(), req.ServerCode) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(response.BuildMCPConnectionResponse(result)) + httpx.WriteJSON(ctx, response.BuildMCPConnectionResponse(result)) + return } -func (c *MCPController) PostList_tools() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionMCPView); err != nil { - return web.JsonError(err) +func MCPPostList_tools(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionMCPView); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.MCPServerDebugRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } result, err := services.MCPDebugService.ListTools(context.Background(), req.ServerCode) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(response.BuildMCPToolInfoResponses(result)) + httpx.WriteJSON(ctx, response.BuildMCPToolInfoResponses(result)) + return } -func (c *MCPController) PostCall_tool() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionMCPCall); err != nil { - return web.JsonError(err) +func MCPPostCall_tool(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionMCPCall); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.MCPCallToolRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } result, err := services.MCPDebugService.CallTool(context.Background(), req.ServerCode, req.ToolName, req.Arguments) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(response.BuildMCPCallToolResponse(result)) + httpx.WriteJSON(ctx, response.BuildMCPCallToolResponse(result)) + return } diff --git a/internal/controllers/dashboard/notification_controller.go b/internal/controllers/dashboard/notification_controller.go index f8105a9..9d5372d 100644 --- a/internal/controllers/dashboard/notification_controller.go +++ b/internal/controllers/dashboard/notification_controller.go @@ -1,6 +1,7 @@ package dashboard import ( + "cs-agent/internal/pkg/httpx" "strings" "cs-agent/internal/builders" @@ -11,27 +12,25 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type NotificationController struct { - Ctx *gin.Context -} - -func (c *NotificationController) AnyList() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionNotificationView) +func NotificationAnyList(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionNotificationView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "type", ColumnName: "notification_type"}, ).Eq("recipient_user_id", operator.UserID). Eq("status", enums.StatusOk). Desc("id") - switch strings.TrimSpace(c.Ctx.Query("readStatus")) { + switch strings.TrimSpace(ctx.Query("readStatus")) { case "unread": cnd.Where("read_at IS NULL") case "read": @@ -39,44 +38,54 @@ func (c *NotificationController) AnyList() *web.JsonResult { } list, paging := services.NotificationService.FindPageByCnd(cnd) - return web.JsonData(&web.PageResult{ + httpx.WriteJSON(ctx, &web.PageResult{ Results: builders.BuildNotificationList(list), Page: paging, }) + return } -func (c *NotificationController) GetUnread_count() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionNotificationView) +func NotificationGetUnread_count(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionNotificationView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(&response.NotificationUnreadCountResponse{ + httpx.WriteJSON(ctx, &response.NotificationUnreadCountResponse{ UnreadCount: services.NotificationService.CountUnread(operator.UserID), }) + return } -func (c *NotificationController) PostMark_read() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionNotificationUpdate) +func NotificationPostMark_read(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionNotificationUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.MarkNotificationReadRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.NotificationService.MarkRead(req.ID, operator.UserID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *NotificationController) PostMark_all_read() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionNotificationUpdate) +func NotificationPostMark_all_read(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionNotificationUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } if err := services.NotificationService.MarkAllRead(operator.UserID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/permission_controller.go b/internal/controllers/dashboard/permission_controller.go index 97d18ea..542183b 100644 --- a/internal/controllers/dashboard/permission_controller.go +++ b/internal/controllers/dashboard/permission_controller.go @@ -3,30 +3,29 @@ package dashboard import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/common/strs" "github.com/mlogclub/simple/web" ) -type PermissionController struct { - Ctx *gin.Context -} - -func (c *PermissionController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionPermissionView); err != nil { - return web.JsonError(err) +func PermissionAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionPermissionView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "groupName"}, params.QueryFilter{ParamName: "type"}, params.QueryFilter{ParamName: "status"}, ).Desc("id") - if keyword, _ := params.Get(c.Ctx, "keyword"); strs.IsNotBlank(keyword) { + if keyword, _ := params.Get(ctx, "keyword"); strs.IsNotBlank(keyword) { cnd.Where("(name LIKE ? OR code LIKE ?)", "%"+keyword+"%", "%"+keyword+"%") } @@ -45,19 +44,22 @@ func (c *PermissionController) AnyList() *web.JsonResult { SortNo: item.SortNo, }) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *PermissionController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionPermissionView); err != nil { - return web.JsonError(err) +func PermissionGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionPermissionView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.PermissionService.Get(id) if item == nil { - return web.JsonErrorMsg("权限不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("权限不存在")) + return } - return web.JsonData(&response.PermissionResponse{ + httpx.WriteJSON(ctx, &response.PermissionResponse{ ID: item.ID, Name: item.Name, Code: item.Code, @@ -68,4 +70,5 @@ func (c *PermissionController) GetBy(id int64) *web.JsonResult { Status: item.Status, SortNo: item.SortNo, }) + return } diff --git a/internal/controllers/dashboard/quick_reply_controller.go b/internal/controllers/dashboard/quick_reply_controller.go index 2ef1329..ffe2865 100644 --- a/internal/controllers/dashboard/quick_reply_controller.go +++ b/internal/controllers/dashboard/quick_reply_controller.go @@ -5,24 +5,23 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type QuickReplyController struct { - Ctx *gin.Context -} - -func (c *QuickReplyController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionQuickReplyView); err != nil { - return web.JsonError(err) +func QuickReplyAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionQuickReplyView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "groupName"}, params.QueryFilter{ParamName: "title", Op: params.Like}, @@ -40,12 +39,14 @@ func (c *QuickReplyController) AnyList() *web.JsonResult { SortNo: item.SortNo, }) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *QuickReplyController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionQuickReplyView); err != nil { - return web.JsonError(err) +func QuickReplyGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionQuickReplyView); err != nil { + httpx.WriteJSON(ctx, err) + return } list := services.QuickReplyService.Find(sqls.NewCnd().Eq("status", enums.StatusOk).Asc("sort_no").Desc("id")) results := make([]response.QuickReplyResponse, 0, len(list)) @@ -59,24 +60,28 @@ func (c *QuickReplyController) GetList_all() *web.JsonResult { SortNo: item.SortNo, }) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *QuickReplyController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionQuickReplyCreate) +func QuickReplyPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionQuickReplyCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateQuickReplyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.QuickReplyService.CreateQuickReply(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(&response.QuickReplyResponse{ + httpx.WriteJSON(ctx, &response.QuickReplyResponse{ ID: item.ID, GroupName: item.GroupName, Title: item.Title, @@ -84,35 +89,44 @@ func (c *QuickReplyController) PostCreate() *web.JsonResult { Status: item.Status, SortNo: item.SortNo, }) + return } -func (c *QuickReplyController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionQuickReplyUpdate) +func QuickReplyPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionQuickReplyUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateQuickReplyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.QuickReplyService.UpdateQuickReply(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *QuickReplyController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionQuickReplyDelete); err != nil { - return web.JsonError(err) +func QuickReplyPostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionQuickReplyDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.DeleteQuickReplyRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.QuickReplyService.DeleteQuickReply(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/role_controller.go b/internal/controllers/dashboard/role_controller.go index d34e388..746eaec 100644 --- a/internal/controllers/dashboard/role_controller.go +++ b/internal/controllers/dashboard/role_controller.go @@ -4,23 +4,22 @@ import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type RoleController struct { - Ctx *gin.Context -} - -func (c *RoleController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleView); err != nil { - return web.JsonError(err) +func RoleAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "code", Op: params.Like}, ).Asc("sort_no").Desc("id") @@ -36,12 +35,14 @@ func (c *RoleController) AnyList() *web.JsonResult { SortNo: item.SortNo, }) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *RoleController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleView); err != nil { - return web.JsonError(err) +func RoleGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleView); err != nil { + httpx.WriteJSON(ctx, err) + return } list := services.RoleService.Find(sqls.NewCnd().Asc("sort_no").Desc("id")) @@ -56,17 +57,20 @@ func (c *RoleController) GetList_all() *web.JsonResult { SortNo: item.SortNo, }) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *RoleController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleView); err != nil { - return web.JsonError(err) +func RoleGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.RoleService.Get(id) if item == nil { - return web.JsonErrorMsg("角色不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("角色不存在")) + return } permissionCodes := make([]string, 0) @@ -77,7 +81,7 @@ func (c *RoleController) GetBy(id int64) *web.JsonResult { permissionCodes = append(permissionCodes, permission.Code) } } - return web.JsonData(&response.RoleResponse{ + httpx.WriteJSON(ctx, &response.RoleResponse{ ID: item.ID, Name: item.Name, Code: item.Code, @@ -86,23 +90,27 @@ func (c *RoleController) GetBy(id int64) *web.JsonResult { SortNo: item.SortNo, Permissions: permissionCodes, }) + return } -func (c *RoleController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleCreate) +func RolePostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateRoleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } role, err := services.RoleService.CreateRole(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(&response.RoleResponse{ + httpx.WriteJSON(ctx, &response.RoleResponse{ ID: role.ID, Name: role.Name, Code: role.Code, @@ -110,78 +118,98 @@ func (c *RoleController) PostCreate() *web.JsonResult { IsSystem: role.IsSystem, SortNo: role.SortNo, }) + return } -func (c *RoleController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleUpdate) +func RolePostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateRoleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.RoleService.UpdateRole(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *RoleController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleDelete); err != nil { - return web.JsonError(err) +func RolePostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.DeleteRoleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.RoleService.DeleteRole(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *RoleController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleUpdate) +func RolePostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateRoleStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.RoleService.UpdateStatus(req.ID, req.Status, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *RoleController) PostAssign_permission() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionRoleAssignPermission) +func RolePostAssign_permission(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleAssignPermission) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AssignPermissionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.RoleService.AssignPermissions(req.RoleID, req.PermissionIDs, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *RoleController) PostUpdate_sort() *web.JsonResult { +func RolePostUpdate_sort(ctx *gin.Context) { var ids []int64 - if err := params.ReadJSON(c.Ctx, &ids); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &ids); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.RoleService.UpdateSort(ids); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/session_controller.go b/internal/controllers/dashboard/session_controller.go index 870ecd3..fde6226 100644 --- a/internal/controllers/dashboard/session_controller.go +++ b/internal/controllers/dashboard/session_controller.go @@ -4,25 +4,24 @@ import ( "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/pkg/utils" "cs-agent/internal/services" "time" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type SessionController struct { - Ctx *gin.Context -} - -func (c *SessionController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionView); err != nil { - return web.JsonError(err) +func SessionAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "userId"}, params.QueryFilter{ParamName: "clientType"}, ).Desc("id") @@ -45,37 +44,46 @@ func (c *SessionController) AnyList() *web.JsonResult { LastSeenAt: utils.FormatTimePtr(item.LastSeenAt), }) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *SessionController) PostRevoke() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke) +func SessionPostRevoke(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionRevoke) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.RevokeSessionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.LoginSessionService.Revoke(req.ID, user.UserID, user.Username); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *SessionController) PostRevokeByUser() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke) +func SessionPostRevokeByUser(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionRevoke) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.RevokeUserSessionsRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.LoginSessionService.RevokeByUser(req.UserID, user.UserID, user.Username); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/skill_definition_controller.go b/internal/controllers/dashboard/skill_definition_controller.go index 86a3281..70e6d25 100644 --- a/internal/controllers/dashboard/skill_definition_controller.go +++ b/internal/controllers/dashboard/skill_definition_controller.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + "cs-agent/internal/pkg/httpx" "strings" "time" @@ -13,25 +14,23 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type SkillDefinitionController struct { - Ctx *gin.Context -} - -func (c *SkillDefinitionController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil { - return web.JsonError(err) +func SkillDefinitionAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "name", Op: params.Like}, params.QueryFilter{ParamName: "code", Op: params.Like}, ).Desc("id") - if _, ok := params.Get(c.Ctx, "status"); !ok { + if _, ok := params.Get(ctx, "status"); !ok { cnd.Where("status <> ?", enums.StatusDeleted) } list, paging := services.SkillDefinitionService.FindPageByCnd(cnd) @@ -39,18 +38,20 @@ func (c *SkillDefinitionController) AnyList() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildSkillDefinitionResponse(&item)) } - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *SkillDefinitionController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil { - return web.JsonError(err) +func SkillDefinitionGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewSqlCnd(c.Ctx, + cnd := params.NewSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, ).Desc("id") - if status, ok := params.Get(c.Ctx, "status"); !ok || strings.TrimSpace(status) == "" { + if status, ok := params.Get(ctx, "status"); !ok || strings.TrimSpace(status) == "" { cnd.Where("status <> ?", enums.StatusDeleted) } list := services.SkillDefinitionService.Find(cnd) @@ -58,79 +59,98 @@ func (c *SkillDefinitionController) GetList_all() *web.JsonResult { for _, item := range list { results = append(results, builders.BuildSkillDefinitionResponse(&item)) } - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *SkillDefinitionController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil { - return web.JsonError(err) +func SkillDefinitionGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.SkillDefinitionService.Get(id) if item == nil { - return web.JsonErrorMsg("Skill 不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在")) + return } - return web.JsonData(builders.BuildSkillDefinitionResponse(item)) + httpx.WriteJSON(ctx, builders.BuildSkillDefinitionResponse(item)) + return } -func (c *SkillDefinitionController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionCreate) +func SkillDefinitionPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateSkillDefinitionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.SkillDefinitionService.CreateSkillDefinition(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildSkillDefinitionResponse(item)) + httpx.WriteJSON(ctx, builders.BuildSkillDefinitionResponse(item)) + return } -func (c *SkillDefinitionController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionUpdate) +func SkillDefinitionPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateSkillDefinitionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.SkillDefinitionService.UpdateSkillDefinition(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *SkillDefinitionController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionUpdate) +func SkillDefinitionPostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateSkillDefinitionStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if req.ID <= 0 { - return web.JsonErrorMsg("Skill ID 不合法") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法")) + return } if !enums.IsValidStatus(req.Status) { - return web.JsonErrorMsg("状态值不合法") + httpx.WriteJSON(ctx, web.JsonErrorMsg("状态值不合法")) + return } item := services.SkillDefinitionService.Get(req.ID) if item == nil { - return web.JsonErrorMsg("Skill 不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在")) + return } if item.Status == enums.StatusDeleted { - return web.JsonErrorMsg("已删除的 Skill 不能直接修改状态,请先恢复") + httpx.WriteJSON(ctx, web.JsonErrorMsg("已删除的 Skill 不能直接修改状态,请先恢复")) + return } if req.Status == int(enums.StatusDeleted) { - return web.JsonErrorMsg("请使用删除接口处理删除状态") + httpx.WriteJSON(ctx, web.JsonErrorMsg("请使用删除接口处理删除状态")) + return } if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{ @@ -139,26 +159,32 @@ func (c *SkillDefinitionController) PostUpdate_status() *web.JsonResult { "update_user_name": operator.Username, "updated_at": time.Now(), }); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *SkillDefinitionController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionDelete) +func SkillDefinitionPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteSkillDefinitionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if req.ID <= 0 { - return web.JsonErrorMsg("Skill ID 不合法") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法")) + return } if services.SkillDefinitionService.Get(req.ID) == nil { - return web.JsonErrorMsg("Skill 不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在")) + return } if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{ "status": enums.StatusDeleted, @@ -166,31 +192,38 @@ func (c *SkillDefinitionController) PostDelete() *web.JsonResult { "update_user_name": operator.Username, "updated_at": time.Now(), }); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *SkillDefinitionController) PostRestore() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionDelete) +func SkillDefinitionPostRestore(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.RestoreSkillDefinitionRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if req.ID <= 0 { - return web.JsonErrorMsg("Skill ID 不合法") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill ID 不合法")) + return } item := services.SkillDefinitionService.Get(req.ID) if item == nil { - return web.JsonErrorMsg("Skill 不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("Skill 不存在")) + return } if item.Status != enums.StatusDeleted { - return web.JsonErrorMsg("仅已删除的 Skill 支持恢复") + httpx.WriteJSON(ctx, web.JsonErrorMsg("仅已删除的 Skill 支持恢复")) + return } if err := services.SkillDefinitionService.Updates(req.ID, map[string]any{ @@ -199,39 +232,49 @@ func (c *SkillDefinitionController) PostRestore() *web.JsonResult { "update_user_name": operator.Username, "updated_at": time.Now(), }); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *SkillDefinitionController) PostDebug_run() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil { - return web.JsonError(err) +func SkillDefinitionPostDebug_run(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.SkillDebugRunRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } resp, err := services.SkillRuntimeService.DebugRun(context.Background(), req) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } -func (c *SkillDefinitionController) PostDebug_resume() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSkillDefinitionView); err != nil { - return web.JsonError(err) +func SkillDefinitionPostDebug_resume(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.SkillDebugResumeRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } resp, err := services.SkillRuntimeService.DebugResume(context.Background(), req) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(resp) + httpx.WriteJSON(ctx, resp) + return } diff --git a/internal/controllers/dashboard/tag_controller.go b/internal/controllers/dashboard/tag_controller.go index c97939d..29b1df8 100644 --- a/internal/controllers/dashboard/tag_controller.go +++ b/internal/controllers/dashboard/tag_controller.go @@ -4,124 +4,148 @@ import ( "cs-agent/internal/builders" "cs-agent/internal/pkg/constants" "cs-agent/internal/pkg/dto/request" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type TagController struct { - Ctx *gin.Context -} - -func (c *TagController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagView); err != nil { - return web.JsonError(err) +func TagAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagView); err != nil { + httpx.WriteJSON(ctx, err) + return } - list, paging := services.TagService.FindPageByCnd(params.NewPagedSqlCnd(c.Ctx, + list, paging := services.TagService.FindPageByCnd(params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "parentId"}, params.QueryFilter{ParamName: "name", Op: params.Like}, ).Asc("sort_no").Desc("id")) results := builders.BuildTagResponses(list) - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *TagController) GetList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagView); err != nil { - return web.JsonError(err) +func TagGetList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagView); err != nil { + httpx.WriteJSON(ctx, err) + return } list := services.TagService.FindAll() results := builders.BuildTagTreeResponses(list) - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *TagController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagView); err != nil { - return web.JsonError(err) +func TagGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.TagService.Get(id) if item == nil { - return web.JsonErrorMsg("标签不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("标签不存在")) + return } result := builders.BuildTagResponse(item) - return web.JsonData(&result) + httpx.WriteJSON(ctx, &result) + return } -func (c *TagController) PostCreate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagCreate) +func TagPostCreate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateTagRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.TagService.CreateTag(req, user) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } result := builders.BuildTagResponse(item) - return web.JsonData(&result) + httpx.WriteJSON(ctx, &result) + return } -func (c *TagController) PostUpdate() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagUpdate) +func TagPostUpdate(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateTagRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TagService.UpdateTag(req, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TagController) PostDelete() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagDelete); err != nil { - return web.JsonError(err) +func TagPostDelete(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagDelete); err != nil { + httpx.WriteJSON(ctx, err) + return } req := request.DeleteTagRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TagService.DeleteTag(req.ID); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TagController) PostUpdate_sort() *web.JsonResult { +func TagPostUpdate_sort(ctx *gin.Context) { var ids []int64 - if err := params.ReadJSON(c.Ctx, &ids); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &ids); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TagService.UpdateSort(ids); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TagController) PostUpdate_status() *web.JsonResult { - user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTagUpdate) +func TagPostUpdate_status(ctx *gin.Context) { + user, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateTagStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TagService.UpdateStatus(req.ID, req.Status, user); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/dashboard/ticket_controller.go b/internal/controllers/dashboard/ticket_controller.go index 8775a20..b7a82b5 100644 --- a/internal/controllers/dashboard/ticket_controller.go +++ b/internal/controllers/dashboard/ticket_controller.go @@ -1,6 +1,7 @@ package dashboard import ( + "cs-agent/internal/pkg/httpx" "strings" "cs-agent/internal/builders" @@ -9,21 +10,19 @@ import ( "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/sqls" "github.com/mlogclub/simple/web" ) -type TicketController struct { - Ctx *gin.Context -} - -func (c *TicketController) AnyList() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView) +func TicketAnyList(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "currentAssigneeId"}, params.QueryFilter{ParamName: "customerId"}, @@ -31,28 +30,29 @@ func (c *TicketController) AnyList() *web.JsonResult { params.QueryFilter{ParamName: "source"}, params.QueryFilter{ParamName: "channel"}, ).Desc("updated_at").Desc("id") - if keyword, _ := params.Get(c.Ctx, "keyword"); strings.TrimSpace(keyword) != "" { + if keyword, _ := params.Get(ctx, "keyword"); strings.TrimSpace(keyword) != "" { keyword = "%" + strings.TrimSpace(keyword) + "%" cnd.Where("ticket_no LIKE ? OR title LIKE ? OR description LIKE ?", keyword, keyword, keyword) } - if tagID, _ := params.GetInt64(c.Ctx, "tagId"); tagID > 0 { + if tagID, _ := params.GetInt64(ctx, "tagId"); tagID > 0 { cnd.Where("id IN (SELECT ticket_id FROM t_ticket_tag WHERE tag_id = ?)", tagID) } - if mine, _ := params.Get(c.Ctx, "mine"); mine == "1" || strings.EqualFold(mine, "true") { + if mine, _ := params.Get(ctx, "mine"); mine == "1" || strings.EqualFold(mine, "true") { cnd.Eq("current_assignee_id", operator.UserID) } - if unassigned, _ := params.Get(c.Ctx, "unassigned"); unassigned == "1" || strings.EqualFold(unassigned, "true") { + if unassigned, _ := params.Get(ctx, "unassigned"); unassigned == "1" || strings.EqualFold(unassigned, "true") { cnd.Eq("current_assignee_id", 0) } - if staleHoursValue, _ := params.Get(c.Ctx, "staleHours"); strings.TrimSpace(staleHoursValue) != "" { - staleHours, _ := params.GetInt(c.Ctx, "staleHours") + if staleHoursValue, _ := params.Get(ctx, "staleHours"); strings.TrimSpace(staleHoursValue) != "" { + staleHours, _ := params.GetInt(ctx, "staleHours") services.TicketService.ApplyStaleFilter(cnd, staleHours) } aggregate, err := services.TicketService.FindPageAggregateByCnd(cnd, operator.UserID) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(&web.PageResult{ + httpx.WriteJSON(ctx, &web.PageResult{ Results: builders.BuildTicketListWithContext(aggregate.List, &builders.TicketBuildContext{ TagsByTicketID: aggregate.TagsByTicketID, Users: aggregate.Users, @@ -60,190 +60,239 @@ func (c *TicketController) AnyList() *web.JsonResult { }), Page: aggregate.Paging, }) + return } -func (c *TicketController) AnySummary() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView) +func TicketAnySummary(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - staleHours, _ := params.GetInt(c.Ctx, "staleHours") - return web.JsonData(builders.BuildTicketSummary(services.TicketService.GetSummary(operator, staleHours))) + staleHours, _ := params.GetInt(ctx, "staleHours") + httpx.WriteJSON(ctx, builders.BuildTicketSummary(services.TicketService.GetSummary(operator, staleHours))) + return } -func (c *TicketController) AnyView_list() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView) +func TicketAnyView_list(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicketViewList(services.TicketViewService.ListByUser(operator.UserID))) + httpx.WriteJSON(ctx, builders.BuildTicketViewList(services.TicketViewService.ListByUser(operator.UserID))) + return } -func (c *TicketController) PostSave_view() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView) +func TicketPostSave_view(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.SaveTicketViewRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.TicketViewService.Save(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicketView(item)) + httpx.WriteJSON(ctx, builders.BuildTicketView(item)) + return } -func (c *TicketController) PostDelete_view() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView) +func TicketPostDelete_view(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteTicketViewRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TicketViewService.Delete(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TicketController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView); err != nil { - return web.JsonError(err) +func TicketGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView); err != nil { + httpx.WriteJSON(ctx, err) + return } detail, err := services.TicketService.GetDetail(id) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicketDetail(detail)) + httpx.WriteJSON(ctx, builders.BuildTicketDetail(detail)) + return } -func (c *TicketController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketCreate) +func TicketPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateTicketRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.TicketService.CreateTicket(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicket(item)) + httpx.WriteJSON(ctx, builders.BuildTicket(item)) + return } -func (c *TicketController) PostCreate_from_conversation() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketCreate) +func TicketPostCreate_from_conversation(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateTicketFromConversationRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.TicketService.CreateFromConversation(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicket(item)) + httpx.WriteJSON(ctx, builders.BuildTicket(item)) + return } -func (c *TicketController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketUpdate) +func TicketPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateTicketRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TicketService.UpdateTicket(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TicketController) PostLink_customer() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketUpdate) +func TicketPostLink_customer(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.LinkTicketCustomerRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TicketService.LinkCustomer(req.TicketID, req.CustomerID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TicketController) PostAssign() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketAssign) +func TicketPostAssign(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketAssign) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AssignTicketRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TicketService.AssignTicket(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TicketController) PostChange_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketChangeStatus) +func TicketPostChange_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketChangeStatus) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.ChangeTicketStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.TicketService.ChangeStatus(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *TicketController) AnyProgressList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketView); err != nil { - return web.JsonError(err) +func TicketAnyProgressList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView); err != nil { + httpx.WriteJSON(ctx, err) + return } - ticketID, _ := params.GetInt64(c.Ctx, "ticketId") + ticketID, _ := params.GetInt64(ctx, "ticketId") if ticketID <= 0 { - return web.JsonErrorMsg("工单不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("工单不存在")) + return } if services.TicketService.Get(ticketID) == nil { - return web.JsonErrorMsg("工单不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("工单不存在")) + return } progresses := services.TicketProgressService.Find(sqls.NewCnd().Eq("ticket_id", ticketID).Asc("id")) - return web.JsonData(builders.BuildTicketProgressList(progresses)) + httpx.WriteJSON(ctx, builders.BuildTicketProgressList(progresses)) + return } -func (c *TicketController) PostProgressCreate() *web.JsonResult { - return c.createProgress() +func TicketPostProgressCreate(ctx *gin.Context) { + ticketCreateProgress(ctx) + return } -func (c *TicketController) createProgress() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionTicketProgress) +func ticketCreateProgress(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketProgress) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateTicketProgressRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } item, err := services.TicketService.AddProgress(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(builders.BuildTicketProgress(item)) + httpx.WriteJSON(ctx, builders.BuildTicketProgress(item)) + return } diff --git a/internal/controllers/dashboard/user_controller.go b/internal/controllers/dashboard/user_controller.go index a4fc8e6..9efceec 100644 --- a/internal/controllers/dashboard/user_controller.go +++ b/internal/controllers/dashboard/user_controller.go @@ -6,23 +6,22 @@ import ( "cs-agent/internal/pkg/dto/request" "cs-agent/internal/pkg/dto/response" "cs-agent/internal/pkg/enums" + "cs-agent/internal/pkg/httpx" "cs-agent/internal/services" "cs-agent/internal/pkg/httpx/params" + "github.com/gin-gonic/gin" "github.com/mlogclub/simple/web" ) -type UserController struct { - Ctx *gin.Context -} - -func (c *UserController) AnyList() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil { - return web.JsonError(err) +func UserAnyList(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewPagedSqlCnd(c.Ctx, + cnd := params.NewPagedSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "username", Op: params.Like}, params.QueryFilter{ParamName: "nickname", Op: params.Like}, @@ -33,15 +32,17 @@ func (c *UserController) AnyList() *web.JsonResult { Roles: true, Permissions: false, }) - return web.JsonData(&web.PageResult{Results: results, Page: paging}) + httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) + return } -func (c *UserController) AnyList_all() *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil { - return web.JsonError(err) +func UserAnyList_all(ctx *gin.Context) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil { + httpx.WriteJSON(ctx, err) + return } - cnd := params.NewSqlCnd(c.Ctx, + cnd := params.NewSqlCnd(ctx, params.QueryFilter{ParamName: "status"}, params.QueryFilter{ParamName: "username", Op: params.Like}, params.QueryFilter{ParamName: "nickname", Op: params.Like}, @@ -53,147 +54,180 @@ func (c *UserController) AnyList_all() *web.JsonResult { Roles: true, Permissions: false, }) - return web.JsonData(results) + httpx.WriteJSON(ctx, results) + return } -func (c *UserController) GetBy(id int64) *web.JsonResult { - if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserView); err != nil { - return web.JsonError(err) +func UserGetBy(ctx *gin.Context, id int64) { + if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil { + httpx.WriteJSON(ctx, err) + return } item := services.UserService.Get(id) if item == nil { - return web.JsonErrorMsg("用户不存在") + httpx.WriteJSON(ctx, web.JsonErrorMsg("用户不存在")) + return } - return web.JsonData(builders.BuildUserResponse(item, builders.UserBuildOptions{ + httpx.WriteJSON(ctx, builders.BuildUserResponse(item, builders.UserBuildOptions{ Roles: true, Permissions: true, })) + return } -func (c *UserController) PostCreate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserCreate) +func UserPostCreate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserCreate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.CreateUserRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } user, generatedPassword, err := services.UserService.CreateUser(req, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(&response.CreateUserResultResponse{ + httpx.WriteJSON(ctx, &response.CreateUserResultResponse{ User: builders.BuildUserResponse(user, builders.UserBuildOptions{Roles: true, Permissions: true}), Password: generatedPassword, }) + return } -func (c *UserController) PostUpdate() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate) +func UserPostUpdate(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateUserRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.UserService.UpdateUser(req, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *UserController) PostDelete() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserDelete) +func UserPostDelete(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserDelete) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.DeleteUserRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.UserService.DeleteUser(req.ID, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *UserController) PostUpdate_status() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate) +func UserPostUpdate_status(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.UpdateUserStatusRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.UserService.UpdateStatus(req.ID, req.Status, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *UserController) PostReset_password() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserUpdate) +func UserPostReset_password(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } var req struct { UserID int64 `json:"userId"` } - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } password, err := services.UserService.ResetPassword(req.UserID, operator) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonData(map[string]any{ + httpx.WriteJSON(ctx, map[string]any{ "password": password, }) + return } -func (c *UserController) PostChange_password() *web.JsonResult { - principal := services.AuthService.GetAuthPrincipal(c.Ctx) +func UserPostChange_password(ctx *gin.Context) { + principal := services.AuthService.GetAuthPrincipal(ctx) if principal == nil { - if _, err := services.AuthService.Authenticate(c.Ctx); err != nil { - return web.JsonError(err) + if _, err := services.AuthService.Authenticate(ctx); err != nil { + httpx.WriteJSON(ctx, err) + return } - principal = services.AuthService.GetAuthPrincipal(c.Ctx) + principal = services.AuthService.GetAuthPrincipal(ctx) } if principal == nil { - return web.JsonErrorMsg("未登录或登录已过期") + httpx.WriteJSON(ctx, web.JsonErrorMsg("未登录或登录已过期")) + return } req := request.ChangePasswordRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.UserService.ChangeOwnPassword(req.Password, principal); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } -func (c *UserController) PostAssign_role() *web.JsonResult { - operator, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionUserAssignRole) +func UserPostAssign_role(ctx *gin.Context) { + operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserAssignRole) if err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } req := request.AssignRoleRequest{} - if err := params.ReadJSON(c.Ctx, &req); err != nil { - return web.JsonError(err) + if err := params.ReadJSON(ctx, &req); err != nil { + httpx.WriteJSON(ctx, err) + return } if err := services.UserService.AssignRoles(req.UserID, req.RoleIDs, operator); err != nil { - return web.JsonError(err) + httpx.WriteJSON(ctx, err) + return } - return web.JsonSuccess() + httpx.WriteJSON(ctx, nil) + return } diff --git a/internal/controllers/third/wechat_controller.go b/internal/controllers/third/wechat_controller.go index 53e906e..856d956 100644 --- a/internal/controllers/third/wechat_controller.go +++ b/internal/controllers/third/wechat_controller.go @@ -10,36 +10,32 @@ import ( "github.com/silenceper/wechat/v2/work/kf" ) -type WechatController struct { - Ctx *gin.Context -} - // GetCallback GET请求用于校验回调是否配置正确 -func (c *WechatController) GetCallback() { +func WechatGetCallback(ctx *gin.Context) { cli, err := wxwork.GetWorkCli().GetKF() if err != nil { - c.Ctx.AbortWithError(http.StatusInternalServerError, err) + ctx.AbortWithError(http.StatusInternalServerError, err) return } options := kf.SignatureOptions{} - if err := params.ReadForm(c.Ctx, &options); err != nil { - c.Ctx.AbortWithError(http.StatusUnauthorized, err) + if err := params.ReadForm(ctx, &options); err != nil { + ctx.AbortWithError(http.StatusUnauthorized, err) return } // 调用VerifyURL方法校验当前请求,如果合法则把解密后的内容作为响应返回给微信服务器 echo, err := cli.VerifyURL(options) if err == nil { - c.Ctx.String(http.StatusOK, echo) + ctx.String(http.StatusOK, echo) } else { - c.Ctx.AbortWithError(http.StatusUnauthorized, err) + ctx.AbortWithError(http.StatusUnauthorized, err) } } // PostCallback POST请求用于接收回调 -func (c *WechatController) PostCallback() { +func WechatPostCallback(ctx *gin.Context) { cli, err := wxwork.GetWorkCli().GetKF() if err != nil { - c.Ctx.AbortWithError(http.StatusInternalServerError, err) + ctx.AbortWithError(http.StatusInternalServerError, err) return } var ( @@ -47,22 +43,22 @@ func (c *WechatController) PostCallback() { body []byte ) // 读取原始消息内容 - body, err = io.ReadAll(c.Ctx.Request.Body) + body, err = io.ReadAll(ctx.Request.Body) if err != nil { - c.Ctx.AbortWithError(http.StatusInternalServerError, err) + ctx.AbortWithError(http.StatusInternalServerError, err) return } // 解析原始数据 message, err = cli.GetCallbackMessage(body) if err != nil { - c.Ctx.AbortWithError(http.StatusInternalServerError, err) + ctx.AbortWithError(http.StatusInternalServerError, err) return } if err := wxwork.ConsumeCallback(message); err != nil { - c.Ctx.AbortWithError(http.StatusInternalServerError, err) + ctx.AbortWithError(http.StatusInternalServerError, err) return } - c.Ctx.String(http.StatusOK, "ok") + ctx.String(http.StatusOK, "ok") }