|
|
package ImRelateController
|
|
|
|
|
|
import (
|
|
|
"dsSzxy/Business/ImRelate/ImRelateDao"
|
|
|
"dsSzxy/Utils/CommonUtil"
|
|
|
"dsSzxy/Utils/ConfigUtil"
|
|
|
"encoding/json"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
//模块的路由配置
|
|
|
func Routers(r *gin.RouterGroup) {
|
|
|
rr := r.Group("/imRelate")
|
|
|
rr.POST("/sendTxtMsg", sendTxtMsg)
|
|
|
rr.POST("/sendImgMsg", sendImgMsg)
|
|
|
rr.POST("/sendFileMsg", sendFileMsg)
|
|
|
rr.POST("/sendSightMsg", sendSightMsg)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:发送融云文本消息
|
|
|
作者:吴缤
|
|
|
日期:2021-08-16
|
|
|
*/
|
|
|
func sendTxtMsg(c *gin.Context) {
|
|
|
//发送方Id。
|
|
|
fromId := c.PostForm("fromId")
|
|
|
//接收方Id。
|
|
|
toId := c.PostForm("toId")
|
|
|
//发送类型 p:单聊 g:群聊
|
|
|
sendType := c.PostForm("sendType")
|
|
|
//内容。
|
|
|
content := c.PostForm("content")
|
|
|
|
|
|
m := map[string]string{"content": content}
|
|
|
contentJsonObj, _ := json.Marshal(m)
|
|
|
contentJsonStr := string(contentJsonObj)
|
|
|
|
|
|
sendErr := ImRelateDao.SendRongYunMsg(sendType, fromId, toId, "RC:TxtMsg", contentJsonStr)
|
|
|
if sendErr != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "发送消息失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
fromName := ImRelateDao.GetPersonNameByUserId(fromId)
|
|
|
fromAvatar := ImRelateDao.GetPersonAvatarByUserId(fromId)
|
|
|
var toName, toAvatar string
|
|
|
if sendType == "p" {
|
|
|
toName = ImRelateDao.GetPersonNameByUserId(toId)
|
|
|
toAvatar = ImRelateDao.GetPersonAvatarByUserId(toId)
|
|
|
} else
|
|
|
{
|
|
|
toName, toAvatar = ImRelateDao.GetGroupNameAvatarByGroupId(toId)
|
|
|
}
|
|
|
|
|
|
cm := map[string]string{
|
|
|
"sender_user_id": fromId,
|
|
|
"sender_user_name": fromName,
|
|
|
"sender_user_avatar": fromAvatar,
|
|
|
"receiver_user_id": toId,
|
|
|
"receiver_user_name": toName,
|
|
|
"receiver_user_avatar": toAvatar,
|
|
|
"send_time": CommonUtil.GetCurrentTime(),
|
|
|
"content": content,
|
|
|
"message_type": "TxtMsg",
|
|
|
"chat_type": sendType,
|
|
|
}
|
|
|
bodyJsonObj, _ := json.Marshal(cm)
|
|
|
bodyString := string(bodyJsonObj)
|
|
|
|
|
|
saveErr := ImRelateDao.SaveChatRecord("chat_record", bodyString)
|
|
|
if saveErr != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "聊天记录保存失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:发送融云图片消息
|
|
|
作者:吴缤
|
|
|
日期:2021-08-16
|
|
|
*/
|
|
|
func sendImgMsg(c *gin.Context) {
|
|
|
//发送方Id。
|
|
|
fromId := c.PostForm("fromId")
|
|
|
//接收方Id。
|
|
|
toId := c.PostForm("toId")
|
|
|
//发送类型 p:单聊 g:群聊
|
|
|
sendType := c.PostForm("sendType")
|
|
|
//图片base64。
|
|
|
imgBase64 := c.PostForm("imgBase64")
|
|
|
//imgUrl
|
|
|
imgUrl := c.PostForm("imgUrl")
|
|
|
//文件名
|
|
|
fileName := c.PostForm("fileName")
|
|
|
|
|
|
m := map[string]string{"content": imgBase64, "imageUri": imgUrl}
|
|
|
contentJsonObj, _ := json.Marshal(m)
|
|
|
contentJsonStr := string(contentJsonObj)
|
|
|
|
|
|
err := ImRelateDao.SendRongYunMsg(sendType, fromId, toId, "RC:ImgMsg", contentJsonStr)
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "发送消息失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
fromName := ImRelateDao.GetPersonNameByUserId(fromId)
|
|
|
fromAvatar := ImRelateDao.GetPersonAvatarByUserId(fromId)
|
|
|
var toName, toAvatar string
|
|
|
if sendType == "p" {
|
|
|
toName = ImRelateDao.GetPersonNameByUserId(toId)
|
|
|
toAvatar = ImRelateDao.GetPersonAvatarByUserId(toId)
|
|
|
} else
|
|
|
{
|
|
|
toName, toAvatar = ImRelateDao.GetGroupNameAvatarByGroupId(toId)
|
|
|
}
|
|
|
|
|
|
cm := map[string]string{
|
|
|
"sender_user_id": fromId,
|
|
|
"sender_user_name": fromName,
|
|
|
"sender_user_avatar": fromAvatar,
|
|
|
"receiver_user_id": toId,
|
|
|
"receiver_user_name": toName,
|
|
|
"receiver_user_avatar": toAvatar,
|
|
|
"send_time": CommonUtil.GetCurrentTime(),
|
|
|
"content": fileName,
|
|
|
"base64": imgBase64,
|
|
|
"url": imgUrl,
|
|
|
"message_type": "ImgMsg",
|
|
|
"chat_type": sendType,
|
|
|
}
|
|
|
bodyJsonObj, _ := json.Marshal(cm)
|
|
|
bodyString := string(bodyJsonObj)
|
|
|
|
|
|
saveErr := ImRelateDao.SaveChatRecord("chat_record", bodyString)
|
|
|
if saveErr != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "聊天记录保存失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:发送融云文件消息
|
|
|
作者:吴缤
|
|
|
日期:2021-08-16
|
|
|
*/
|
|
|
func sendFileMsg(c *gin.Context) {
|
|
|
//发送方Id。
|
|
|
fromId := c.PostForm("fromId")
|
|
|
//接收方Id。
|
|
|
toId := c.PostForm("toId")
|
|
|
//发送类型 p:单聊 g:群聊
|
|
|
sendType := c.PostForm("sendType")
|
|
|
//文件名
|
|
|
fileName := c.PostForm("fileName")
|
|
|
//文件大小
|
|
|
fileSize := c.PostForm("fileSize")
|
|
|
//文件类型
|
|
|
fileType := c.PostForm("fileType")
|
|
|
//文件URL
|
|
|
fileUrl := c.PostForm("fileUrl")
|
|
|
|
|
|
m := map[string]string{"name": fileName, "size": fileSize, "type": fileType, "fileUrl": fileUrl}
|
|
|
contentJsonObj, _ := json.Marshal(m)
|
|
|
contentJsonStr := string(contentJsonObj)
|
|
|
|
|
|
err := ImRelateDao.SendRongYunMsg(sendType, fromId, toId, "RC:FileMsg", contentJsonStr)
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "发送消息失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
fromName := ImRelateDao.GetPersonNameByUserId(fromId)
|
|
|
fromAvatar := ImRelateDao.GetPersonAvatarByUserId(fromId)
|
|
|
var toName, toAvatar string
|
|
|
if sendType == "p" {
|
|
|
toName = ImRelateDao.GetPersonNameByUserId(toId)
|
|
|
toAvatar = ImRelateDao.GetPersonAvatarByUserId(toId)
|
|
|
} else
|
|
|
{
|
|
|
toName, toAvatar = ImRelateDao.GetGroupNameAvatarByGroupId(toId)
|
|
|
}
|
|
|
|
|
|
cm := map[string]string{
|
|
|
"sender_user_id": fromId,
|
|
|
"sender_user_name": fromName,
|
|
|
"sender_user_avatar": fromAvatar,
|
|
|
"receiver_user_id": toId,
|
|
|
"receiver_user_name": toName,
|
|
|
"receiver_user_avatar": toAvatar,
|
|
|
"send_time": CommonUtil.GetCurrentTime(),
|
|
|
"content": fileName,
|
|
|
"size": fileSize,
|
|
|
"file_type": fileType,
|
|
|
"url": fileUrl,
|
|
|
"message_type": "FileMsg",
|
|
|
"chat_type": sendType,
|
|
|
}
|
|
|
bodyJsonObj, _ := json.Marshal(cm)
|
|
|
bodyString := string(bodyJsonObj)
|
|
|
|
|
|
saveErr := ImRelateDao.SaveChatRecord("chat_record", bodyString)
|
|
|
if saveErr != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "聊天记录保存失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:发送融云视频消息
|
|
|
作者:吴缤
|
|
|
日期:2021-08-16
|
|
|
*/
|
|
|
func sendSightMsg(c *gin.Context) {
|
|
|
//发送方Id。
|
|
|
fromId := c.PostForm("fromId")
|
|
|
//接收方Id。
|
|
|
toId := c.PostForm("toId")
|
|
|
//发送类型 p:单聊 g:群聊
|
|
|
sendType := c.PostForm("sendType")
|
|
|
//视频URL
|
|
|
videoUrl := c.PostForm("videoUrl")
|
|
|
//视频缩略图base64编码
|
|
|
thumbBase64 := ConfigUtil.VideoThumb
|
|
|
//视频大小
|
|
|
videoSize := c.PostForm("videoSize")
|
|
|
//视频名称
|
|
|
videoName := c.PostForm("fileName")
|
|
|
|
|
|
m := map[string]string{"sightUrl": videoUrl, "content": thumbBase64, "duration": "60", "size": videoSize, "name": videoName}
|
|
|
contentJsonObj, _ := json.Marshal(m)
|
|
|
contentJsonStr := string(contentJsonObj)
|
|
|
|
|
|
err := ImRelateDao.SendRongYunMsg(sendType, fromId, toId, "RC:SightMsg", contentJsonStr)
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "发送消息失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
fromName := ImRelateDao.GetPersonNameByUserId(fromId)
|
|
|
fromAvatar := ImRelateDao.GetPersonAvatarByUserId(fromId)
|
|
|
var toName, toAvatar string
|
|
|
if sendType == "p" {
|
|
|
toName = ImRelateDao.GetPersonNameByUserId(toId)
|
|
|
toAvatar = ImRelateDao.GetPersonAvatarByUserId(toId)
|
|
|
} else
|
|
|
{
|
|
|
toName, toAvatar = ImRelateDao.GetGroupNameAvatarByGroupId(toId)
|
|
|
}
|
|
|
|
|
|
cm := map[string]string{
|
|
|
"sender_user_id": fromId,
|
|
|
"sender_user_name": fromName,
|
|
|
"sender_user_avatar": fromAvatar,
|
|
|
"receiver_user_id": toId,
|
|
|
"receiver_user_name": toName,
|
|
|
"receiver_user_avatar": toAvatar,
|
|
|
"send_time": CommonUtil.GetCurrentTime(),
|
|
|
"content": videoName,
|
|
|
"size": videoSize,
|
|
|
"base64": thumbBase64,
|
|
|
"url": videoUrl,
|
|
|
"message_type": "SightMsg",
|
|
|
"chat_type": sendType,
|
|
|
}
|
|
|
bodyJsonObj, _ := json.Marshal(cm)
|
|
|
bodyString := string(bodyJsonObj)
|
|
|
|
|
|
saveErr := ImRelateDao.SaveChatRecord("chat_record", bodyString)
|
|
|
if saveErr != nil {
|
|
|
c.JSON(http.StatusOK, gin.H{"success": false, "info": "聊天记录保存失败!"})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"})
|
|
|
}
|