添加消息发送API、丰富种子对话数据、Dashboard支持真实消息发送和客户名显示
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"kefu-sys/server/internal/middleware"
|
||||
@@ -12,6 +13,52 @@ type SessionHandler struct{}
|
||||
|
||||
func NewSessionHandler() *SessionHandler { return &SessionHandler{} }
|
||||
|
||||
type SendMessageReq struct {
|
||||
Content string `json:"content" binding:"required"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func (h *SessionHandler) SendMessage(c *gin.Context) {
|
||||
tenantID := middleware.GetTenantID(c)
|
||||
userID := middleware.GetUserID(c)
|
||||
id := c.Param("id")
|
||||
|
||||
var req SendMessageReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
||||
return
|
||||
}
|
||||
if req.Type == "" {
|
||||
req.Type = "text"
|
||||
}
|
||||
|
||||
var session model.Session
|
||||
if err := model.DB.Where("id = ? AND tenant_id = ?", id, tenantID).First(&session).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "会话不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
var maxSeq int
|
||||
model.DB.Model(&model.Message{}).Where("session_id = ?", session.ID).Select("COALESCE(MAX(seq), 0)").Scan(&maxSeq)
|
||||
|
||||
msg := model.Message{
|
||||
SessionID: session.ID,
|
||||
SenderType: "agent",
|
||||
SenderID: &userID,
|
||||
Content: req.Content,
|
||||
Type: req.Type,
|
||||
Seq: maxSeq + 1,
|
||||
SentAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := model.DB.Create(&msg).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "发送失败"})
|
||||
return
|
||||
}
|
||||
|
||||
middleware.JSON(c, msg)
|
||||
}
|
||||
|
||||
type CreateSessionReq struct {
|
||||
ChannelID uint `json:"channel_id"`
|
||||
CustomerID uint `json:"customer_id"`
|
||||
@@ -170,7 +217,6 @@ func (h *SessionHandler) UpdatePriority(c *gin.Context) {
|
||||
|
||||
func parseID(s string) uint {
|
||||
var id uint
|
||||
// Simple atoi for uint
|
||||
for _, c := range s {
|
||||
if c >= '0' && c <= '9' {
|
||||
id = id*10 + uint(c-'0')
|
||||
|
||||
Reference in New Issue
Block a user