记录聊天记录

pull/30/head
陶士涵 4 years ago
parent 0e4feb18b1
commit 50a8616b8b

@ -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

@ -1,2 +1,4 @@
p, admin, /mysql, GET
p, admin, /mysql, POST
p, admin, /mysql, POST
p, admin, /kefuinfo,POST
p, admin, /kefuinfo,DELETE
1 p admin /mysql GET
2 p admin /mysql POST
3 p admin /kefuinfo POST
4 p admin /kefuinfo DELETE

@ -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")
}
}
}

@ -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,
})
}

@ -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
}

@ -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)

@ -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<retData.length;i++){
this.usersMap[retData[i].uid]=retData[i].username;
if (typeof (this.msgListUser[retData[i].uid]) == "undefined") {
this.msgListUser[retData[i].uid] = [];
}
}
for(let i=0;i<this.visitors.length;i++){
let vid=this.visitors[i].visitor_id;
@ -349,7 +326,45 @@
});
}
}
}).then(function(data){
});
},
//获取信息列表
getMesssagesByVisitorId(visitorId){
let _this=this;
$.ajax({
type:"get",
url:"/messages?visitorId="+visitorId,
headers:{
"token":localStorage.getItem("token")
},
success: function(data) {
if(data.code==200 && data.result!=null){
let msgList=data.result;
_this.msgList=[];
for(let i=0;i<msgList.length;i++){
let visitorMes=msgList[i];
let content = {}
if(visitorMes["mes_type"]=="kefu"){
content.is_kefu = true;
content.avator = visitorMes["kefu_avator"];
content.name = visitorMes["kefu_name"];
}else{
content.is_kefu = false;
content.avator = visitorMes["visitor_avator"];
content.name = visitorMes["visitor_name"];
}
content.content = visitorMes["content"];
_this.msgList.push(content);
_this.scrollBottom();
}
}
if(data.code!=200){
_this.$message({
message: data.msg,
type: 'error'
});
}
}
});
},
//获取客服信息
@ -403,6 +418,12 @@
});
}
},
//滚到底部
scrollBottom(){
this.$nextTick(() => {
$('.chatBox').scrollTop($(".chatBox")[0].scrollHeight);
});
}
},
created: function () {
this.getKefuInfo();

@ -284,6 +284,7 @@
content.time = msg.time;
setTimeout(function () {
_this.msgList.push(content);
_this.scrollBottom();
}, 2000);
;
}

@ -3,7 +3,7 @@
default-active="3"
mode="horizontal">
<el-menu-item class="mainLogo" v-on:click="openUrl('/login')">GO-FLY</el-menu-item>
<el-menu-item index="2" v-on:click="openIframeUrl('/list')">邮箱<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
<el-menu-item style="display:none" index="2" v-on:click="openIframeUrl('/list')">邮箱<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
</el-menu-item>
<el-menu-item index="3" v-on:click="openIframeUrl('/chat_main')">聊天</el-menu-item>
<el-menu-item index="4" v-on:click="openIframeUrl('/setting')">设置</el-menu-item>

@ -6,7 +6,7 @@
<span>账户设置</span>
</template>
<el-menu-item-group>
<el-menu-item index="1-1" v-on:click="">智能回复</el-menu-item>
<el-menu-item style="display:none" index="1-1" v-on:click="">智能回复</el-menu-item>
<el-menu-item index="1-2" v-on:click="openUrl('/setting_kefu_list')">客服管理</el-menu-item>
</el-menu-item-group>
</el-submenu>
@ -16,9 +16,9 @@
<span>系统设置</span>
</template>
<el-menu-item-group>
<el-menu-item index="2-1">设置smtp</el-menu-item>
<el-menu-item index="2-2">设置imap</el-menu-item>
<el-menu-item index="2-3" v-on:click="openUrl('/setting')">设置登陆账号</el-menu-item>
<el-menu-item style="display:none" index="2-1">设置smtp</el-menu-item>
<el-menu-item style="display:none" index="2-2">设置imap</el-menu-item>
<el-menu-item style="display:none" index="2-3" v-on:click="openUrl('/setting')">设置登陆账号</el-menu-item>
<el-menu-item index="2-4" v-on:click="openUrl('/setting_mysql')">设置mysql</el-menu-item>
</el-menu-item-group>
</el-submenu>

Loading…
Cancel
Save