Refactor API routes and controllers for improved structure and clarity

- Updated API route structure to remove versioning and consolidate endpoints.
- Refactored routing in `server.go` to use a unified `/api` prefix for all controllers.
- Added new `ChannelController`, `ConversationController`, and `MessageController` to handle specific API functionalities.
- Removed deprecated `OpenImContextMiddleware` and related logic.
- Enhanced channel validation logic in `ChannelService` and `ChannelRepository`.
- Updated frontend API calls to align with new endpoint structure.
This commit is contained in:
mlogclub
2026-04-25 10:27:43 +08:00
parent c1ab26ed71
commit 96168dc6fb
13 changed files with 98 additions and 132 deletions
@@ -0,0 +1,77 @@
package api
import (
"cs-agent/internal/builders"
"cs-agent/internal/pkg/dto/request"
"cs-agent/internal/pkg/dto/response"
"cs-agent/internal/pkg/irisx"
"cs-agent/internal/services"
"github.com/kataras/iris/v12"
"github.com/mlogclub/simple/web"
"github.com/mlogclub/simple/web/params"
)
type ConversationController struct {
Ctx iris.Context
}
func (c *ConversationController) GetBy(id int64) *web.JsonResult {
if services.ChannelService.GetEnabledChannel(c.Ctx) == nil {
return web.JsonErrorMsg("接入渠道未初始化")
}
external := irisx.GetExternalInfo(c.Ctx)
if external == nil {
return web.JsonErrorMsg("外部身份未初始化")
}
item := services.ConversationService.Get(id)
if item == nil {
return web.JsonErrorMsg("会话不存在")
}
if !services.ConversationService.IsCustomerConversationOwner(item, *external) {
return web.JsonErrorMsg("无权访问该会话")
}
detail := response.ConversationDetailResponse{
ConversationResponse: builders.BuildConversation(item),
Participants: builders.BuildParticipantResponses(id),
}
return web.JsonData(detail)
}
func (c *ConversationController) PostCreate_or_match() *web.JsonResult {
channel := services.ChannelService.GetEnabledChannel(c.Ctx)
if channel == nil {
return web.JsonErrorMsg("接入渠道未初始化")
}
external := irisx.GetExternalInfo(c.Ctx)
if external == nil {
return web.JsonErrorMsg("外部身份未初始化")
}
item, err := services.ConversationService.Create(*external, channel.AIAgentID)
if err != nil {
return web.JsonError(err)
}
return web.JsonData(builders.BuildConversation(item))
}
func (c *ConversationController) PostClose() *web.JsonResult {
if services.ChannelService.GetEnabledChannel(c.Ctx) == nil {
return web.JsonErrorMsg("接入渠道未初始化")
}
external := irisx.GetExternalInfo(c.Ctx)
if external == nil {
return web.JsonErrorMsg("外部身份未初始化")
}
req := request.CloseConversationRequest{}
if err := params.ReadJSON(c.Ctx, &req); err != nil {
return web.JsonError(err)
}
if err := services.ConversationService.CloseCustomerConversation(req.ConversationID, *external); err != nil {
return web.JsonError(err)
}
return web.JsonSuccess()
}