refactor: simplify route handlers by removing redundant ID parsing logic

This commit is contained in:
mlogclub
2026-05-23 22:28:21 +08:00
parent 1852f9df8b
commit f9ddf39c83
25 changed files with 155 additions and 195 deletions
+22 -168
View File
@@ -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)
+5 -5
View File
@@ -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
}
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
+18
View File
@@ -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
}