实现客服转接功能

pull/23/head
taoshihan1991 4 years ago
parent 12b1c5096a
commit fba199b6ff

@ -34,6 +34,7 @@ func GetOtherKefuList(c *gin.Context) {
idStr, _ := c.Get("kefu_id")
id := idStr.(float64)
result := make([]interface{}, 0)
ws.SendPingToKefuClient()
kefus := models.FindUsers()
for _, kefu := range kefus {
if uint(id) == kefu.ID {
@ -42,6 +43,7 @@ func GetOtherKefuList(c *gin.Context) {
item := make(map[string]interface{})
item["name"] = kefu.Name
item["nickname"] = kefu.Nickname
item["avator"] = kefu.Avator
item["status"] = "offline"
kefus, ok := ws.KefuList[kefu.Name]
@ -56,6 +58,29 @@ func GetOtherKefuList(c *gin.Context) {
"result": result,
})
}
func PostTransKefu(c *gin.Context) {
kefuId := c.Query("kefu_id")
visitorId := c.Query("visitor_id")
curKefuId, _ := c.Get("kefu_name")
user := models.FindUser(kefuId)
visitor := models.FindVisitorByVistorId(visitorId)
if user.Name == "" || visitor.Name == "" {
c.JSON(200, gin.H{
"code": 400,
"msg": "访客或客服不存在",
})
return
}
models.UpdateVisitorKefu(visitorId, kefuId)
ws.UpdateVisitorUser(visitorId, kefuId)
go ws.VisitorOnline(kefuId, visitor)
go ws.VisitorOffline(curKefuId.(string), visitor.VisitorId, visitor.Name)
go ws.VisitorNotice(visitor.VisitorId, "客服转接到"+user.Nickname)
c.JSON(200, gin.H{
"code": 200,
"msg": "转移成功",
})
}
func GetKefuInfoSetting(c *gin.Context) {
kefuId := c.Query("kefu_id")
user := models.FindUserById(kefuId)

@ -148,6 +148,7 @@ func SendMessageV2(c *gin.Context) {
var msg TypeMessage
if cType == "kefu" {
guest, ok := ws.ClientList[vistorInfo.VisitorId]
if guest != nil && ok {
conn := guest.Conn

@ -74,6 +74,10 @@ func UpdateVisitor(visitorId string, status uint, clientIp string, sourceIp stri
}
DB.Model(visitor).Where("visitor_id = ?", visitorId).Update(visitor)
}
func UpdateVisitorKefu(visitorId string, kefuId string) {
visitor := Visitor{}
DB.Model(&visitor).Where("visitor_id = ?", visitorId).Update("to_id", kefuId)
}
//查询条数
func CountVisitors() uint {

@ -50,6 +50,7 @@ func InitApiRouter(engine *gin.Engine) {
engine.DELETE("/kefuinfo", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.DeleteKefuInfo)
engine.GET("/kefulist", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.GetKefuList)
engine.GET("/other_kefulist", middleware.JwtApiMiddleware, controller.GetOtherKefuList)
engine.GET("/trans_kefu", middleware.JwtApiMiddleware, controller.PostTransKefu)
//角色列表
engine.GET("/roles", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.GetRoleList)
engine.POST("/role", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.PostRole)

@ -224,7 +224,7 @@
:data="otherKefus"
style="width: 100%">
<el-table-column
prop="name"
prop="nickname"
label="客服">
</el-table-column>
<el-table-column

@ -606,9 +606,10 @@ var app=new Vue({
//转移访客客服
transKefuVisitor(kefu,visitorId){
var _this=this;
// this.sendAjax("/other_kefulist","get",{},function(result){
// _this.otherKefus=result;
// });
this.sendAjax("/trans_kefu","get",{kefu_id:kefu,visitor_id:visitorId},function(result){
//_this.otherKefus=result;
_this.transKefuDialog = false
});
},
sendAjax(url,method,params,callback){
let _this=this;

@ -100,6 +100,7 @@ func kefuServerBackend() {
//给超管发消息
func SuperAdminMessage(str []byte) {
return
//给超管发
for _, kefuUsers := range KefuList {
for _, kefuUser := range kefuUsers {
@ -121,3 +122,28 @@ func OneKefuMessage(toId string, str []byte) {
}
SuperAdminMessage(str)
}
//给客服客户端发送消息判断客户端是否在线
func SendPingToKefuClient() {
msg := TypeMessage{
Type: "many pong",
}
str, _ := json.Marshal(msg)
for kefuId, kfConns := range KefuList {
var newKefuConns = []*User{}
for _, kefuConn := range kfConns {
if kefuConn == nil {
continue
}
err := kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
if err == nil {
newKefuConns = append(newKefuConns, kefuConn)
}
}
if len(newKefuConns) > 0 {
KefuList[kefuId] = newKefuConns
} else {
delete(KefuList, kefuId)
}
}
}

@ -3,6 +3,7 @@ package ws
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/models"
"log"
)
@ -40,21 +41,11 @@ func NewVisitorServer(c *gin.Context) {
var receive []byte
messageType, receive, err := conn.ReadMessage()
if err != nil {
for uid, visitor := range ClientList {
for _, visitor := range ClientList {
if visitor.Conn == conn {
log.Println("删除用户", uid)
delete(ClientList, uid)
models.UpdateVisitorStatus(uid, 0)
userInfo := make(map[string]string)
userInfo["uid"] = uid
userInfo["name"] = visitor.Name
msg := TypeMessage{
Type: "userOffline",
Data: userInfo,
}
str, _ := json.Marshal(msg)
//新版
OneKefuMessage(user.To_id, str)
log.Println("删除用户", visitor.Id)
delete(ClientList, visitor.Id)
VisitorOffline(visitor.To_id, visitor.Id, visitor.Name)
}
}
log.Println(err)
@ -90,3 +81,43 @@ func AddVisitorToList(user *User) {
//新版
OneKefuMessage(user.To_id, str)
}
func VisitorOnline(kefuId string, visitor models.Visitor) {
lastMessage := models.FindLastMessageByVisitorId(visitor.VisitorId)
userInfo := make(map[string]string)
userInfo["uid"] = visitor.VisitorId
userInfo["username"] = visitor.Name
userInfo["avator"] = visitor.Avator
userInfo["last_message"] = lastMessage.Content
if userInfo["last_message"] == "" {
userInfo["last_message"] = "新访客"
}
msg := TypeMessage{
Type: "userOnline",
Data: userInfo,
}
str, _ := json.Marshal(msg)
OneKefuMessage(kefuId, str)
}
func VisitorOffline(kefuId string, visitorId string, visitorName string) {
models.UpdateVisitorStatus(visitorId, 0)
userInfo := make(map[string]string)
userInfo["uid"] = visitorId
userInfo["name"] = visitorName
msg := TypeMessage{
Type: "userOffline",
Data: userInfo,
}
str, _ := json.Marshal(msg)
//新版
OneKefuMessage(kefuId, str)
}
func VisitorNotice(visitorId string, notice string) {
msg := TypeMessage{
Type: "notice",
Data: notice,
}
str, _ := json.Marshal(msg)
visitor := ClientList[visitorId]
visitor.Conn.WriteMessage(websocket.TextMessage, str)
}

@ -123,3 +123,7 @@ func WsServerBackend() {
}
}
func UpdateVisitorUser(visitorId string, toId string) {
guest, _ := ClientList[visitorId]
guest.To_id = toId
}

Loading…
Cancel
Save