Refactor controllers to use httpx for JSON responses and eliminate context dependency

- Updated TagController methods to use httpx.WriteJSON for consistent JSON response handling.
- Refactored TicketController to replace context-dependent methods with standalone functions using httpx.
- Modified UserController to utilize httpx for error handling and response formatting.
- Adjusted WechatController to remove context dependency and standardize response handling.
This commit is contained in:
mlogclub
2026-05-23 22:22:18 +08:00
parent 7fdf96dd9d
commit e9d2e41f9e
34 changed files with 2395 additions and 2313 deletions
@@ -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
}