增加群发通知http接口

pull/23/head
陶士涵 5 years ago
parent e77562d6d1
commit 757a6c7c70

@ -119,6 +119,15 @@ func SendKefuOnline(clientMsg ClientMessage, conn *websocket.Conn) {
jsonStrByte, _ := json.Marshal(sendMsg) jsonStrByte, _ := json.Marshal(sendMsg)
conn.WriteMessage(websocket.TextMessage,jsonStrByte) conn.WriteMessage(websocket.TextMessage,jsonStrByte)
} }
//发送通知
func SendNotice(msg string, conn *websocket.Conn) {
sendMsg := TypeMessage{
Type: "notice",
Data: msg,
}
jsonStrByte, _ := json.Marshal(sendMsg)
conn.WriteMessage(websocket.TextMessage,jsonStrByte)
}
//定时给客户端发送消息判断客户端是否在线 //定时给客户端发送消息判断客户端是否在线
func sendPingToClient() { func sendPingToClient() {
@ -231,6 +240,10 @@ func singleBroadcaster(){
//用户上线 //用户上线
case "userInit": case "userInit":
json.Unmarshal(msgData, &clientMsg) json.Unmarshal(msgData, &clientMsg)
vistorInfo:=models.FindVisitorByVistorId(clientMsg.VisitorId)
if vistorInfo.VisitorId==""{
SendNotice("访客数据不存在",conn)
}
//用户id对应的连接 //用户id对应的连接
user:=&vistor{ user:=&vistor{
conn:conn, conn:conn,
@ -241,7 +254,8 @@ func singleBroadcaster(){
} }
clientList[clientMsg.VisitorId] = user clientList[clientMsg.VisitorId] = user
//插入数据表 //插入数据表
models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.VisitorId,clientMsg.Refer,clientMsg.City,clientMsg.ClientIp) models.UpdateVisitor(clientMsg.VisitorId,1,clientMsg.ClientIp,message.c.ClientIP(),clientMsg.Refer)
//models.CreateVisitor(clientMsg.Name,clientMsg.Avator,message.c.ClientIP(),clientMsg.ToId,clientMsg.VisitorId,clientMsg.Refer,clientMsg.City,clientMsg.ClientIp)
userInfo := make(map[string]string) userInfo := make(map[string]string)
userInfo["uid"] = user.id userInfo["uid"] = user.id
userInfo["username"] = user.name userInfo["username"] = user.name

@ -112,6 +112,28 @@ func SendMessage(c *gin.Context) {
"msg": "ok", "msg": "ok",
}) })
} }
func SendVisitorNotice(c *gin.Context) {
notice := c.Query("msg")
if notice==""{
c.JSON(200, gin.H{
"code": 400,
"msg": "msg不能为空",
})
return
}
msg := TypeMessage{
Type: "notice",
Data: notice,
}
str, _ := json.Marshal(msg)
for _,visitor :=range clientList{
visitor.conn.WriteMessage(websocket.TextMessage,str)
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
func UploadImg(c *gin.Context){ func UploadImg(c *gin.Context){
config:=config.CreateConfig() config:=config.CreateConfig()
f, err := c.FormFile("imgfile") f, err := c.FormFile("imgfile")

@ -15,6 +15,7 @@ func InitApiRouter(engine *gin.Engine){
engine.GET("/chat_server",middleware.Ipblack, controller.NewChatServer) engine.GET("/chat_server",middleware.Ipblack, controller.NewChatServer)
//获取消息 //获取消息
engine.GET("/messages", controller.GetVisitorMessage) engine.GET("/messages", controller.GetVisitorMessage)
engine.GET("/message_notice", controller.SendVisitorNotice)
//发送单条消息 //发送单条消息
engine.POST("/message",middleware.Ipblack,controller.SendMessage) engine.POST("/message",middleware.Ipblack,controller.SendMessage)
//上传文件 //上传文件

@ -43,6 +43,15 @@ new Vue({
this.scrollBottom(); this.scrollBottom();
this.showKfonline=true; this.showKfonline=true;
} }
if (redata.type == "notice") {
let msg = redata.data
if(!msg){
return;
}
this.chatTitle=msg
$(".chatBox").append("<div class=\"chatTime\">"+this.chatTitle+"</div>");
this.scrollBottom();
}
if (redata.type == "message") { if (redata.type == "message") {
let msg = redata.data let msg = redata.data
this.visitor.to_id=msg.id; this.visitor.to_id=msg.id;

Loading…
Cancel
Save