存储访客数据

pull/30/head
陶士涵 4 years ago
parent 4abe35cdec
commit b2cb6bd1ae

@ -8,4 +8,18 @@ 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 AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `visitor` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '',
`avator` varchar(500) NOT NULL DEFAULT '',
`source_ip` varchar(50) NOT NULL DEFAULT '',
`to_id` varchar(50) 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,
`visitor_id` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `visitor_id` (`visitor_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

@ -0,0 +1,7 @@
{
"Server":"127.0.0.1",
"Port":"3306",
"Database":"go-fly",
"Username":"go-fly",
"Password":"go-fly"
}

@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/models"
"log"
"time"
)
@ -15,6 +16,7 @@ type vistor struct{
}
type Message struct{
conn *websocket.Conn
ip string
content []byte
}
var clientList = make(map[string]*vistor)
@ -25,6 +27,15 @@ type TypeMessage struct {
Type interface{} `json:"type"`
Data interface{} `json:"data"`
}
type ClientMessage struct {
Name string `json:"name"`
Avator string `json:"avator"`
Id string `json:"id"`
Group string `json:"group"`
Time string `json:"time"`
ToId string `json:"to_id"`
Content string `json:"content"`
}
type KfMessage struct {
Kf_name string `json:"kf_name"`
Avatar string `json:"avatar"`
@ -73,25 +84,26 @@ func NewChatServer(c *gin.Context){
message<-&Message{
conn:conn,
content: receive,
ip:c.ClientIP(),
}
}
}
//发送给客户客服上线
func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
func SendKefuOnline(clientMsg ClientMessage, conn *websocket.Conn) {
sendMsg := TypeMessage{
Type: "kfOnline",
Data: KfMessage{
Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id,
Kf_group: kfMsg.Kf_group,
Data: ClientMessage{
Name: clientMsg.Name,
Avator: clientMsg.Avator,
Id: clientMsg.Id,
Group: clientMsg.Group,
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: "客服上线",
},
}
jsonStrByte, _ := json.Marshal(sendMsg)
conn.WriteMessage(1,jsonStrByte)
conn.WriteMessage(websocket.TextMessage,jsonStrByte)
}
//定时给客户端发送消息判断客户端是否在线
@ -150,8 +162,7 @@ func singleBroadcaster(){
for {
message:=<-message
var typeMsg TypeMessage
var kfMsg KfMessage
var userMsg UserMessage
var clientMsg ClientMessage
json.Unmarshal(message.content, &typeMsg)
conn:=message.conn
if typeMsg.Type == nil || typeMsg.Data == nil {
@ -165,21 +176,23 @@ func singleBroadcaster(){
getOnlineUser(conn)
//用户上线
case "userInit":
json.Unmarshal(msgData, &userMsg)
json.Unmarshal(msgData, &clientMsg)
//用户id对应的连接
user:=&vistor{
conn:conn,
name: userMsg.From_name,
avator: userMsg.From_avatar,
id:userMsg.From_id,
name: clientMsg.Name,
avator: clientMsg.Avator,
id:clientMsg.Id,
}
clientList[userMsg.From_id] = user
clientList[clientMsg.Id] = user
//插入数据表
models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.ip,clientMsg.ToId,clientMsg.Id)
SendNoticeToAllKefu()
//客服上线
case "kfOnline":
json.Unmarshal(msgData, &kfMsg)
json.Unmarshal(msgData, &clientMsg)
//客服id对应的连接
kefuList[kfMsg.Kf_id] = conn
kefuList[clientMsg.Id] = conn
//发送给客户
if len(clientList) == 0 {
break
@ -191,12 +204,12 @@ func singleBroadcaster(){
//SendOnekfuAllNotice(w)
//客服接手
case "kfConnect":
json.Unmarshal(msgData, &kfMsg)
kefuList[kfMsg.Kf_id] = conn
SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id].conn)
json.Unmarshal(msgData, &clientMsg)
kefuList[clientMsg.Id] = conn
SendKefuOnline(clientMsg, clientList[clientMsg.ToId].conn)
case "kfChatMessage":
json.Unmarshal(msgData, &kfMsg)
guest,ok:=clientList[kfMsg.Guest_id]
json.Unmarshal(msgData, &clientMsg)
guest,ok:=clientList[clientMsg.ToId]
if guest==nil||!ok{
return
}
@ -204,29 +217,31 @@ func singleBroadcaster(){
msg := TypeMessage{
Type: "kfChatMessage",
Data: KfMessage{
Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id,
Data: ClientMessage{
Name: clientMsg.Name,
Avator: clientMsg.Avator,
Id: clientMsg.Id,
Time: time.Now().Format("2006-01-02 15:04:05"),
Guest_id: kfMsg.Guest_id,
Content: kfMsg.Content,
ToId: clientMsg.ToId,
Content: clientMsg.Content,
},
}
str, _ := json.Marshal(msg)
conn.WriteMessage(websocket.TextMessage,str)
case "chatMessage":
json.Unmarshal(msgData, &userMsg)
conn := kefuList[userMsg.To_id]
json.Unmarshal(msgData, &clientMsg)
conn,ok := kefuList[clientMsg.ToId]
if conn==nil||!ok{
return
}
msg := TypeMessage{
Type: "chatMessage",
Data: UserMessage{
From_avatar: userMsg.From_avatar,
From_id: userMsg.From_id,
From_name: userMsg.From_name,
To_id: userMsg.To_id,
To_name: userMsg.To_name,
Content: userMsg.Content,
Data: ClientMessage{
Avator: clientMsg.Avator,
Id: clientMsg.Id,
Name: clientMsg.Name,
ToId: clientMsg.ToId,
Content: clientMsg.Content,
Time: time.Now().Format("2006-01-02 15:04:05"),
},
}

@ -9,8 +9,8 @@ func GetKefuInfo(c *gin.Context){
kefuId, _ := c.Get("kefu_id")
user:=models.FindUserById(kefuId)
info:=make(map[string]interface{})
info["kf_name"]=user.Nickname
info["kf_id"]=user.Name
info["name"]=user.Nickname
info["id"]=user.Name
info["avator"]=user.Avator
c.JSON(200, gin.H{
"code": 200,

@ -0,0 +1,31 @@
package models
import "github.com/jinzhu/gorm"
type Visitor struct {
gorm.Model
Name string
Avator string
SourceIp string
ToId string
VisitorId string
}
func CreateVisitor(name string,avator string,sourceIp string,toId string,visitorId string){
old:=FindVisitorByVistorId(visitorId)
if old.Name!=""{
return
}
v:=&Visitor{
Name:name,
Avator: avator,
SourceIp:sourceIp,
ToId:toId,
VisitorId: visitorId,
}
DB.Create(v)
}
func FindVisitorByVistorId(visitorId string)Visitor{
var v Visitor
DB.Where("visitor_id = ?", visitorId).First(&v)
return v
}

@ -122,10 +122,10 @@
msgListUser:[],
chatTitle:"暂时未处理咨询",
kfConfig:{
kf_id : "kf_1",
kf_name : "客服丽丽",
id : "kf_1",
name : "客服丽丽",
avatar : "",
guest_id : "",
to_id : "",
}
},
methods: {
@ -225,13 +225,13 @@
//发送给客户
let mes = {}
mes.type = "kfConnect";
this.kfConfig.guest_id=guestId;
this.kfConfig.to_id=guestId;
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
},
//发送给客户
chatToUser() {
if(this.messageContent==""||this.guest_id==""){
if(this.messageContent==""||this.currentGuest==""){
return;
}
let mes = {};
@ -243,7 +243,7 @@
let content = {}
content.avatar = this.kfConfig.avatar;
content.name = this.kfConfig.kf_name;
content.name = this.kfConfig.name;
content.content = this.kfConfig.content;
content.is_kefu = true;
content.time = '';
@ -280,8 +280,8 @@
},
success: function(data) {
if(data.result!=null){
_this.kfConfig.kf_id=data.result.kf_id;
_this.kfConfig.kf_name=data.result.kf_name;
_this.kfConfig.id=data.result.id;
_this.kfConfig.name=data.result.name;
_this.kfConfig.avatar=data.result.avator;
}
if(data.code!=200){

@ -101,10 +101,10 @@
<script src="https://cdn.bootcss.com/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js"></script>
<script>
var guest={};
guest.from_id = "";
guest.from_name = typeof(returnCitySN)!="undefined" ?returnCitySN["cip"]+"-"+returnCitySN["cname"]:"小米";
guest.from_avatar = "https://dss2.bdstatic.com/6Ot1bjeh1BF3odCf/it/u=1539628792,3031847515&fm=85&app=92&f=JPEG?w=121&h=75&s=65F03DC608BA199ED70C5D3703009040";
guest.from_group = "1";
guest.id = "";
guest.name = typeof(returnCitySN)!="undefined" ?returnCitySN["cip"]+"-"+returnCitySN["cname"]:"小米";
guest.avator = "https://dss2.bdstatic.com/6Ot1bjeh1BF3odCf/it/u=1539628792,3031847515&fm=85&app=92&f=JPEG?w=121&h=75&s=65F03DC608BA199ED70C5D3703009040";
guest.group = "1";
guest.to_id="";
new Vue({
@ -140,12 +140,12 @@
const redata = JSON.parse(e.data);
if (redata.type == "kfOnline") {
let msg = redata.data
guest.to_id=msg.kf_id;
this.chatTitle=msg.kf_name+",正在与您沟通!"
guest.to_id=msg.id;
this.chatTitle=msg.name+",正在与您沟通!"
}
if (redata.type == "kfChatMessage") {
let msg = redata.data
guest.to_id=msg.kf_id;
guest.to_id=msg.id;
let content = {}
content.avatar = msg.avatar;
@ -186,9 +186,9 @@
this.messageContent = "";
let content = {}
content.avatar=guest.from_avatar;
content.avatar=guest.avator;
content.content = guest.content;
content.name = guest.from_name;
content.name = guest.name;
content.is_kefu = true;
content.time = this.getNowDate();
this.msgList.push(content);
@ -202,7 +202,7 @@
getUserInfo(){
let obj=this.getCache("guest");
if(!obj){
guest.from_id=this.generateUUID();
guest.id=this.generateUUID();
this.setCache("guest",guest);
obj=this.getCache("guest");
}

Loading…
Cancel
Save