From 30ac128a5f904771b634df522d69667cbecea549 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Thu, 23 Jul 2020 16:31:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/message.go | 106 +++++++++++++++++++++++++++++++++++++ docs/docs.go | 52 +++++++++++++++++- docs/swagger.json | 50 +++++++++++++++++ docs/swagger.yaml | 34 ++++++++++++ main.go | 2 +- static/html/chat_main.html | 16 +++--- 6 files changed, 252 insertions(+), 8 deletions(-) create mode 100644 controller/message.go diff --git a/controller/message.go b/controller/message.go new file mode 100644 index 0000000..0d701fe --- /dev/null +++ b/controller/message.go @@ -0,0 +1,106 @@ +package controller + +import ( + "encoding/json" + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + "github.com/taoshihan1991/imaptool/models" + "time" +) +// @Summary 发送消息接口 +// @Produce json +// @Accept multipart/form-data +// @Param from_id formData string true "来源uid" +// @Param to_id formData string true "目标uid" +// @Param content formData string true "内容" +// @Param type formData string true "类型|kefu,visitor" +// @Success 200 {object} controller.Response +// @Failure 200 {object} controller.Response +// @Router /message [post] +func SendMessage(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 := 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.WriteMessage(websocket.TextMessage,str) + } + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} diff --git a/docs/docs.go b/docs/docs.go index e515bd3..53780f4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2020-07-13 19:22:07.3168355 +0800 CST m=+0.136007801 +// 2020-07-23 16:30:50.1490506 +0800 CST m=+0.137007801 package docs @@ -66,6 +66,56 @@ var doc = `{ } } }, + "/message": { + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "summary": "发送消息接口", + "parameters": [ + { + "type": "string", + "description": "来源uid", + "name": "from_id", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "目标uid", + "name": "to_id", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "内容", + "name": "content", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "类型|kefu,visitor", + "name": "type", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, "/messages": { "get": { "consumes": [ diff --git a/docs/swagger.json b/docs/swagger.json index ab3e990..d340b46 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -53,6 +53,56 @@ } } }, + "/message": { + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "summary": "发送消息接口", + "parameters": [ + { + "type": "string", + "description": "来源uid", + "name": "from_id", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "目标uid", + "name": "to_id", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "内容", + "name": "content", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "类型|kefu,visitor", + "name": "type", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, "/messages": { "get": { "consumes": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9900116..2fcea99 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -46,6 +46,40 @@ paths: $ref: '#/definitions/controller.Response' type: object summary: 登陆验证接口 + /message: + post: + consumes: + - multipart/form-data + parameters: + - description: 来源uid + in: formData + name: from_id + required: true + type: string + - description: 目标uid + in: formData + name: to_id + required: true + type: string + - description: 内容 + in: formData + name: content + required: true + type: string + - description: 类型|kefu,visitor + in: formData + name: type + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/controller.Response' + type: object + summary: 发送消息接口 /messages: get: consumes: diff --git a/main.go b/main.go index 5adbb6b..b88e970 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func main() { //获取消息 engine.GET("/messages",middleware.JwtApiMiddleware, controller.GetVisitorMessage) //发送单条消息 - engine.POST("/message",controller.GetVisitorMessage) + engine.POST("/message",controller.SendMessage) //获取未读消息数 engine.GET("/message_status",controller.GetVisitorMessage) //设置消息已读 diff --git a/static/html/chat_main.html b/static/html/chat_main.html index aad30f4..0dc13f8 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -300,17 +300,21 @@ if(this.messageContent==""||this.currentGuest==""){ return; } + let _this=this; let mes = {}; - mes.type = "kfChatMessage"; - this.kfConfig.content = this.messageContent; - mes.data = this.kfConfig; - this.socket.send(JSON.stringify(mes)); - this.messageContent = ""; + mes.type = "kefu"; + mes.content = this.messageContent; + mes.from_id = this.kfConfig.id; + mes.to_id = this.currentGuest; + mes.content = this.messageContent; + $.post("/message",mes,function(){ + _this.messageContent = ""; + }); let content = {} content.avator = this.kfConfig.avator; content.name = this.kfConfig.name; - content.content = this.kfConfig.content; + content.content = this.messageContent; content.is_kefu = true; content.time = ''; this.msgList.push(content);