From 50a8616b8b24867cfc6f9a26b5963e7908ea6754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sun, 5 Jul 2020 17:03:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E8=81=8A=E5=A4=A9=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/go-fly.sql | 21 ++++++---- config/policy.csv | 4 +- controller/chat.go | 2 + controller/visitor.go | 28 +++++++++++++ models/messages.go | 24 +++++++++++ server.go | 6 ++- static/html/chat_main.html | 77 ++++++++++++++++++++++------------- static/html/chat_page.html | 1 + static/html/nav.html | 2 +- static/html/setting_left.html | 8 ++-- 10 files changed, 130 insertions(+), 43 deletions(-) create mode 100644 models/messages.go diff --git a/config/go-fly.sql b/config/go-fly.sql index 7280eac..a903ddb 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -1,3 +1,4 @@ +DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '', @@ -8,9 +9,13 @@ CREATE TABLE `user` ( `deleted_at` timestamp NULL DEFAULT NULL, `avator` varchar(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; - +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +TRUNCATE TABLE `user`; +INSERT INTO `user` (`id`, `name`, `password`, `nickname`, `created_at`, `updated_at`, `deleted_at`, `avator`) VALUES +(1, 'kefu2', '202cb962ac59075b964b07152d234b70', '小白菜', '2020-06-27 19:32:41', '2020-07-04 09:32:20', NULL, '/static/images/4.jpg'), +(2, 'kefu3', '202cb962ac59075b964b07152d234b70', '中白菜', '2020-07-02 14:36:46', '2020-07-05 08:46:57', NULL, '/static/images/11.jpg'); +DROP TABLE IF EXISTS `visitor`; CREATE TABLE `visitor` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '', @@ -27,18 +32,20 @@ CREATE TABLE `visitor` ( `client_ip` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `visitor_id` (`visitor_id`) -) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +DROP TABLE IF EXISTS `message`; CREATE TABLE `message` ( `id` int(11) NOT NULL AUTO_INCREMENT, - `from_id` varchar(100) NOT NULL DEFAULT '', - `to_id` varchar(100) NOT NULL DEFAULT '', + `kefu_id` varchar(100) NOT NULL DEFAULT '', + `visitor_id` varchar(100) NOT NULL DEFAULT '', `content` varchar(2048) NOT NULL DEFAULT '', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `deleted_at` timestamp NULL DEFAULT NULL, + `mes_type` enum('kefu','visitor') NOT NULL DEFAULT 'visitor', `status` enum('read','unread') NOT NULL DEFAULT 'unread', PRIMARY KEY (`id`), - KEY `from_id` (`from_id`), - KEY `to_id` (`to_id`) + KEY `kefu_id` (`kefu_id`), + KEY `visitor_id` (`visitor_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 \ No newline at end of file diff --git a/config/policy.csv b/config/policy.csv index b03254c..c47d7c2 100644 --- a/config/policy.csv +++ b/config/policy.csv @@ -1,2 +1,4 @@ p, admin, /mysql, GET -p, admin, /mysql, POST \ No newline at end of file +p, admin, /mysql, POST +p, admin, /kefuinfo,POST +p, admin, /kefuinfo,DELETE \ No newline at end of file diff --git a/controller/chat.go b/controller/chat.go index cce4e32..4af93ae 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -226,6 +226,7 @@ func singleBroadcaster(){ } str, _ := json.Marshal(msg) conn.WriteMessage(websocket.TextMessage,str) + models.CreateMessage(clientMsg.Id,clientMsg.ToId,clientMsg.Content,"kefu") case "chatMessage": json.Unmarshal(msgData, &clientMsg) conn,ok := kefuList[clientMsg.ToId] @@ -245,6 +246,7 @@ func singleBroadcaster(){ } str, _ := json.Marshal(msg) conn.WriteMessage(websocket.TextMessage,str) + models.CreateMessage(clientMsg.ToId,clientMsg.Id,clientMsg.Content,"visitor") } } } diff --git a/controller/visitor.go b/controller/visitor.go index 747b719..c36c977 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -22,3 +22,31 @@ func GetVisitors(c *gin.Context) { "result":vistors, }) } +func GetVisitorMessage(c *gin.Context) { + visitorId:=c.Query("visitorId") + messages:=models.FindMessageByVisitorId(visitorId) + result:=make([]map[string]interface{},0) + for _,message:=range messages{ + item:=make(map[string]interface{}) + var visitor models.Visitor + var kefu models.User + if visitor.Name=="" || kefu.Name==""{ + kefu=models.FindUser(message.KefuId) + visitor=models.FindVisitorByVistorId(message.VisitorId) + } + item["time"]=message.CreatedAt + item["content"]=message.Content + item["mes_type"]=message.MesType + item["visitor_name"]=visitor.Name + item["visitor_avator"]=visitor.Avator + item["kefu_name"]=kefu.Nickname + item["kefu_avator"]=kefu.Avator + result=append(result,item) + + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":result, + }) +} diff --git a/models/messages.go b/models/messages.go new file mode 100644 index 0000000..8244ea6 --- /dev/null +++ b/models/messages.go @@ -0,0 +1,24 @@ +package models +type Message struct { + Model + KefuId string `json:"kefu_id"` + VisitorId string `json:"visitor_id"` + Content string `json:"content"` + MesType string `json:"mes_type"` + Status string `json:"status"` +} +func CreateMessage(kefu_id string,visitor_id string,content string,mes_type string){ + v:=&Message{ + KefuId: kefu_id, + VisitorId: visitor_id, + Content: content, + MesType: mes_type, + Status: "unread", + } + DB.Create(v) +} +func FindMessageByVisitorId(visitor_id string)[]Message{ + var messages []Message + DB.Where("visitor_id=?",visitor_id).Order("id asc").Find(&messages) + return messages +} \ No newline at end of file diff --git a/server.go b/server.go index 60f5c20..70a4c98 100644 --- a/server.go +++ b/server.go @@ -41,11 +41,13 @@ func main() { engine.POST("/check_auth",middleware.JwtApiMiddleware, controller.MainCheckAuth) //前后聊天 engine.GET("/chat_server", controller.NewChatServer) + //获取消息 + engine.GET("/messages",middleware.JwtApiMiddleware, controller.GetVisitorMessage) //获取客服信息 engine.GET("/kefuinfo",middleware.JwtApiMiddleware, controller.GetKefuInfo) engine.GET("/kefuinfo_setting",middleware.JwtApiMiddleware, controller.GetKefuInfoSetting) - engine.POST("/kefuinfo",middleware.JwtApiMiddleware, controller.PostKefuInfo) - engine.DELETE("/kefuinfo",middleware.JwtApiMiddleware, controller.DeleteKefuInfo) + engine.POST("/kefuinfo",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.PostKefuInfo) + engine.DELETE("/kefuinfo",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.DeleteKefuInfo) engine.GET("/kefulist",middleware.JwtApiMiddleware, controller.GetKefuList) //设置页 engine.GET("/setting", tmpl.PageSetting) diff --git a/static/html/chat_main.html b/static/html/chat_main.html index d85dab5..133f42f 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -161,7 +161,6 @@ messageContent:"", currentGuest:"", msgList:[], - msgListUser:[], chatTitle:"暂时未处理咨询", kfConfig:{ id : "kf_1", @@ -243,28 +242,13 @@ if (msg.id == this.currentGuest) { this.msgList.push(content); } - if (typeof (this.msgListUser[msg.id]) == "undefined") { - this.msgListUser[msg.id] = []; - } - this.msgListUser[msg.id].push(content); - this.$nextTick(() => { - $('.chatBox').scrollTop($(".chatBox")[0].scrollHeight); - }); + this.scrollBottom(); } }, //接手客户 talkTo(guestId,name) { this.currentGuest = guestId; this.chatTitle=name+"|"+guestId+",正在处理中..."; - this.msgList = []; - let buf = []; - if(typeof this.msgListUser[guestId]!="undefined"){ - var i = this.msgListUser[guestId].length; - while (i--) { - buf[i] = this.msgListUser[guestId][i]; - } - } - this.msgList = buf; //发送给客户 let mes = {} @@ -275,6 +259,8 @@ //获取当前访客信息 this.getVistorInfo(guestId); + //获取当前客户消息 + this.getMesssagesByVisitorId(guestId); }, //发送给客户 chatToUser() { @@ -295,13 +281,7 @@ content.is_kefu = true; content.time = ''; this.msgList.push(content); - if (typeof (this.msgListUser[this.currentGuest]) == "undefined") { - this.msgListUser[this.currentGuest] = []; - } - this.msgListUser[this.currentGuest].push(content); - this.$nextTick(() => { - $('.chatBox').scrollTop($(".chatBox")[0].scrollHeight); - }); + this.scrollBottom(); }, //处理当前在线用户列表 handleOnlineUsers:function (retData) { @@ -312,9 +292,6 @@ this.usersMap=[]; for(let i=0;i { + $('.chatBox').scrollTop($(".chatBox")[0].scrollHeight); + }); + } }, created: function () { this.getKefuInfo(); diff --git a/static/html/chat_page.html b/static/html/chat_page.html index bc27dda..2f0d753 100644 --- a/static/html/chat_page.html +++ b/static/html/chat_page.html @@ -284,6 +284,7 @@ content.time = msg.time; setTimeout(function () { _this.msgList.push(content); + _this.scrollBottom(); }, 2000); ; } diff --git a/static/html/nav.html b/static/html/nav.html index 4e38a55..8df96ac 100644 --- a/static/html/nav.html +++ b/static/html/nav.html @@ -3,7 +3,7 @@ default-active="3" mode="horizontal"> - 邮箱 + 邮箱 聊天 设置 diff --git a/static/html/setting_left.html b/static/html/setting_left.html index 8c264f1..b659ffe 100644 --- a/static/html/setting_left.html +++ b/static/html/setting_left.html @@ -6,7 +6,7 @@ 账户设置 - 智能回复 + 智能回复 客服管理 @@ -16,9 +16,9 @@ 系统设置 - 设置smtp - 设置imap - 设置登陆账号 + 设置smtp + 设置imap + 设置登陆账号 设置mysql