From 01274b29a3b192f03f56aff0f659e10a012e1a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Thu, 22 Oct 2020 22:21:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AFv2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/message.go | 88 +++++++++++++++++++++++++++++++++++++++++++ router/api.go | 1 + 2 files changed, 89 insertions(+) diff --git a/controller/message.go b/controller/message.go index 4849c95..74b339e 100644 --- a/controller/message.go +++ b/controller/message.go @@ -8,6 +8,7 @@ import ( "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" + "github.com/taoshihan1991/imaptool/ws" "os" "path" "strings" @@ -113,6 +114,93 @@ func SendMessage(c *gin.Context) { "msg": "ok", }) } +func SendMessageV2(c *gin.Context) { + fromId := c.PostForm("from_id") + toId := c.PostForm("to_id") + content := c.PostForm("content") + cType := c.PostForm("type") + if content == "" { + c.JSON(200, gin.H{ + "code": 400, + "msg": "内容不能为空", + }) + return + } + + var kefuInfo models.User + var vistorInfo models.Visitor + if cType == "kefu" { + kefuInfo = models.FindUser(fromId) + vistorInfo = models.FindVisitorByVistorId(toId) + } else if cType == "visitor" { + vistorInfo = models.FindVisitorByVistorId(fromId) + kefuInfo = models.FindUser(toId) + } + + if kefuInfo.ID == 0 || vistorInfo.ID == 0 { + c.JSON(200, gin.H{ + "code": 400, + "msg": "用户不存在", + }) + return + } + models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType) + + if cType == "kefu" { + guest, ok := clientList[vistorInfo.VisitorId] + if guest == nil || !ok { + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) + return + } + conn := guest.conn + + msg := TypeMessage{ + Type: "message", + Data: ClientMessage{ + Name: kefuInfo.Nickname, + Avator: kefuInfo.Avator, + Id: kefuInfo.Name, + Time: time.Now().Format("2006-01-02 15:04:05"), + ToId: vistorInfo.VisitorId, + Content: content, + }, + } + str, _ := json.Marshal(msg) + conn.WriteMessage(websocket.TextMessage, str) + } + if cType == "visitor" { + 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{ + Avator: vistorInfo.Avator, + Id: vistorInfo.VisitorId, + Name: vistorInfo.Name, + ToId: kefuInfo.Name, + Content: content, + Time: time.Now().Format("2006-01-02 15:04:05"), + }, + } + str, _ := json.Marshal(msg) + for _, kefuConn := range kefuConns { + kefuConn.Conn.WriteMessage(websocket.TextMessage, str) + } + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} func SendVisitorNotice(c *gin.Context) { notice := c.Query("msg") if notice == "" { diff --git a/router/api.go b/router/api.go index 21e37e3..dcb73e0 100644 --- a/router/api.go +++ b/router/api.go @@ -25,6 +25,7 @@ func InitApiRouter(engine *gin.Engine) { engine.GET("/message_notice", controller.SendVisitorNotice) //发送单条消息 engine.POST("/message", middleware.Ipblack, controller.SendMessage) + engine.POST("/2/message", middleware.Ipblack, controller.SendMessageV2) //发送关闭消息 engine.GET("/message_close", controller.SendCloseMessage) //上传文件