diff --git a/internal/bootstrap/controller_routes.go b/internal/bootstrap/controller_routes.go index cf06e94..473735e 100644 --- a/internal/bootstrap/controller_routes.go +++ b/internal/bootstrap/controller_routes.go @@ -1,27 +1,13 @@ package bootstrap import ( - "net/http" - "strconv" - "cs-agent/internal/controllers/api" "cs-agent/internal/controllers/dashboard" "cs-agent/internal/controllers/third" - "cs-agent/internal/pkg/httpx" "github.com/gin-gonic/gin" - "github.com/mlogclub/simple/web" ) -func pathInt64(ctx *gin.Context, name string) (int64, bool) { - value, err := strconv.ParseInt(ctx.Param(name), 10, 64) - if err != nil { - httpx.WriteHttpStatusJSON(ctx, http.StatusBadRequest, web.JsonErrorMsg("路径参数错误")) - return 0, false - } - return value, true -} - func registerApiAuthRoutes(group *gin.RouterGroup) { group.POST("/login", api.Login) group.POST("/logout", api.Logout) @@ -41,13 +27,7 @@ func registerApiCustomerRoutes(group *gin.RouterGroup) { } func registerApiConversationRoutes(group *gin.RouterGroup) { - group.GET("/:id", func(ctx *gin.Context) { - id, ok := pathInt64(ctx, "id") - if !ok { - return - } - api.ConversationGetBy(ctx, id) - }) + group.GET("/:id", api.ConversationGetBy) group.POST("/close", api.ConversationPostClose) group.POST("/create_or_match", api.ConversationPostCreate_or_match) } @@ -65,13 +45,7 @@ func registerDashboardDashboardRoutes(group *gin.RouterGroup) { } func registerDashboardUserRoutes(group *gin.RouterGroup) { - group.GET("/:id", func(ctx *gin.Context) { - id, ok := pathInt64(ctx, "id") - if !ok { - return - } - dashboard.UserGetBy(ctx, id) - }) + group.GET("/:id", dashboard.UserGetBy) group.POST("/assign_role", dashboard.UserPostAssign_role) group.POST("/change_password", dashboard.UserPostChange_password) group.POST("/create", dashboard.UserPostCreate) @@ -84,13 +58,7 @@ func registerDashboardUserRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.CompanyGetBy) group.POST("/create", dashboard.CompanyPostCreate) group.POST("/delete", dashboard.CompanyPostDelete) group.Any("/list", dashboard.CompanyAnyList) @@ -99,13 +67,7 @@ func registerDashboardCompanyRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.CustomerGetBy) group.POST("/create", dashboard.CustomerPostCreate) group.POST("/delete", dashboard.CustomerPostDelete) group.POST("/list", dashboard.CustomerPostList) @@ -122,13 +84,7 @@ func registerDashboardCustomerContactRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.RoleGetBy) group.POST("/assign_permission", dashboard.RolePostAssign_permission) group.POST("/create", dashboard.RolePostCreate) group.POST("/delete", dashboard.RolePostDelete) @@ -140,13 +96,7 @@ func registerDashboardRoleRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.PermissionGetBy) group.Any("/list", dashboard.PermissionAnyList) } @@ -157,13 +107,7 @@ func registerDashboardSessionRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.TagGetBy) group.POST("/create", dashboard.TagPostCreate) group.POST("/delete", dashboard.TagPostDelete) group.Any("/list", dashboard.TagAnyList) @@ -174,13 +118,7 @@ func registerDashboardTagRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.ConversationGetBy) group.POST("/add_tag", dashboard.ConversationPostAdd_tag) group.POST("/assign", dashboard.ConversationPostAssign) group.POST("/close", dashboard.ConversationPostClose) @@ -199,13 +137,7 @@ func registerDashboardConversationRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.TicketGetBy) group.POST("/assign", dashboard.TicketPostAssign) group.POST("/change_status", dashboard.TicketPostChange_status) group.POST("/create", dashboard.TicketPostCreate) @@ -237,13 +169,7 @@ func registerDashboardQuickReplyRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.ChannelGetBy) group.POST("/create", dashboard.ChannelPostCreate) group.POST("/delete", dashboard.ChannelPostDelete) group.Any("/list", dashboard.ChannelAnyList) @@ -254,13 +180,7 @@ func registerDashboardChannelRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AgentGetBy) group.POST("/create", dashboard.AgentPostCreate) group.POST("/delete", dashboard.AgentPostDelete) group.Any("/list", dashboard.AgentAnyList) @@ -269,13 +189,7 @@ func registerDashboardAgentRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AgentTeamGetBy) group.POST("/create", dashboard.AgentTeamPostCreate) group.POST("/delete", dashboard.AgentTeamPostDelete) group.Any("/list", dashboard.AgentTeamAnyList) @@ -284,13 +198,7 @@ func registerDashboardAgentTeamRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AgentTeamScheduleGetBy) group.POST("/batch_generate", dashboard.AgentTeamSchedulePostBatch_generate) group.POST("/batch_preview", dashboard.AgentTeamSchedulePostBatch_preview) group.Any("/calendar", dashboard.AgentTeamScheduleAnyCalendar) @@ -301,13 +209,7 @@ func registerDashboardAgentTeamScheduleRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AIAgentGetBy) group.POST("/create", dashboard.AIAgentPostCreate) group.POST("/delete", dashboard.AIAgentPostDelete) group.Any("/list", dashboard.AIAgentAnyList) @@ -318,13 +220,7 @@ func registerDashboardAIAgentRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AIConfigGetBy) group.POST("/create", dashboard.AIConfigPostCreate) group.POST("/delete", dashboard.AIConfigPostDelete) group.Any("/list", dashboard.AIConfigAnyList) @@ -335,26 +231,14 @@ func registerDashboardAIConfigRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.AssetGetBy) 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.GET("/:id", dashboard.KnowledgeBaseGetBy) group.POST("/create", dashboard.KnowledgeBasePostCreate) group.POST("/delete", dashboard.KnowledgeBasePostDelete) group.Any("/list", dashboard.KnowledgeBaseAnyList) @@ -365,13 +249,7 @@ func registerDashboardKnowledgeBaseRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.KnowledgeDocumentGetBy) group.POST("/create", dashboard.KnowledgeDocumentPostCreate) group.POST("/delete", dashboard.KnowledgeDocumentPostDelete) group.Any("/list", dashboard.KnowledgeDocumentAnyList) @@ -379,13 +257,7 @@ func registerDashboardKnowledgeDocumentRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.KnowledgeFAQGetBy) group.POST("/create", dashboard.KnowledgeFAQPostCreate) group.POST("/delete", dashboard.KnowledgeFAQPostDelete) group.Any("/list", dashboard.KnowledgeFAQAnyList) @@ -399,35 +271,17 @@ func registerDashboardKnowledgeRetrieveRoutes(group *gin.RouterGroup) { } 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.GET("/:id", dashboard.KnowledgeRetrieveLogGetBy) 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.GET("/:id", dashboard.AgentRunLogGetBy) 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.GET("/:id", dashboard.SkillDefinitionGetBy) group.POST("/create", dashboard.SkillDefinitionPostCreate) group.POST("/debug_resume", dashboard.SkillDefinitionPostDebug_resume) group.POST("/debug_run", dashboard.SkillDefinitionPostDebug_run) diff --git a/internal/controllers/api/auth_controller.go b/internal/controllers/api/auth_controller.go index ffa3f12..c53576d 100644 --- a/internal/controllers/api/auth_controller.go +++ b/internal/controllers/api/auth_controller.go @@ -32,7 +32,7 @@ func Login(ctx *gin.Context) { func WxWorkLogin(ctx *gin.Context) { loginURL, err := services.WxWorkLoginService.BuildWxWorkLoginURL(ctx.Query("next")) if err != nil { - redirectWxWorkError(ctx, err.Error()) + ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error()))) return } ctx.Redirect(http.StatusFound, loginURL) @@ -41,7 +41,7 @@ func WxWorkLogin(ctx *gin.Context) { func WxWorkQRLogin(ctx *gin.Context) { loginURL, err := services.WxWorkLoginService.BuildWxWorkQRCodeLoginURL(ctx.Query("next")) if err != nil { - redirectWxWorkError(ctx, err.Error()) + ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error()))) return } ctx.Redirect(http.StatusFound, loginURL) @@ -57,7 +57,7 @@ func WxWorkCallback(ctx *gin.Context) { ctx.GetHeader("User-Agent"), ) if err != nil { - redirectWxWorkError(ctx, err.Error()) + ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error()))) return } ctx.Redirect(http.StatusFound, "/dashboard/login/wxwork/callback?ticket="+url.QueryEscape(ticket)+"&next="+url.QueryEscape(next)) @@ -94,9 +94,9 @@ func Profile(ctx *gin.Context) { httpx.WriteJSON(ctx, ret) } -func redirectWxWorkError(ctx *gin.Context, message string) { +func wxWorkErrorMessage(message string) string { if idx := strings.Index(message, ": "); idx >= 0 { message = message[idx+2:] } - ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(message)) + return message } diff --git a/internal/controllers/api/conversation_controller.go b/internal/controllers/api/conversation_controller.go index af95619..6b4e81d 100644 --- a/internal/controllers/api/conversation_controller.go +++ b/internal/controllers/api/conversation_controller.go @@ -13,7 +13,11 @@ import ( "github.com/mlogclub/simple/web" ) -func ConversationGetBy(ctx *gin.Context, id int64) { +func ConversationGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if services.ChannelService.GetEnabledChannel(ctx) == nil { httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化")) return diff --git a/internal/controllers/dashboard/agent_controller.go b/internal/controllers/dashboard/agent_controller.go index d390acb..df20a66 100644 --- a/internal/controllers/dashboard/agent_controller.go +++ b/internal/controllers/dashboard/agent_controller.go @@ -44,7 +44,11 @@ func AgentGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, builders.BuildAgentProfileList(list)) } -func AgentGetBy(ctx *gin.Context, id int64) { +func AgentGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/agent_run_log_controller.go b/internal/controllers/dashboard/agent_run_log_controller.go index ebb7ba4..80c130d 100644 --- a/internal/controllers/dashboard/agent_run_log_controller.go +++ b/internal/controllers/dashboard/agent_run_log_controller.go @@ -46,7 +46,11 @@ func AgentRunLogAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func AgentRunLogGetBy(ctx *gin.Context, id int64) { +func AgentRunLogGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/agent_team_controller.go b/internal/controllers/dashboard/agent_team_controller.go index 10d1669..8ed6eba 100644 --- a/internal/controllers/dashboard/agent_team_controller.go +++ b/internal/controllers/dashboard/agent_team_controller.go @@ -50,7 +50,11 @@ func AgentTeamGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func AgentTeamGetBy(ctx *gin.Context, id int64) { +func AgentTeamGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/agent_team_schedule_controller.go b/internal/controllers/dashboard/agent_team_schedule_controller.go index 6c5cc9f..9e05664 100644 --- a/internal/controllers/dashboard/agent_team_schedule_controller.go +++ b/internal/controllers/dashboard/agent_team_schedule_controller.go @@ -93,7 +93,11 @@ func AgentTeamSchedulePostBatch_generate(ctx *gin.Context) { httpx.WriteJSON(ctx, builders.BuildAgentTeamScheduleBatchGenerateResponse(ret)) } -func AgentTeamScheduleGetBy(ctx *gin.Context, id int64) { +func AgentTeamScheduleGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAgentTeamScheduleView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/ai_agent_controller.go b/internal/controllers/dashboard/ai_agent_controller.go index ae93328..7666c31 100644 --- a/internal/controllers/dashboard/ai_agent_controller.go +++ b/internal/controllers/dashboard/ai_agent_controller.go @@ -52,7 +52,11 @@ func AIAgentGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func AIAgentGetBy(ctx *gin.Context, id int64) { +func AIAgentGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/ai_config_controller.go b/internal/controllers/dashboard/ai_config_controller.go index d0dea70..d3ef8a1 100644 --- a/internal/controllers/dashboard/ai_config_controller.go +++ b/internal/controllers/dashboard/ai_config_controller.go @@ -50,7 +50,11 @@ func AIConfigAnyList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func AIConfigGetBy(ctx *gin.Context, id int64) { +func AIConfigGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIConfigView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/asset_controller.go b/internal/controllers/dashboard/asset_controller.go index 40c499b..8d733df 100644 --- a/internal/controllers/dashboard/asset_controller.go +++ b/internal/controllers/dashboard/asset_controller.go @@ -40,7 +40,11 @@ func AssetAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func AssetGetBy(ctx *gin.Context, id int64) { +func AssetGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAssetView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/channel_controller.go b/internal/controllers/dashboard/channel_controller.go index 5007f07..9c37f65 100644 --- a/internal/controllers/dashboard/channel_controller.go +++ b/internal/controllers/dashboard/channel_controller.go @@ -33,7 +33,11 @@ func ChannelAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func ChannelGetBy(ctx *gin.Context, id int64) { +func ChannelGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionChannelView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/company_controller.go b/internal/controllers/dashboard/company_controller.go index 71c5ff0..157d3c4 100644 --- a/internal/controllers/dashboard/company_controller.go +++ b/internal/controllers/dashboard/company_controller.go @@ -37,7 +37,11 @@ func CompanyAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func CompanyGetBy(ctx *gin.Context, id int64) { +func CompanyGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCompanyView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/conversation_controller.go b/internal/controllers/dashboard/conversation_controller.go index 0275dee..c3e286d 100644 --- a/internal/controllers/dashboard/conversation_controller.go +++ b/internal/controllers/dashboard/conversation_controller.go @@ -99,7 +99,11 @@ func ConversationAnyConversations(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func ConversationGetBy(ctx *gin.Context, id int64) { +func ConversationGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionConversationView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/customer_controller.go b/internal/controllers/dashboard/customer_controller.go index feb49a1..70aa9e6 100644 --- a/internal/controllers/dashboard/customer_controller.go +++ b/internal/controllers/dashboard/customer_controller.go @@ -29,7 +29,11 @@ func CustomerPostList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildCustomerList(list), Page: paging}) } -func CustomerGetBy(ctx *gin.Context, id int64) { +func CustomerGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/knowledge_base_controller.go b/internal/controllers/dashboard/knowledge_base_controller.go index e44ec0d..42cc7e7 100644 --- a/internal/controllers/dashboard/knowledge_base_controller.go +++ b/internal/controllers/dashboard/knowledge_base_controller.go @@ -60,7 +60,11 @@ func KnowledgeBaseAnyList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func KnowledgeBaseGetBy(ctx *gin.Context, id int64) { +func KnowledgeBaseGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeBaseView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/knowledge_document_controller.go b/internal/controllers/dashboard/knowledge_document_controller.go index f37eb65..a01f165 100644 --- a/internal/controllers/dashboard/knowledge_document_controller.go +++ b/internal/controllers/dashboard/knowledge_document_controller.go @@ -47,7 +47,11 @@ func KnowledgeDocumentAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func KnowledgeDocumentGetBy(ctx *gin.Context, id int64) { +func KnowledgeDocumentGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/knowledge_faq_controller.go b/internal/controllers/dashboard/knowledge_faq_controller.go index 0762be0..622834e 100644 --- a/internal/controllers/dashboard/knowledge_faq_controller.go +++ b/internal/controllers/dashboard/knowledge_faq_controller.go @@ -33,7 +33,11 @@ func KnowledgeFAQAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func KnowledgeFAQGetBy(ctx *gin.Context, id int64) { +func KnowledgeFAQGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeFAQView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/knowledge_retrieve_log_controller.go b/internal/controllers/dashboard/knowledge_retrieve_log_controller.go index f13c08d..9fd9b9f 100644 --- a/internal/controllers/dashboard/knowledge_retrieve_log_controller.go +++ b/internal/controllers/dashboard/knowledge_retrieve_log_controller.go @@ -48,7 +48,11 @@ func KnowledgeRetrieveLogAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func KnowledgeRetrieveLogGetBy(ctx *gin.Context, id int64) { +func KnowledgeRetrieveLogGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionKnowledgeDocumentView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/permission_controller.go b/internal/controllers/dashboard/permission_controller.go index 8ec06d7..70e4629 100644 --- a/internal/controllers/dashboard/permission_controller.go +++ b/internal/controllers/dashboard/permission_controller.go @@ -47,7 +47,11 @@ func PermissionAnyList(ctx *gin.Context) { httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging}) } -func PermissionGetBy(ctx *gin.Context, id int64) { +func PermissionGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionPermissionView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/role_controller.go b/internal/controllers/dashboard/role_controller.go index 5e556d9..35f0a5a 100644 --- a/internal/controllers/dashboard/role_controller.go +++ b/internal/controllers/dashboard/role_controller.go @@ -59,7 +59,11 @@ func RoleGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func RoleGetBy(ctx *gin.Context, id int64) { +func RoleGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionRoleView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/skill_definition_controller.go b/internal/controllers/dashboard/skill_definition_controller.go index e0900e7..d0dd1f4 100644 --- a/internal/controllers/dashboard/skill_definition_controller.go +++ b/internal/controllers/dashboard/skill_definition_controller.go @@ -61,7 +61,11 @@ func SkillDefinitionGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func SkillDefinitionGetBy(ctx *gin.Context, id int64) { +func SkillDefinitionGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSkillDefinitionView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/tag_controller.go b/internal/controllers/dashboard/tag_controller.go index 440a1f9..b692ee5 100644 --- a/internal/controllers/dashboard/tag_controller.go +++ b/internal/controllers/dashboard/tag_controller.go @@ -37,7 +37,11 @@ func TagGetList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func TagGetBy(ctx *gin.Context, id int64) { +func TagGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTagView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/ticket_controller.go b/internal/controllers/dashboard/ticket_controller.go index fa423c8..85f1cb1 100644 --- a/internal/controllers/dashboard/ticket_controller.go +++ b/internal/controllers/dashboard/ticket_controller.go @@ -118,7 +118,11 @@ func TicketPostDelete_view(ctx *gin.Context) { httpx.WriteJSON(ctx, nil) } -func TicketGetBy(ctx *gin.Context, id int64) { +func TicketGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionTicketView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/controllers/dashboard/user_controller.go b/internal/controllers/dashboard/user_controller.go index 7f8f4ca..724ed9c 100644 --- a/internal/controllers/dashboard/user_controller.go +++ b/internal/controllers/dashboard/user_controller.go @@ -56,7 +56,11 @@ func UserAnyList_all(ctx *gin.Context) { httpx.WriteJSON(ctx, results) } -func UserGetBy(ctx *gin.Context, id int64) { +func UserGetBy(ctx *gin.Context) { + id, ok := httpx.GetPathInt64(ctx, "id") + if !ok { + return + } if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil { httpx.WriteJSON(ctx, err) return diff --git a/internal/pkg/httpx/path.go b/internal/pkg/httpx/path.go new file mode 100644 index 0000000..1716140 --- /dev/null +++ b/internal/pkg/httpx/path.go @@ -0,0 +1,18 @@ +package httpx + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/mlogclub/simple/web" +) + +func GetPathInt64(ctx *gin.Context, name string) (int64, bool) { + value, err := strconv.ParseInt(ctx.Param(name), 10, 64) + if err != nil { + WriteHttpStatusJSON(ctx, http.StatusBadRequest, web.JsonErrorMsg("路径参数错误")) + return 0, false + } + return value, true +}