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:
@@ -0,0 +1,173 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"cs-agent/internal/builders"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
"cs-agent/internal/pkg/irisx"
|
||||
"cs-agent/internal/services"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/mlogclub/simple/web"
|
||||
"github.com/mlogclub/simple/web/params"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
type MessageController struct {
|
||||
Ctx iris.Context
|
||||
}
|
||||
|
||||
func (c *MessageController) AnyList() *web.JsonResult {
|
||||
if services.ChannelService.GetEnabledChannel(c.Ctx) == nil {
|
||||
return web.JsonErrorMsg("接入渠道未初始化")
|
||||
}
|
||||
external := irisx.GetExternalInfo(c.Ctx)
|
||||
if external == nil {
|
||||
return web.JsonErrorMsg("外部身份未初始化")
|
||||
}
|
||||
|
||||
conversationID, _ := params.GetInt64(c.Ctx, "conversationId")
|
||||
if conversationID <= 0 {
|
||||
return web.JsonErrorMsg("conversationId不能为空")
|
||||
}
|
||||
conversation := services.ConversationService.Get(conversationID)
|
||||
if conversation == nil {
|
||||
return web.JsonErrorMsg("会话不存在")
|
||||
}
|
||||
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
||||
return web.JsonErrorMsg("无权访问该会话")
|
||||
}
|
||||
|
||||
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")
|
||||
)
|
||||
list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
|
||||
conversationID, cursor, limit, senderType, messageType,
|
||||
)
|
||||
results := builders.BuildMessages(list)
|
||||
return web.JsonCursorData(results, cast.ToString(nextCursor), hasMore)
|
||||
}
|
||||
|
||||
func (c *MessageController) PostSend() *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.SendConversationMessageRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
|
||||
item, err := services.MessageService.SendCustomerMessage(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
return web.JsonData(builders.BuildMessage(item))
|
||||
}
|
||||
|
||||
func (c *MessageController) PostRead() *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.ReadConversationRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
}
|
||||
|
||||
func (c *MessageController) PostUpload_image() *web.JsonResult {
|
||||
if services.ChannelService.GetEnabledChannel(c.Ctx) == nil {
|
||||
return web.JsonErrorMsg("接入渠道未初始化")
|
||||
}
|
||||
external := irisx.GetExternalInfo(c.Ctx)
|
||||
if external == nil {
|
||||
return web.JsonErrorMsg("外部身份未初始化")
|
||||
}
|
||||
|
||||
rawConv := strings.TrimSpace(c.Ctx.FormValue("conversationId"))
|
||||
if rawConv == "" {
|
||||
return web.JsonErrorMsg("conversationId不能为空")
|
||||
}
|
||||
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
||||
if err != nil || conversationID <= 0 {
|
||||
return web.JsonErrorMsg("conversationId不能为空")
|
||||
}
|
||||
conversation := services.ConversationService.Get(conversationID)
|
||||
if conversation == nil {
|
||||
return web.JsonErrorMsg("会话不存在")
|
||||
}
|
||||
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
||||
return web.JsonErrorMsg("无权访问该会话")
|
||||
}
|
||||
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
|
||||
f, header, err := c.Ctx.FormFile("file")
|
||||
if err != nil {
|
||||
return web.JsonErrorMsg("请选择上传图片")
|
||||
}
|
||||
_ = f.Close()
|
||||
|
||||
if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
|
||||
return web.JsonErrorMsg("仅支持上传图片文件")
|
||||
}
|
||||
|
||||
item, err := services.AssetService.UploadFile(header, "images", nil)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
return web.JsonData(builders.BuildAsset(item))
|
||||
}
|
||||
|
||||
func (c *MessageController) PostUpload_attachment() *web.JsonResult {
|
||||
if services.ChannelService.GetEnabledChannel(c.Ctx) == nil {
|
||||
return web.JsonErrorMsg("接入渠道未初始化")
|
||||
}
|
||||
external := irisx.GetExternalInfo(c.Ctx)
|
||||
if external == nil {
|
||||
return web.JsonErrorMsg("外部身份未初始化")
|
||||
}
|
||||
|
||||
rawConv := strings.TrimSpace(c.Ctx.FormValue("conversationId"))
|
||||
if rawConv == "" {
|
||||
return web.JsonErrorMsg("conversationId不能为空")
|
||||
}
|
||||
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
||||
if err != nil || conversationID <= 0 {
|
||||
return web.JsonErrorMsg("conversationId不能为空")
|
||||
}
|
||||
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
|
||||
f, header, err := c.Ctx.FormFile("file")
|
||||
if err != nil {
|
||||
return web.JsonErrorMsg("请选择上传附件")
|
||||
}
|
||||
_ = f.Close()
|
||||
|
||||
item, err := services.AssetService.UploadFile(header, "attachments", nil)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
}
|
||||
return web.JsonData(builders.BuildAsset(item))
|
||||
}
|
||||
Reference in New Issue
Block a user