2026-04-25 10:27:43 +08:00
|
|
|
package api
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cs-agent/internal/builders"
|
|
|
|
|
"cs-agent/internal/pkg/dto/request"
|
|
|
|
|
"cs-agent/internal/pkg/enums"
|
2026-05-23 22:10:20 +08:00
|
|
|
"cs-agent/internal/pkg/httpx"
|
2026-05-25 12:06:15 +08:00
|
|
|
"cs-agent/internal/pkg/i18nx"
|
2026-04-09 10:01:23 +08:00
|
|
|
"cs-agent/internal/services"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
2026-05-23 22:22:18 +08:00
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-04-09 10:01:23 +08:00
|
|
|
"github.com/mlogclub/simple/web"
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func MessageAnyList(ctx *gin.Context) {
|
|
|
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
external := httpx.GetExternalUser(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if external == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
conversationID, _ := params.GetInt64(ctx, "conversationId")
|
2026-04-09 10:01:23 +08:00
|
|
|
if conversationID <= 0 {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
conversation := services.ConversationService.Get(conversationID)
|
|
|
|
|
if conversation == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
2026-05-23 22:22:18 +08:00
|
|
|
senderType, _ = params.Get(ctx, "senderType")
|
|
|
|
|
messageType, _ = params.Get(ctx, "messageType")
|
|
|
|
|
cursor, _ = params.GetInt64(ctx, "cursor")
|
|
|
|
|
limit, _ = params.GetInt(ctx, "limit")
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
|
|
|
|
|
conversationID, cursor, limit, senderType, messageType,
|
|
|
|
|
)
|
2026-05-25 12:06:15 +08:00
|
|
|
results := builders.BuildMessagesWithLocale(list, i18nx.Locale(ctx))
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func MessagePostSend(ctx *gin.Context) {
|
|
|
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
external := httpx.GetExternalUser(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if external == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := request.SendConversationMessageRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item, err := services.MessageService.SendCustomerMessage(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-25 12:06:15 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildMessageWithLocale(item, i18nx.Locale(ctx)))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func MessagePostRead(ctx *gin.Context) {
|
|
|
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
external := httpx.GetExternalUser(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if external == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := request.ReadConversationRequest{}
|
2026-05-23 22:22:18 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, nil)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func MessagePostUpload_image(ctx *gin.Context) {
|
|
|
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
external := httpx.GetExternalUser(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if external == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
2026-04-09 10:01:23 +08:00
|
|
|
if rawConv == "" {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
|
|
|
if err != nil || conversationID <= 0 {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
conversation := services.ConversationService.Get(conversationID)
|
|
|
|
|
if conversation == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
header, err := ctx.FormFile("file")
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item, err := services.AssetService.UploadFile(header, "images", nil)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
func MessagePostUpload_attachment(ctx *gin.Context) {
|
|
|
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
external := httpx.GetExternalUser(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if external == nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
2026-04-09 10:01:23 +08:00
|
|
|
if rawConv == "" {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
|
|
|
if err != nil || conversationID <= 0 {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:22:18 +08:00
|
|
|
header, err := ctx.FormFile("file")
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件"))
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
item, err := services.AssetService.UploadFile(header, "attachments", nil)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:22:18 +08:00
|
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|