package ImRelateController import ( "dsSzxy/Business/ImRelate/ImRelateDao" "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("/sendVideoMsg", sendVideoMsg) } /** 功能:发送融云文本消息 作者:吴缤 日期: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 } cm := map[string]string{"name": "中华人民共和国1234", "country": "China", "age": "21", "date": "2021-08-17"} 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 } 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 } c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"}) } /** 功能:发送融云视频消息 作者:吴缤 日期:2021-08-16 */ func sendVideoMsg(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") //视频名称 fileName := c.PostForm("fileName") m := map[string]string{"sightUrl": videoUrl, "content": thumbBase64, "duration": "60", "size": videoSize, "name": fileName} 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 } c.JSON(http.StatusOK, gin.H{"success": true, "info": "发送消息成功!"}) }