kefu/controller/call.go

102 lines
2.6 KiB
Go
Raw Normal View History

2024-12-10 02:50:12 +00:00
package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"kefu/models"
"kefu/types"
"kefu/ws"
)
type CallKefuForm struct {
KefuId string `form:"kefu_id" json:"kefu_id" uri:"kefu_id" xml:"kefu_id" binding:"required"`
VisitorId string `form:"visitor_id" json:"visitor_id" uri:"visitor_id" xml:"visitor_id" binding:"required"`
Action string `form:"action" json:"action" uri:"action" xml:"action" binding:"required"`
}
//peerjs call客服
func PostCallKefuV2(c *gin.Context) {
var form CallKefuForm
err := c.Bind(&form)
if err != nil {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
"result": err.Error(),
})
return
}
kefuInfo := models.FindUser(form.KefuId)
vistorInfo := models.FindVisitorByVistorId(form.VisitorId)
if kefuInfo.ID == 0 || vistorInfo.ID == 0 {
c.JSON(200, gin.H{
"code": 400,
"msg": "用户不存在",
})
return
}
msg := ws.TypeMessage{
Type: form.Action,
Data: gin.H{
"name": vistorInfo.Name,
"visitor_id": vistorInfo.VisitorId,
},
}
str, _ := json.Marshal(msg)
ws.OneKefuMessage(kefuInfo.Name, str)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
//peerjs call访客
type CallVisitorForm struct {
PeerId string `form:"peer_id" json:"peer_id" uri:"peer_id" xml:"peer_id" binding:"required"`
VisitorId string `form:"visitor_id" json:"visitor_id" uri:"visitor_id" xml:"visitor_id" binding:"required"`
Action string `form:"action" json:"action" uri:"action" xml:"action" binding:"required"`
}
func PostCallVisitorV2(c *gin.Context) {
kefuName, _ := c.Get("kefu_name")
entId, _ := c.Get("ent_id")
var form CallVisitorForm
err := c.Bind(&form)
if err != nil {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
"result": err.Error(),
})
return
}
msg := ws.TypeMessage{
Type: form.Action,
Data: form.PeerId,
}
str, _ := json.Marshal(msg)
visitor, ok := ws.ClientList[form.VisitorId]
if !ok || visitor.Name == "" || kefuName.(string) != visitor.ToId {
c.JSON(200, gin.H{
"code": 400,
"msg": "访客不存在或不在线",
"result": "",
})
return
}
visitor.Conn.WriteMessage(websocket.TextMessage, str)
if form.Action == "refuse" {
content := "🚫 refuse"
models.CreateMessage(kefuName.(string), form.VisitorId, content, "kefu", entId.(string), "unread")
ws.VisitorMessage(form.VisitorId, content, models.FindUser(kefuName.(string)))
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}