客服取自数据库

pull/30/head
陶士涵 4 years ago
parent 059a9fdb56
commit bfd681f757

@ -6,5 +6,6 @@ CREATE TABLE `user` (
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` timestamp NULL DEFAULT NULL,
`avator` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

@ -4,31 +4,15 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"log"
"net/http"
"time"
)
//聊天主界面
func ActionChatMain(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_main", nil)
}
//聊天客户端界面
func ActionChatPage(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_page", nil)
}
//咨询界面
func PageChat(c *gin.Context) {
html := tools.FileGetContent("html/chat_page.html")
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, html)
}
var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string)
var kefuList = make(map[string]*websocket.Conn)
//获取在线用户
func ChatUsers(w http.ResponseWriter, r *http.Request) {
@ -362,6 +346,4 @@ func getOnlineUser(w *websocket.Conn,messageType int) {
var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string)
var kefuList = make(map[string]*websocket.Conn)

@ -0,0 +1,20 @@
package controller
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/models"
)
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["avator"]=user.Avator
c.JSON(200, gin.H{
"code": 200,
"msg": "获取成功",
"result":info,
})
}

@ -45,8 +45,10 @@ func LoginCheckPass(c *gin.Context) {
return
}
userinfo["name"] = info.Name
userinfo["kefu_id"] = info.ID
userinfo["type"] = "kefu"
userinfo["create_time"] = time.Now().Unix()
token, _ := tools.MakeToken(userinfo)
userinfo["ref_token"]=true
refToken, _ := tools.MakeToken(userinfo)

@ -3,6 +3,7 @@ package middleware
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools"
"log"
"time"
)
func JwtPageMiddleware(c *gin.Context){
@ -17,6 +18,7 @@ func JwtPageMiddleware(c *gin.Context){
func JwtApiMiddleware(c *gin.Context){
token := c.GetHeader("token")
userinfo := tools.ParseToken(token)
log.Println(userinfo)
if userinfo == nil||userinfo["name"]==nil {
c.JSON(200, gin.H{
"code": 400,
@ -35,4 +37,7 @@ func JwtApiMiddleware(c *gin.Context){
c.Abort()
}
c.Set("user",userinfo["name"])
if userinfo["type"]=="kefu"{
c.Set("kefu_id",userinfo["kefu_id"])
}
}

@ -6,9 +6,10 @@ import (
)
type User struct {
gorm.Model
Id int64
Name string
Password string
Nickname string
Avator string
}
func CreateUser(name string,password string){
user:=&User{
@ -21,4 +22,9 @@ func FindUser(username string)User{
var user User
DB.Where("name = ?", username).First(&user)
return user
}
func FindUserById(id interface{})User{
var user User
DB.Where("id = ?", id).First(&user)
return user
}

@ -31,6 +31,8 @@ func main() {
engine.POST("/check_auth",middleware.JwtApiMiddleware, controller.MainCheckAuth)
//前后聊天
engine.GET("/chat_server", controller.NewChatServer)
//获取客服信息
engine.GET("/kefuinfo",middleware.JwtApiMiddleware, controller.GetKefuInfo)
//设置页
engine.GET("/setting", tmpl.PageSetting)
//设置mysql
@ -61,13 +63,8 @@ func main() {
mux.HandleFunc("/setting_account", controller.SettingAccount)
//发送邮件接口
mux.HandleFunc("/send", controller.FolderSend)
//聊天界面
mux.HandleFunc("/chat_main", controller.ActionChatMain)
//新邮件提醒服务
mux.HandleFunc("/push_mail", controller.PushMailServer)
//聊天界面
mux.HandleFunc("/chat_page", controller.ActionChatPage)
//聊天服务
//mux.Handle("/chat_server", websocket.Handler(controller.ChatServer))
//获取在线用户
mux.HandleFunc("/chat_users", controller.ChatUsers)

@ -95,7 +95,7 @@
<el-menu class="chatLeft">
<el-menu-item>
<i class="el-icon-s-tools"></i>
<span slot="title">常用工具</span>
<span slot="title">访客信息</span>
</el-menu-item>
</el-menu>
@ -105,12 +105,7 @@
</div>
</body>
<script>
var kfConfig={};
kfConfig.kf_id = "kf_1";
kfConfig.kf_name = "客服丽丽";
kfConfig.avatar = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4217138672,2588039002&fm=26&gp=0.jpg";
kfConfig.kf_group = "1";
kfConfig.guest_id = "";
var app=new Vue({
el: '#app',
@ -126,6 +121,12 @@
msgList:[],
msgListUser:[],
chatTitle:"暂时未处理咨询",
kfConfig:{
kf_id : "kf_1",
kf_name : "客服丽丽",
avatar : "",
guest_id : "",
}
},
methods: {
//跳转
@ -135,13 +136,13 @@
getOnlineUsers() {
let mes = {}
mes.type = "getOnlineUsers";
mes.data = kfConfig;
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
},
sendKefuOnline(){
let mes = {}
mes.type = "kfOnline";
mes.data = kfConfig;
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
},
//初始化websocket
@ -224,26 +225,26 @@
//发送给客户
let mes = {}
mes.type = "kfConnect";
kfConfig.guest_id=guestId;
mes.data = kfConfig;
this.kfConfig.guest_id=guestId;
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
},
//发送给客户
chatToUser() {
if(this.messageContent==""||kfConfig.guest_id==""){
if(this.messageContent==""||this.guest_id==""){
return;
}
let mes = {};
mes.type = "kfChatMessage";
kfConfig.content = this.messageContent;
mes.data = kfConfig;
this.kfConfig.content = this.messageContent;
mes.data = this.kfConfig;
this.socket.send(JSON.stringify(mes));
this.messageContent = "";
let content = {}
content.avatar = kfConfig.avatar;
content.name = kfConfig.kf_name;
content.content = kfConfig.content;
content.avatar = this.kfConfig.avatar;
content.name = this.kfConfig.kf_name;
content.content = this.kfConfig.content;
content.is_kefu = true;
content.time = '';
this.msgList.push(content);
@ -268,9 +269,35 @@
}
}
},
//获取客服信息
getKefuInfo(){
let _this=this;
$.ajax({
type:"get",
url:"/kefuinfo",
headers:{
"token":localStorage.getItem("token")
},
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.avatar=data.result.avator;
}
if(data.code!=200){
_this.$message({
message: data.msg,
type: 'error'
});
}
}
}).then(function(data){
_this.initConn();
});
},
},
created: function () {
this.initConn();
this.getKefuInfo();
}
})

Loading…
Cancel
Save