diff --git a/controller/main.go b/controller/main.go index 37c66de..97cc7c1 100644 --- a/controller/main.go +++ b/controller/main.go @@ -5,6 +5,7 @@ import ( "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tools" + "github.com/taoshihan1991/imaptool/ws" "net/http" ) @@ -34,7 +35,7 @@ func MainCheckAuth(c *gin.Context) { func GetStatistics(c *gin.Context) { visitors := models.CountVisitors() message := models.CountMessage() - session := len(clientList) + session := len(ws.ClientList) c.JSON(200, gin.H{ "code": 200, "msg": "ok", diff --git a/controller/message.go b/controller/message.go index 131c28e..9011d94 100644 --- a/controller/message.go +++ b/controller/message.go @@ -170,17 +170,30 @@ func SendMessageV2(c *gin.Context) { } str, _ := json.Marshal(msg) 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" { - kefuConns, ok := ws.KefuList[kefuInfo.Name] - if kefuConns == nil || !ok { - c.JSON(200, gin.H{ - "code": 200, - "msg": "ok", - }) - return - } + //kefuConns, ok := ws.KefuList[kefuInfo.Name] + //if kefuConns == nil || !ok { + // c.JSON(200, gin.H{ + // "code": 200, + // "msg": "ok", + // }) + // return + //} msg = TypeMessage{ Type: "message", Data: ClientMessage{ @@ -193,9 +206,7 @@ func SendMessageV2(c *gin.Context) { }, } str, _ := json.Marshal(msg) - for _, kefuConn := range kefuConns { - kefuConn.Conn.WriteMessage(websocket.TextMessage, str) - } + ws.OneKefuMessage(kefuInfo.Name, str) } c.JSON(200, gin.H{ "code": 200, diff --git a/ws/user.go b/ws/user.go index 446f50b..9cb3fb5 100644 --- a/ws/user.go +++ b/ws/user.go @@ -9,8 +9,8 @@ import ( ) func NewKefuServer(c *gin.Context) { - kefuId, _ := c.Get("kefu_name") - kefuInfo := models.FindUser(kefuId.(string)) + kefuId, _ := c.Get("kefu_id") + kefuInfo := models.FindUserById(kefuId) if kefuInfo.ID == 0 { c.JSON(200, gin.H{ "code": 400, @@ -30,6 +30,7 @@ func NewKefuServer(c *gin.Context) { kefu.Id = kefuInfo.Name kefu.Name = kefuInfo.Nickname kefu.Avator = kefuInfo.Avator + kefu.Role_id = kefuInfo.RoleId kefu.Conn = conn 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) +} diff --git a/ws/visitor.go b/ws/visitor.go index ea25ffb..1d333bb 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -3,7 +3,6 @@ package ws import ( "encoding/json" "github.com/gin-gonic/gin" - "github.com/gorilla/websocket" "github.com/taoshihan1991/imaptool/models" "log" ) @@ -54,12 +53,8 @@ func NewVisitorServer(c *gin.Context) { Data: userInfo, } str, _ := json.Marshal(msg) - kefuConns := KefuList[visitor.To_id] - if kefuConns != nil { - for _, kefuConn := range kefuConns { - kefuConn.Conn.WriteMessage(websocket.TextMessage, str) - } - } + //新版 + OneKefuMessage(user.To_id, str) } } log.Println(err) @@ -93,10 +88,5 @@ func AddVisitorToList(user *User) { str, _ := json.Marshal(msg) //新版 - mKefuConns := KefuList[user.To_id] - if mKefuConns != nil { - for _, kefu := range mKefuConns { - kefu.Conn.WriteMessage(websocket.TextMessage, str) - } - } + OneKefuMessage(user.To_id, str) } diff --git a/ws/ws.go b/ws/ws.go index fc92777..a4e3cc0 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -13,11 +13,12 @@ import ( ) type User struct { - Conn *websocket.Conn - Name string - Id string - Avator string - To_id string + Conn *websocket.Conn + Name string + Id string + Avator string + To_id string + Role_id string } type Message struct { conn *websocket.Conn