From 194b6f3418d311713f68f434e54beda1ceba5175 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Wed, 8 Jul 2020 11:12:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=A2=E6=9C=8D=E4=B9=8B?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E7=94=A8=E6=88=B7=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 33 +++++++++++++++++++-------------- controller/visitor.go | 3 ++- middleware/jwt.go | 1 + models/visitors.go | 9 +++++++++ static/html/chat_web.js | 14 ++++++++------ static/html/nav.html | 2 +- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/controller/chat.go b/controller/chat.go index a5b491f..fdd54bb 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -14,6 +14,7 @@ type vistor struct{ name string id string avator string + to_id string } type Message struct{ conn *websocket.Conn @@ -130,26 +131,29 @@ func sendPingUpdateStatus() { //定时推送当前在线用户 func sendPingOnlineUsers() { for { - result := make([]map[string]string, 0) var visitorIds []string for visitorId, _ := range clientList { visitorIds=append(visitorIds,visitorId) } sort.Strings(visitorIds) - for _,visitorId:=range visitorIds{ - user:=clientList[visitorId] - userInfo := make(map[string]string) - userInfo["uid"] = user.id - userInfo["username"] = user.name - userInfo["avator"] = user.avator - result = append(result, userInfo) - } - msg := TypeMessage{ - Type: "getOnlineUsers", - Data: result, - } - str, _ := json.Marshal(msg) + for kefuId, kfConn := range kefuList { + result := make([]map[string]string, 0) + for _,visitorId:=range visitorIds{ + user:=clientList[visitorId] + userInfo := make(map[string]string) + userInfo["uid"] = user.id + userInfo["username"] = user.name + userInfo["avator"] = user.avator + if user.to_id==kefuId{ + result = append(result, userInfo) + } + } + msg := TypeMessage{ + Type: "getOnlineUsers", + Data: result, + } + str, _ := json.Marshal(msg) err:=kfConn.WriteMessage(websocket.TextMessage,str) if err != nil { delete(kefuList, kefuId) @@ -185,6 +189,7 @@ func singleBroadcaster(){ name: clientMsg.Name, avator: clientMsg.Avator, id:clientMsg.Id, + to_id:clientMsg.ToId, } clientList[clientMsg.Id] = user //插入数据表 diff --git a/controller/visitor.go b/controller/visitor.go index 6b69316..c2142c0 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -18,7 +18,8 @@ func GetVisitor(c *gin.Context) { } func GetVisitors(c *gin.Context) { page,_:=strconv.Atoi(c.Query("page")) - vistors:=models.FindVisitors(uint(page),config.VisitorPageSize) + kefuId,_:=c.Get("kefu_name") + vistors:=models.FindVisitorsByKefuId(uint(page),config.VisitorPageSize,kefuId.(string)) count:=models.CountVisitors() c.JSON(200, gin.H{ "code": 200, diff --git a/middleware/jwt.go b/middleware/jwt.go index f254bde..032642c 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -39,5 +39,6 @@ func JwtApiMiddleware(c *gin.Context){ c.Set("user",userinfo["name"]) if userinfo["type"]=="kefu"{ c.Set("kefu_id",userinfo["kefu_id"]) + c.Set("kefu_name",userinfo["name"]) } } diff --git a/models/visitors.go b/models/visitors.go index 9a20ba8..b8ffe01 100644 --- a/models/visitors.go +++ b/models/visitors.go @@ -46,6 +46,15 @@ func FindVisitors(page uint,pagesize uint)[]Visitor{ DB.Offset(offset).Limit(pagesize).Order("status desc, updated_at desc").Find(&visitors) return visitors } +func FindVisitorsByKefuId(page uint,pagesize uint,kefuId string)[]Visitor{ + offset:=(page-1)*pagesize + if offset<0{ + offset=0 + } + var visitors []Visitor + DB.Where("to_id=?",kefuId).Offset(offset).Limit(pagesize).Order("status desc, updated_at desc").Find(&visitors) + return visitors +} func FindVisitorsOnline()[]Visitor{ var visitors []Visitor DB.Where("status = ?",1).Find(&visitors) diff --git a/static/html/chat_web.js b/static/html/chat_web.js index 464a001..f029d3a 100644 --- a/static/html/chat_web.js +++ b/static/html/chat_web.js @@ -1,7 +1,6 @@ var loadJs=function(url,callback){ var script = document.createElement('script'), fn = callback || function(){}; script.type = 'text/javascript'; - //IE if(script.readyState){ script.onreadystatechange = function(){ if( script.readyState == 'loaded' || script.readyState == 'complete' ){ @@ -10,7 +9,6 @@ var loadJs=function(url,callback){ } }; }else{ - //其他浏览器 script.onload = function(){ fn(); }; @@ -21,7 +19,11 @@ var loadJs=function(url,callback){ loadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function(){ loadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js" ,function () { $(function () { - //==================================================================== + var goflyKefuId=""; + if(typedef(GOFLY_KEFU_ID)!="undefined"){ + var goflyKefuId=GOFLY_KEFU_ID; + } + var div =document.createElement('div'); div.id ='goflyKefu'; div.className +='goflyKefu'; @@ -36,15 +38,15 @@ loadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function(){ title: '在线咨询', shadeClose: true, shade: false, - maxmin: true, //开启最大化最小化按钮 + maxmin: true, area: ['660px', '600px'], - content: ['http://gofly.sopans.com/chat_page?kefu_id='+KEFU_ID,'no'], + content: ['http://gofly.sopans.com/chat_page?kefu_id='+goflyKefuId,'no'], end: function(){ $("#goflyKefu").show(); } }); }); - //--------------------------------------------------------------- + //END }) }); }); diff --git a/static/html/nav.html b/static/html/nav.html index 9560ce7..0b4b4a2 100644 --- a/static/html/nav.html +++ b/static/html/nav.html @@ -2,7 +2,7 @@ - + 邮箱 聊天