给超管发消息

pull/23/head
taoshihan1991 4 years ago
parent 6af8350add
commit fad3b1f75b

@ -5,6 +5,7 @@ import (
"github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
"net/http" "net/http"
) )
@ -34,7 +35,7 @@ func MainCheckAuth(c *gin.Context) {
func GetStatistics(c *gin.Context) { func GetStatistics(c *gin.Context) {
visitors := models.CountVisitors() visitors := models.CountVisitors()
message := models.CountMessage() message := models.CountMessage()
session := len(clientList) session := len(ws.ClientList)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"code": 200, "code": 200,
"msg": "ok", "msg": "ok",

@ -170,17 +170,30 @@ func SendMessageV2(c *gin.Context) {
} }
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)
conn.WriteMessage(websocket.TextMessage, str) conn.WriteMessage(websocket.TextMessage, str)
msg = TypeMessage{
Type: "message",
Data: ClientMessage{
Name: kefuInfo.Nickname,
Avator: kefuInfo.Avator,
Id: vistorInfo.VisitorId,
Time: time.Now().Format("2006-01-02 15:04:05"),
ToId: vistorInfo.VisitorId,
Content: content,
},
}
str2, _ := json.Marshal(msg)
ws.SuperAdminMessage(str2)
} }
if cType == "visitor" { if cType == "visitor" {
kefuConns, ok := ws.KefuList[kefuInfo.Name] //kefuConns, ok := ws.KefuList[kefuInfo.Name]
if kefuConns == nil || !ok { //if kefuConns == nil || !ok {
c.JSON(200, gin.H{ // c.JSON(200, gin.H{
"code": 200, // "code": 200,
"msg": "ok", // "msg": "ok",
}) // })
return // return
} //}
msg = TypeMessage{ msg = TypeMessage{
Type: "message", Type: "message",
Data: ClientMessage{ Data: ClientMessage{
@ -193,9 +206,7 @@ func SendMessageV2(c *gin.Context) {
}, },
} }
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)
for _, kefuConn := range kefuConns { ws.OneKefuMessage(kefuInfo.Name, str)
kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
}
} }
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"code": 200, "code": 200,

@ -9,8 +9,8 @@ import (
) )
func NewKefuServer(c *gin.Context) { func NewKefuServer(c *gin.Context) {
kefuId, _ := c.Get("kefu_name") kefuId, _ := c.Get("kefu_id")
kefuInfo := models.FindUser(kefuId.(string)) kefuInfo := models.FindUserById(kefuId)
if kefuInfo.ID == 0 { if kefuInfo.ID == 0 {
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"code": 400, "code": 400,
@ -30,6 +30,7 @@ func NewKefuServer(c *gin.Context) {
kefu.Id = kefuInfo.Name kefu.Id = kefuInfo.Name
kefu.Name = kefuInfo.Nickname kefu.Name = kefuInfo.Nickname
kefu.Avator = kefuInfo.Avator kefu.Avator = kefuInfo.Avator
kefu.Role_id = kefuInfo.RoleId
kefu.Conn = conn kefu.Conn = conn
AddKefuToList(&kefu) AddKefuToList(&kefu)
@ -96,3 +97,27 @@ func kefuServerBackend() {
} }
} }
//给超管发消息
func SuperAdminMessage(str []byte) {
//给超管发
for _, kefuUsers := range KefuList {
for _, kefuUser := range kefuUsers {
if kefuUser.Role_id == "2" {
kefuUser.Conn.WriteMessage(websocket.TextMessage, str)
}
}
}
}
//给指定客服发消息
func OneKefuMessage(toId string, str []byte) {
//新版
mKefuConns := KefuList[toId]
if mKefuConns != nil {
for _, kefu := range mKefuConns {
kefu.Conn.WriteMessage(websocket.TextMessage, str)
}
}
SuperAdminMessage(str)
}

@ -3,7 +3,6 @@ package ws
import ( import (
"encoding/json" "encoding/json"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/models"
"log" "log"
) )
@ -54,12 +53,8 @@ func NewVisitorServer(c *gin.Context) {
Data: userInfo, Data: userInfo,
} }
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)
kefuConns := KefuList[visitor.To_id] //新版
if kefuConns != nil { OneKefuMessage(user.To_id, str)
for _, kefuConn := range kefuConns {
kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
}
}
} }
} }
log.Println(err) log.Println(err)
@ -93,10 +88,5 @@ func AddVisitorToList(user *User) {
str, _ := json.Marshal(msg) str, _ := json.Marshal(msg)
//新版 //新版
mKefuConns := KefuList[user.To_id] OneKefuMessage(user.To_id, str)
if mKefuConns != nil {
for _, kefu := range mKefuConns {
kefu.Conn.WriteMessage(websocket.TextMessage, str)
}
}
} }

@ -13,11 +13,12 @@ import (
) )
type User struct { type User struct {
Conn *websocket.Conn Conn *websocket.Conn
Name string Name string
Id string Id string
Avator string Avator string
To_id string To_id string
Role_id string
} }
type Message struct { type Message struct {
conn *websocket.Conn conn *websocket.Conn

Loading…
Cancel
Save