c246b85a9e
- Implemented request ID handling in various services and handlers to improve traceability of requests. - Added new AuthOptions endpoint to expose WxWork and OIDC configuration options. - Updated message and event logging to include request ID for better debugging. - Enhanced login form to dynamically show available authentication options based on server configuration. - Introduced utility functions for normalizing and ensuring valid request IDs. - Updated tests to verify request ID functionality in message sending and event logging.
198 lines
5.9 KiB
Go
198 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"cs-agent/internal/builders"
|
|
"cs-agent/internal/pkg/dto/request"
|
|
"cs-agent/internal/pkg/enums"
|
|
"cs-agent/internal/pkg/httpx"
|
|
"cs-agent/internal/pkg/i18nx"
|
|
"cs-agent/internal/services"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/web"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
func MessageAnyList(ctx *gin.Context) {
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
return
|
|
}
|
|
external := httpx.GetExternalUser(ctx)
|
|
if external == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
return
|
|
}
|
|
|
|
conversationID, _ := params.GetInt64(ctx, "conversationId")
|
|
if conversationID <= 0 {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
return
|
|
}
|
|
conversation := services.ConversationService.Get(conversationID)
|
|
if conversation == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
|
return
|
|
}
|
|
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
|
return
|
|
}
|
|
|
|
var (
|
|
senderType, _ = params.Get(ctx, "senderType")
|
|
messageType, _ = params.Get(ctx, "messageType")
|
|
cursor, _ = params.GetInt64(ctx, "cursor")
|
|
limit, _ = params.GetInt(ctx, "limit")
|
|
)
|
|
list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
|
|
conversationID, cursor, limit, senderType, messageType,
|
|
)
|
|
results := builders.BuildMessagesWithLocale(list, i18nx.Locale(ctx))
|
|
httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore))
|
|
}
|
|
|
|
func MessagePostSend(ctx *gin.Context) {
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
return
|
|
}
|
|
external := httpx.GetExternalUser(ctx)
|
|
if external == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
return
|
|
}
|
|
|
|
req := request.SendConversationMessageRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
item, err := services.MessageService.SendCustomerMessageWithRequestID(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external, httpx.GetRequestID(ctx))
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildMessageWithLocale(item, i18nx.Locale(ctx)))
|
|
}
|
|
|
|
func MessagePostRead(ctx *gin.Context) {
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
return
|
|
}
|
|
external := httpx.GetExternalUser(ctx)
|
|
if external == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
return
|
|
}
|
|
|
|
req := request.ReadConversationRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
}
|
|
|
|
func MessagePostUpload_image(ctx *gin.Context) {
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
return
|
|
}
|
|
external := httpx.GetExternalUser(ctx)
|
|
if external == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
return
|
|
}
|
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
|
if rawConv == "" {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
return
|
|
}
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
if err != nil || conversationID <= 0 {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
return
|
|
}
|
|
conversation := services.ConversationService.Get(conversationID)
|
|
if conversation == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
|
|
return
|
|
}
|
|
if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
|
|
return
|
|
}
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
header, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片"))
|
|
return
|
|
}
|
|
if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件"))
|
|
return
|
|
}
|
|
|
|
item, err := services.AssetService.UploadFile(header, "images", nil)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
|
}
|
|
|
|
func MessagePostUpload_attachment(ctx *gin.Context) {
|
|
if services.ChannelService.GetEnabledChannel(ctx) == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
|
|
return
|
|
}
|
|
external := httpx.GetExternalUser(ctx)
|
|
if external == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
|
|
return
|
|
}
|
|
|
|
rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
|
|
if rawConv == "" {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
return
|
|
}
|
|
conversationID, err := strconv.ParseInt(rawConv, 10, 64)
|
|
if err != nil || conversationID <= 0 {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
|
|
return
|
|
}
|
|
if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
header, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件"))
|
|
return
|
|
}
|
|
item, err := services.AssetService.UploadFile(header, "attachments", nil)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildAsset(item))
|
|
}
|