diff --git a/config/go-fly.sql b/config/go-fly.sql index 9a9a082..192cfa7 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -30,7 +30,8 @@ CREATE TABLE `visitor` ( `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, + `client_ip` varchar(100) NOT NULL DEFAULT '', + `extra` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), UNIQUE KEY `visitor_id` (`visitor_id`), KEY `to_id` (`to_id`) diff --git a/controller/visitor.go b/controller/visitor.go index 784b95e..893a52f 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -1,70 +1,68 @@ package controller import ( - "encoding/json" "fmt" "github.com/gin-gonic/gin" - "github.com/gorilla/websocket" "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" - "log" "math/rand" "strconv" ) -func PostVisitor(c *gin.Context) { - name := c.PostForm("name") - avator := c.PostForm("avator") - toId := c.PostForm("to_id") - id := c.PostForm("id") - refer := c.PostForm("refer") - city := c.PostForm("city") - client_ip := c.PostForm("client_ip") - if name == "" || avator == "" || toId == "" || id == "" || refer == "" || city == "" || client_ip == "" { - c.JSON(200, gin.H{ - "code": 400, - "msg": "error", - }) - return - } - kefuInfo := models.FindUser(toId) - if kefuInfo.ID == 0 { - c.JSON(200, gin.H{ - "code": 400, - "msg": "用户不存在", - }) - return - } - models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip) - - userInfo := make(map[string]string) - userInfo["uid"] = id - userInfo["username"] = name - userInfo["avator"] = avator - msg := TypeMessage{ - Type: "userOnline", - Data: userInfo, - } - str, _ := json.Marshal(msg) - kefuConns := kefuList[toId] - if kefuConns != nil { - for k, kefuConn := range kefuConns { - log.Println(k, "xxxxxxxx") - kefuConn.WriteMessage(websocket.TextMessage, str) - } - } - c.JSON(200, gin.H{ - "code": 200, - "msg": "ok", - }) -} +//func PostVisitor(c *gin.Context) { +// name := c.PostForm("name") +// avator := c.PostForm("avator") +// toId := c.PostForm("to_id") +// id := c.PostForm("id") +// refer := c.PostForm("refer") +// city := c.PostForm("city") +// client_ip := c.PostForm("client_ip") +// if name == "" || avator == "" || toId == "" || id == "" || refer == "" || city == "" || client_ip == "" { +// c.JSON(200, gin.H{ +// "code": 400, +// "msg": "error", +// }) +// return +// } +// kefuInfo := models.FindUser(toId) +// if kefuInfo.ID == 0 { +// c.JSON(200, gin.H{ +// "code": 400, +// "msg": "用户不存在", +// }) +// return +// } +// models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip) +// +// userInfo := make(map[string]string) +// userInfo["uid"] = id +// userInfo["username"] = name +// userInfo["avator"] = avator +// msg := TypeMessage{ +// Type: "userOnline", +// Data: userInfo, +// } +// str, _ := json.Marshal(msg) +// kefuConns := kefuList[toId] +// if kefuConns != nil { +// for k, kefuConn := range kefuConns { +// log.Println(k, "xxxxxxxx") +// kefuConn.WriteMessage(websocket.TextMessage, str) +// } +// } +// c.JSON(200, gin.H{ +// "code": 200, +// "msg": "ok", +// }) +//} func PostVisitorLogin(c *gin.Context) { ipcity := tools.ParseIp(c.ClientIP()) avator := fmt.Sprintf("/static/images/%d.jpg", rand.Intn(14)) toId := c.PostForm("to_id") id := c.PostForm("visitor_id") + extra := c.PostForm("extra") if id == "" { id = tools.Uuid() } @@ -97,7 +95,7 @@ func PostVisitorLogin(c *gin.Context) { }) return } - models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip) + models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip, extra) visitor := models.FindVisitorByVistorId(id) //各种通知 diff --git a/models/visitors.go b/models/visitors.go index 19f730f..63d0b76 100644 --- a/models/visitors.go +++ b/models/visitors.go @@ -11,9 +11,10 @@ type Visitor struct { Refer string `json:"refer"` City string `json:"city"` ClientIp string `json:"client_ip"` + Extra string `json:"extra"` } -func CreateVisitor(name string, avator string, sourceIp string, toId string, visitorId string, refer string, city string, clientIp string) { +func CreateVisitor(name, avator, sourceIp, toId, visitorId, refer, city, clientIp, extra string) { old := FindVisitorByVistorId(visitorId) if old.Name != "" { //更新状态上线 @@ -30,6 +31,7 @@ func CreateVisitor(name string, avator string, sourceIp string, toId string, vis Refer: refer, City: city, ClientIp: clientIp, + Extra: extra, } DB.Create(v) } diff --git a/router/api.go b/router/api.go index 8af7f4c..c04b609 100644 --- a/router/api.go +++ b/router/api.go @@ -69,7 +69,7 @@ func InitApiRouter(engine *gin.Engine) { engine.GET("/visitors_kefu_online", middleware.JwtApiMiddleware, controller.GetKefusVisitorOnlines) engine.GET("/clear_online_tcp", controller.DeleteOnlineTcp) engine.POST("/visitor_login", middleware.Ipblack, controller.PostVisitorLogin) - engine.POST("/visitor", controller.PostVisitor) + //engine.POST("/visitor", controller.PostVisitor) engine.GET("/visitor", middleware.JwtApiMiddleware, controller.GetVisitor) engine.GET("/visitors", middleware.JwtApiMiddleware, controller.GetVisitors) engine.GET("/statistics", middleware.JwtApiMiddleware, controller.GetStatistics) diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 92be31e..e70ba09 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -161,12 +161,6 @@ ClientIP:<{visitor.source_ip}> - - - - IP:<{visitor.client_ip}> - - 城市:<{visitor.city}> @@ -192,6 +186,12 @@ 状态:<{visitor.status}> + + + + <{v.key}>:<{v.val}> + + diff --git a/static/html/index.html b/static/html/index.html index 1d278ac..e4cfac8 100644 --- a/static/html/index.html +++ b/static/html/index.html @@ -25,7 +25,7 @@ GOFLY_URL:"", GOFLY_KEFU_ID: "kefu2", GOFLY_BTN_TEXT: "{{.OnlineChat}}", - GOFLY_LANG:"{{.Lang}}" + GOFLY_LANG:"{{.Lang}}", }) diff --git a/static/js/chat-main.js b/static/js/chat-main.js index 58d1f93..1b0d2cc 100644 --- a/static/js/chat-main.js +++ b/static/js/chat-main.js @@ -31,6 +31,7 @@ var app=new Vue({ source_ip:"", created_at:"", }, + visitorExtra:[], visitors:[], visitorCount:0, visitorCurrentPage:1, @@ -377,9 +378,18 @@ var app=new Vue({ // _this.visitor.client_ip=r.client_ip; // _this.visitor.source_ip=r.source_ip; _this.visitor.status=r.status==1?"在线":"离线"; + //_this.visitor.visitor_id=r.visitor_id; _this.chatTitle="#"+r.id+"|"+r.name; _this.chatTitleType="success"; + if(r.extra!=""){ + var extra=JSON.parse(b64ToUtf8(r.extra)); + for(var key in extra){ + var temp={key:key,val:extra[key]} + _this.visitorExtra.push(temp); + } + } + } if(data.code!=200){ _this.$message({ diff --git a/static/js/chat-page.js b/static/js/chat-page.js index d487678..04ee281 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -159,8 +159,9 @@ new Vue({ visitor_id=obj.visitor_id; } let _this=this; + var extra=getQuery("extra"); //发送消息 - $.post("/visitor_login",{visitor_id:visitor_id,refer:REFER,to_id:KEFU_ID,client_ip:'',},function(res){ + $.post("/visitor_login",{visitor_id:visitor_id,refer:REFER,to_id:KEFU_ID,extra:extra},function(res){ if(res.code!=200){ _this.$message({ message: res.msg, diff --git a/static/js/functions.js b/static/js/functions.js index 037cc26..5c8eb53 100644 --- a/static/js/functions.js +++ b/static/js/functions.js @@ -136,4 +136,10 @@ function getQuery(key) { } return ""; } +function utf8ToB64(str) { + return window.btoa(unescape(encodeURIComponent(str))); +} +function b64ToUtf8(str) { + return decodeURIComponent(escape(window.atob(str))); +} ; \ No newline at end of file diff --git a/static/js/gofly-front.js b/static/js/gofly-front.js index 8a2e8cf..371174c 100644 --- a/static/js/gofly-front.js +++ b/static/js/gofly-front.js @@ -3,6 +3,7 @@ var GOFLY={ GOFLY_KEFU_ID:"", GOFLY_BTN_TEXT:"Chat with me", GOFLY_LANG:"en", + GOFLY_EXTRA:"", }; GOFLY.launchButtonFlag=false; GOFLY.titleTimer=0; @@ -26,6 +27,9 @@ GOFLY.init=function(config){ if (typeof config.GOFLY_BTN_TEXT!="undefined"){ this.GOFLY_BTN_TEXT=config.GOFLY_BTN_TEXT; } + if (typeof config.GOFLY_EXTRA!="undefined"){ + this.GOFLY_EXTRA=config.GOFLY_EXTRA; + } this.dynamicLoadJs(this.GOFLY_URL+"/static/js/functions.js?v=1",function(){ if (typeof config.GOFLY_LANG!="undefined"){ @@ -33,6 +37,7 @@ GOFLY.init=function(config){ }else{ _this.GOFLY_LANG=checkLang(); } + _this.GOFLY_EXTRA=utf8ToB64(_this.GOFLY_EXTRA); }); if (typeof $!="function"){ @@ -187,7 +192,7 @@ GOFLY.layerOpen=function (){ area: ['520px', '530px'], offset: 'rb', //右下角弹出 anim: 2, - content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title, 'yes'], //iframe的url,no代表不显示滚动条 + content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA , 'yes'], //iframe的url,no代表不显示滚动条 end: function(){ _this.launchButtonFlag=false; $(".launchButtonBox").show(); @@ -195,7 +200,7 @@ GOFLY.layerOpen=function (){ }); } GOFLY.windowOpen=function (){ - window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title); + window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA); } GOFLY.flashTitle=function () { this.titleNum++;