完成客服工作台闭环
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"kefu-sys/server/internal/model"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
@@ -32,6 +33,11 @@ type Event struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
type ClientEvent struct {
|
||||
Type string `json:"type"`
|
||||
SessionID uint `json:"session_id"`
|
||||
}
|
||||
|
||||
type Hub struct {
|
||||
clients map[*Client]bool
|
||||
register chan *Client
|
||||
@@ -118,17 +124,59 @@ func (h *Hub) BroadcastToTenantStaff(tenantID uint, message []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) BroadcastToVisitor(tenantID, sessionID uint, message []byte) {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
for client := range h.clients {
|
||||
if client.TenantID == tenantID && client.Kind == "visitor" && client.SessionID != nil && *client.SessionID == sessionID {
|
||||
h.send(client, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleClientEvent(client *Client, event ClientEvent) {
|
||||
if client.Kind != "agent" || event.Type != "typing" || event.SessionID == 0 {
|
||||
return
|
||||
}
|
||||
var session model.Session
|
||||
if err := model.DB.Where("id = ? AND tenant_id = ?", event.SessionID, client.TenantID).First(&session).Error; err != nil {
|
||||
return
|
||||
}
|
||||
if client.Role == "agent" && (session.AgentID == nil || *session.AgentID != client.UserID) {
|
||||
return
|
||||
}
|
||||
if client.Role != "agent" && client.Role != "admin" && client.Role != "supervisor" {
|
||||
return
|
||||
}
|
||||
payload, err := NewEvent("typing", session.ID, nil)
|
||||
if err == nil {
|
||||
DefaultHub.BroadcastToVisitor(session.TenantID, session.ID, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleWebSocket(client *Client) {
|
||||
defer func() {
|
||||
DefaultHub.unregister <- client
|
||||
client.Conn.Close()
|
||||
}()
|
||||
|
||||
client.Conn.SetReadLimit(1024)
|
||||
client.Conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
||||
client.Conn.SetPongHandler(func(string) error {
|
||||
client.Conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
||||
return nil
|
||||
})
|
||||
go writePump(client)
|
||||
for {
|
||||
if _, _, err := client.Conn.ReadMessage(); err != nil {
|
||||
_, message, err := client.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var event ClientEvent
|
||||
if json.Unmarshal(message, &event) == nil {
|
||||
handleClientEvent(client, event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user