diff --git a/config/go-fly.sql b/config/go-fly.sql index 02f8603..760f2c9 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -10,6 +10,7 @@ CREATE TABLE `user` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; + CREATE TABLE `visitor` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '', @@ -20,6 +21,10 @@ CREATE TABLE `visitor` ( `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `deleted_at` timestamp NULL DEFAULT NULL, `visitor_id` varchar(100) NOT NULL DEFAULT '', + `status` tinyint(4) NOT NULL DEFAULT '0', + `refer` varchar(500) NOT NULL DEFAULT '', + `city` varchar(100) NOT NULL DEFAULT '', + `client_ip` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `visitor_id` (`visitor_id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; \ No newline at end of file +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 \ No newline at end of file diff --git a/controller/chat.go b/controller/chat.go index a1907a6..7428e2e 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -16,7 +16,7 @@ type vistor struct{ } type Message struct{ conn *websocket.Conn - ip string + c *gin.Context content []byte } var clientList = make(map[string]*vistor) @@ -35,6 +35,8 @@ type ClientMessage struct { Time string `json:"time"` ToId string `json:"to_id"` Content string `json:"content"` + City string `json:"city"` + ClientIp string `json:"client_ip"` } //定时检测客户端是否在线 func init() { @@ -66,7 +68,7 @@ func NewChatServer(c *gin.Context){ message<-&Message{ conn:conn, content: receive, - ip:c.ClientIP(), + c:c, } } } @@ -168,7 +170,7 @@ func singleBroadcaster(){ } clientList[clientMsg.Id] = user //插入数据表 - models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.ip,clientMsg.ToId,clientMsg.Id) + models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.Id,message.c.Request.Referer(),clientMsg.City,clientMsg.ClientIp) SendNoticeToAllKefu() //客服上线 case "kfOnline": diff --git a/controller/visitor.go b/controller/visitor.go new file mode 100644 index 0000000..0f04675 --- /dev/null +++ b/controller/visitor.go @@ -0,0 +1,18 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/models" + "log" +) + +func GetVisitor(c *gin.Context) { + visitorId:=c.Query("visitorId") + vistor:=models.FindVisitorByVistorId(visitorId) + log.Println(visitorId) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":vistor, + }) +} diff --git a/models/visitors.go b/models/visitors.go index 64bd62f..c1bce92 100644 --- a/models/visitors.go +++ b/models/visitors.go @@ -4,13 +4,17 @@ import "github.com/jinzhu/gorm" type Visitor struct { gorm.Model - Name string - Avator string - SourceIp string - ToId string - VisitorId string + Name string `json:"name"` + Avator string `json:"avator"` + SourceIp string `json:"source_ip"` + ToId string `json:"to_id"` + VisitorId string `json:"visitor_id"` + Status uint `json:"status"` + Refer string `json:"refer"` + City string `json:"city"` + ClientIp string `json:"client_ip"` } -func CreateVisitor(name string,avator string,sourceIp string,toId string,visitorId string){ +func CreateVisitor(name string,avator string,sourceIp string,toId string,visitorId string,refer string,city string,clientIp string){ old:=FindVisitorByVistorId(visitorId) if old.Name!=""{ return @@ -21,6 +25,10 @@ func CreateVisitor(name string,avator string,sourceIp string,toId string,visitor SourceIp:sourceIp, ToId:toId, VisitorId: visitorId, + Status:1, + Refer:refer, + City:city, + ClientIp:clientIp, } DB.Create(v) } diff --git a/server.go b/server.go index c440494..3fcfbf2 100644 --- a/server.go +++ b/server.go @@ -39,6 +39,7 @@ func main() { engine.GET("/setting_mysql", tmpl.PageSettingMysql) engine.GET("/mysql",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.MysqlGetConf) engine.POST("/mysql",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.MysqlSetConf) + engine.GET("/visitor",middleware.JwtApiMiddleware, controller.GetVisitor) //------------------old code----------------------------- mux := &http.ServeMux{} //根路径 diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 847ab35..b852e61 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -102,13 +102,28 @@ 发送 - - + + - + 访客信息 - + + + 来源:<{visitor.refer}> + + + + IP:<{visitor.client_ip}> + + + + 城市:<{visitor.city}> + + + + 状态:<{visitor.status}> + @@ -138,6 +153,12 @@ name : "客服丽丽", avator : "", to_id : "", + }, + visitor:{ + refer:"", + client_ip:"", + city:"", + status:"", } }, methods: { @@ -240,6 +261,9 @@ this.kfConfig.to_id=guestId; mes.data = this.kfConfig; this.socket.send(JSON.stringify(mes)); + + //获取当前访客信息 + this.getVistorInfo(guestId); }, //发送给客户 chatToUser() { @@ -307,6 +331,33 @@ _this.initConn(); }); }, + //获取客服信息 + getVistorInfo(vid){ + let _this=this; + $.ajax({ + type:"get", + url:"/visitor", + data:{visitorId:vid}, + headers:{ + "token":localStorage.getItem("token") + }, + success: function(data) { + if(data.result!=null){ + let r=data.result; + _this.visitor.refer=r.refer; + _this.visitor.city=r.city; + _this.visitor.client_ip=r.client_ip; + _this.visitor.status=r.status; + } + if(data.code!=200){ + _this.$message({ + message: data.msg, + type: 'error' + }); + } + } + }); + }, }, created: function () { this.getKefuInfo(); diff --git a/static/html/chat_page.html b/static/html/chat_page.html index d1ee037..72c7aff 100644 --- a/static/html/chat_page.html +++ b/static/html/chat_page.html @@ -133,6 +133,8 @@ let userinfo=this.getUserInfo(); let mes = {} mes.type = "userInit"; + userinfo.client_ip=returnCitySN["cip"]; + userinfo.city=returnCitySN["cname"]; mes.data = userinfo; this.socket.send(JSON.stringify(mes)); },