客服终端只允许一个在线

master
taoshihan 3 years ago
parent df24344919
commit 9daff59ed7

@ -8,5 +8,5 @@ var (
Upload string = "static/upload/"
Dir string = "config/"
MysqlConf string = Dir + "mysql.json"
IsCompireTemplate bool = true //是否编译静态模板到二进制
IsCompireTemplate bool = false //是否编译静态模板到二进制
)

@ -114,8 +114,8 @@ func GetOtherKefuList(c *gin.Context) {
item["nickname"] = kefu.Nickname
item["avator"] = kefu.Avator
item["status"] = "offline"
kefus, ok := ws.KefuList[kefu.Name]
if ok && len(kefus) != 0 {
kefu, ok := ws.KefuList[kefu.Name]
if ok && kefu != nil {
item["status"] = "online"
}
result = append(result, item)

@ -113,9 +113,6 @@ func GetStatistics(c *gin.Context) {
message := models.CountMessage()
session := len(ws.ClientList)
kefuNum := 0
for _, kefus := range ws.KefuList {
kefuNum += len(kefus)
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",

@ -112,8 +112,8 @@ func SendMessageV2(c *gin.Context) {
//ws.KefuMessage(vistorInfo.VisitorId, content, kefuInfo)
go ws.SendServerJiang(vistorInfo.Name+"说", content, c.Request.Host)
go SendAppGetuiPush(kefuInfo.Name, vistorInfo.Name, content)
kefus, ok := ws.KefuList[kefuInfo.Name]
if !ok || len(kefus) == 0 {
kefu, ok := ws.KefuList[kefuInfo.Name]
if !ok || kefu == nil{
go SendNoticeEmail(content+"|"+vistorInfo.Name, content)
}
go ws.VisitorAutoReply(vistorInfo, kefuInfo, content)

@ -24,10 +24,8 @@ func GetNotice(c *gin.Context) {
result = append(result, h)
}
status := "online"
if kefus, ok := ws.KefuList[kefuId]; !ok {
if len(kefus) <= 0 {
if _, ok := ws.KefuList[kefuId]; !ok {
status = "offline"
}
}
c.JSON(200, gin.H{
"code": 200,

@ -12,6 +12,7 @@ var app=new Vue({
server:getWsBaseUrl()+"/ws_kefu?token="+localStorage.getItem("token"),
//server:getWsBaseUrl()+"/chat_server",
socket:null,
socketClosed:false,
messageContent:"",
currentGuest:"",
msgList:[],
@ -94,6 +95,14 @@ var app=new Vue({
OnMessage(e) {
const redata = JSON.parse(e.data);
switch (redata.type){
case "close":
this.$message({
message: "客服在其他地方登录,当前登录状态退出",
type: 'error'
});
localStorage.removeItem("token");
this.socket.close();
break;
case "inputing":
this.handleInputing(redata.data);
//this.sendKefuOnline();
@ -162,8 +171,6 @@ var app=new Vue({
//接手客户
talkTo(guestId,name) {
this.currentGuest = guestId;
//this.chatTitle=name+"|"+guestId+",正在处理中...";
//发送给客户
let mes = {}
mes.type = "kfConnect";

@ -42,7 +42,6 @@ func NewKefuServer(c *gin.Context) {
messageType, receive, err := conn.ReadMessage()
if err != nil {
log.Println(err)
go SendPingToKefuClient()
return
}
@ -55,53 +54,31 @@ func NewKefuServer(c *gin.Context) {
}
}
func AddKefuToList(kefu *User) {
var newKefuConns = []*User{kefu}
kefuConns := KefuList[kefu.Id]
if kefuConns != nil {
for _, otherKefu := range kefuConns {
msg := TypeMessage{
Type: "many pong",
}
str, _ := json.Marshal(msg)
err := otherKefu.Conn.WriteMessage(websocket.TextMessage, str)
if err == nil {
newKefuConns = append(newKefuConns, otherKefu)
}
oldUser, ok := KefuList[kefu.Id]
if oldUser != nil || ok {
msg := TypeMessage{
Type: "close",
Data: kefu.Id,
}
}
log.Println("xxxxxxxxxxxxxxxxxxxxxxxx", newKefuConns)
KefuList[kefu.Id] = newKefuConns
}
//给超管发消息
func SuperAdminMessage(str []byte) {
return
//给超管发
for _, kefuUsers := range KefuList {
for _, kefuUser := range kefuUsers {
if kefuUser.Role_id == "2" {
kefuUser.Conn.WriteMessage(websocket.TextMessage, str)
}
str, _ := json.Marshal(msg)
if err := oldUser.Conn.WriteMessage(websocket.TextMessage, str); err != nil {
oldUser.Conn.Close()
}
}
KefuList[kefu.Id] = kefu
}
//给指定客服发消息
func OneKefuMessage(toId string, str []byte) {
//新版
mKefuConns, ok := KefuList[toId]
if ok && len(mKefuConns) > 0 {
for _, kefu := range mKefuConns {
kefu, ok := KefuList[toId]
if ok{
log.Println("OneKefuMessage lock")
kefu.Mux.Lock()
defer kefu.Mux.Unlock()
log.Println("OneKefuMessage unlock")
error := kefu.Conn.WriteMessage(websocket.TextMessage, str)
tools.Logger().Println("send_kefu_message", error, string(str))
}
}
SuperAdminMessage(str)
}
func KefuMessage(visitorId, content string, kefuInfo models.User) {
msg := TypeMessage{
@ -126,22 +103,14 @@ func SendPingToKefuClient() {
Type: "many pong",
}
str, _ := json.Marshal(msg)
for kefuId, kfConns := range KefuList {
var newKefuConns = []*User{}
for _, kefuConn := range kfConns {
if kefuConn == nil {
continue
}
kefuConn.Mux.Lock()
defer kefuConn.Mux.Unlock()
err := kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
if err == nil {
newKefuConns = append(newKefuConns, kefuConn)
}
for kefuId, kefu := range KefuList {
if kefu == nil {
continue
}
if len(newKefuConns) > 0 {
KefuList[kefuId] = newKefuConns
} else {
kefu.Mux.Lock()
defer kefu.Mux.Unlock()
err := kefu.Conn.WriteMessage(websocket.TextMessage, str)
if err == nil {
delete(KefuList, kefuId)
}
}

@ -161,7 +161,7 @@ func VisitorMessage(visitorId, content string, kefuInfo models.User) {
visitor.Conn.WriteMessage(websocket.TextMessage, str)
}
func VisitorAutoReply(vistorInfo models.Visitor, kefuInfo models.User, content string) {
kefus, ok := KefuList[kefuInfo.Name]
kefu, ok := KefuList[kefuInfo.Name]
reply := models.FindReplyItemByUserIdTitle(kefuInfo.Name, content)
if reply.Content != "" {
time.Sleep(1 * time.Second)
@ -169,7 +169,7 @@ func VisitorAutoReply(vistorInfo models.Visitor, kefuInfo models.User, content s
KefuMessage(vistorInfo.VisitorId, reply.Content, kefuInfo)
models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, reply.Content, "kefu")
}
if !ok || len(kefus) == 0 {
if !ok || kefu == nil {
time.Sleep(1 * time.Second)
welcome := models.FindWelcomeByUserIdKey(kefuInfo.Name, "offline")
if welcome.Content == "" || reply.Content != "" {

@ -51,7 +51,7 @@ type ClientMessage struct {
}
var ClientList = make(map[string]*User)
var KefuList = make(map[string][]*User)
var KefuList = make(map[string]*User)
var message = make(chan *Message, 10)
var upgrader = websocket.Upgrader{}
var Mux sync.RWMutex

Loading…
Cancel
Save