diff --git a/controller/message.go b/controller/message.go index b5e95e7..79ea62e 100644 --- a/controller/message.go +++ b/controller/message.go @@ -162,21 +162,25 @@ func SendMessageV2(c *gin.Context) { if guest != nil && ok { ws.VisitorMessage(vistorInfo.VisitorId, content, kefuInfo) } - - msg = TypeMessage{ - Type: "message", - Data: ws.ClientMessage{ - Name: kefuInfo.Nickname, - Avator: kefuInfo.Avator, - Id: vistorInfo.VisitorId, - Time: time.Now().Format("2006-01-02 15:04:05"), - ToId: vistorInfo.VisitorId, - Content: content, - IsKefu: "yes", - }, - } - str2, _ := json.Marshal(msg) - ws.OneKefuMessage(kefuInfo.Name, str2) + ws.KefuMessage(vistorInfo.VisitorId, content, kefuInfo) + //msg = TypeMessage{ + // Type: "message", + // Data: ws.ClientMessage{ + // Name: kefuInfo.Nickname, + // Avator: kefuInfo.Avator, + // Id: vistorInfo.VisitorId, + // Time: time.Now().Format("2006-01-02 15:04:05"), + // ToId: vistorInfo.VisitorId, + // Content: content, + // IsKefu: "yes", + // }, + //} + //str2, _ := json.Marshal(msg) + //ws.OneKefuMessage(kefuInfo.Name, str2) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) } if cType == "visitor" { //kefuConns, ok := ws.KefuList[kefuInfo.Name] @@ -203,26 +207,19 @@ func SendMessageV2(c *gin.Context) { ws.OneKefuMessage(kefuInfo.Name, str) go ws.SendServerJiang(vistorInfo.Name+"说", content, c.Request.Host) go SendAppGetuiPush(kefuInfo.Name, vistorInfo.Name, content) + go ws.VisitorAutoReply(vistorInfo, kefuInfo, content) + kefus, ok := ws.KefuList[kefuInfo.Name] + if !ok || len(kefus) == 0 { + log.Println("客服不在线,发送邮件通知") + go SendNoticeEmail(content+"|"+vistorInfo.Name, content) + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result": msg, + }) } - c.JSON(200, gin.H{ - "code": 200, - "msg": "ok", - "result": msg, - }) - kefus, ok := ws.KefuList[kefuInfo.Name] - if !ok || len(kefus) == 0 { - log.Println("客服不在线,发送邮件通知") - go SendNoticeEmail(content+"|"+vistorInfo.Name, content) - go func() { - time.Sleep(1 * time.Second) - welcome := models.FindWelcomeByUserIdKey(kefuInfo.Name, "offline") - if welcome.Content == "" { - return - } - ws.VisitorMessage(vistorInfo.VisitorId, welcome.Content, kefuInfo) - models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, welcome.Content, "kefu") - }() - } + } func SendVisitorNotice(c *gin.Context) { notice := c.Query("msg") diff --git a/models/replys.go b/models/replys.go index 103e76b..703553c 100644 --- a/models/replys.go +++ b/models/replys.go @@ -14,6 +14,11 @@ type ReplyGroup struct { Items []*ReplyItem `json:"items";"` } +func FindReplyItemByUserIdTitle(userId interface{}, title string) ReplyItem { + var reply ReplyItem + DB.Where("user_id = ? and item_name = ?", userId, title).Find(&reply) + return reply +} func FindReplyByUserId(userId interface{}) []*ReplyGroup { var replyGroups []*ReplyGroup //DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroups) diff --git a/ws/user.go b/ws/user.go index 39348dd..1aef812 100644 --- a/ws/user.go +++ b/ws/user.go @@ -7,6 +7,7 @@ import ( "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "log" + "time" ) func NewKefuServer(c *gin.Context) { @@ -102,6 +103,22 @@ func OneKefuMessage(toId string, str []byte) { SuperAdminMessage(str) } +func KefuMessage(visitorId, content string, kefuInfo models.User) { + msg := TypeMessage{ + Type: "message", + Data: ClientMessage{ + Name: kefuInfo.Nickname, + Avator: kefuInfo.Avator, + Id: visitorId, + Time: time.Now().Format("2006-01-02 15:04:05"), + ToId: visitorId, + Content: content, + IsKefu: "yes", + }, + } + str, _ := json.Marshal(msg) + OneKefuMessage(kefuInfo.Name, str) +} //给客服客户端发送消息判断客户端是否在线 func SendPingToKefuClient() { diff --git a/ws/visitor.go b/ws/visitor.go index 05f0c5b..6bcd32a 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -151,3 +151,22 @@ func VisitorMessage(visitorId, content string, kefuInfo models.User) { visitor := ClientList[visitorId] visitor.Conn.WriteMessage(websocket.TextMessage, str) } +func VisitorAutoReply(vistorInfo models.Visitor, kefuInfo models.User, content string) { + kefus, ok := KefuList[kefuInfo.Name] + reply := models.FindReplyItemByUserIdTitle(kefuInfo.Name, content) + if reply.Content != "" { + time.Sleep(2 * time.Second) + VisitorMessage(vistorInfo.VisitorId, reply.Content, kefuInfo) + KefuMessage(vistorInfo.VisitorId, reply.Content, kefuInfo) + models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, reply.Content, "kefu") + } + if !ok || len(kefus) == 0 { + time.Sleep(1 * time.Second) + welcome := models.FindWelcomeByUserIdKey(kefuInfo.Name, "offline") + if welcome.Content == "" || reply.Content != "" { + return + } + VisitorMessage(vistorInfo.VisitorId, welcome.Content, kefuInfo) + models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, welcome.Content, "kefu") + } +}