message.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/jinzhu/gorm"
  6. "lzyd-message-api/common"
  7. "lzyd-message-api/models"
  8. "lzyd-message-api/services"
  9. "lzyd-message-api/utils"
  10. "lzyd-message-api/wrapper"
  11. "strings"
  12. )
  13. // @tags 消息管理
  14. // @Description 发送消息
  15. // @Summary 发送消息
  16. // @Accept json
  17. // @Produce json
  18. // @Param body body models.MessageParam true "发送请求body"
  19. // @Success 200 {string} json "{"code":200, "message":"success"}"
  20. // @Router /api/message/send [post]
  21. func SendMessage(c *gin.Context) {
  22. var req models.MessageParam
  23. err := c.ShouldBindJSON(&req)
  24. //fmt.Println(err)
  25. if err != nil {
  26. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  27. return
  28. }
  29. if len(req.Message) <= 0 || len(req.UserId) <= 0 || len(req.Message) <= 0 || len(req.RoomId) <= 0 {
  30. common.ReturnFailure(c, common.InvalidParameters)
  31. return
  32. }
  33. // 判断用户是否存在
  34. user := services.GetUser(common.DB, req.UserId)
  35. //fmt.Println(req.UserId,user)
  36. if len(user.UserId) <= 0 {
  37. common.ReturnFailure(c, common.InvalidUser)
  38. return
  39. }
  40. //room := services.GetUser(common.DB, req.RoomId)
  41. //if len(room.UserId) <= 0 {
  42. // common.ReturnFailure(c, common.InvalidUser)
  43. // return
  44. //}
  45. // 判断房间是否存在
  46. //fmt.Println(1,req.RoomId)
  47. room := services.GetChatroom(common.DB, req.RoomId)
  48. //fmt.Println(room)
  49. if len(room.RoomId) <= 0 {
  50. common.ReturnFailure(c, common.InvalidRoom)
  51. return
  52. }
  53. // 判断该用户是否在这个房间
  54. state := models.ChatroomUser{RoomId: req.RoomId, UserId: req.UserId}
  55. if !state.IsInChatroom(common.DB) {
  56. common.ReturnFailure(c, common.UserIsNotInRoom)
  57. return
  58. }
  59. // userBlack 为true 表示双方存在一方有拉黑对方
  60. userBlack := services.UserBlack(room.CreatorId,room.BCreatorId)
  61. if userBlack {
  62. common.ReturnFailure(c, common.UserIsBlackAtten)
  63. return
  64. }else {
  65. // relation 为true 表示双方互相关注
  66. relation := services.UserRelation(room.CreatorId,room.BCreatorId,req.UserId)
  67. if !relation {
  68. counts := services.UserSendNum(room.RoomId,req.UserId)
  69. // counts 表示发送者给接收方发送的消息数量
  70. if counts >= 3 {
  71. common.ReturnFailure(c, common.UserIsNotAttention)
  72. return
  73. }
  74. }
  75. // 打开双方的聊天列表可见
  76. liveUser := services.UploadLiveUser(room.RoomId,room.CreatorId,room.BCreatorId)
  77. if liveUser {
  78. }
  79. }
  80. // 记录到数据库
  81. reviewState := 0
  82. if user.Role == 1 {
  83. reviewState = 1 // 直接审核通过
  84. }
  85. messageObject := services.MessageSql{
  86. Content: req.Message,
  87. MsgType: req.MessageType,
  88. FromUid: req.UserId,
  89. TargetId: req.RoomId,
  90. TargetType: "chatroom",
  91. ReviewState: reviewState,
  92. }
  93. message := messageObject.SaveMessage(common.DB,req.UserId,req.RoomId)
  94. //if len(room.RoomId) <= 0 {
  95. // common.ReturnFailure(c, common.InvalidRoom)
  96. // return
  97. //}
  98. //if err := common.DB.Save(&messageObject).Error; err != nil {
  99. // common.ReturnFailure(c, common.SaveMessageError)
  100. // return
  101. //}
  102. // 发送消息
  103. // 如果是真实用户, 发送的消息需要先审核才能显示, 此时不需要发送到房间, 只单发给该用户即可
  104. // 如果是模拟用户, 直接发送
  105. //if user.Type == 0 {
  106. // p2pmsg := wrapper.NewMessage()
  107. // p2pmsg.Target = req.UserId
  108. // p2pmsg.MsgType = req.MessageType
  109. // p2pmsg.TargetType = "user"
  110. // p2pmsg.Msg = req.Message
  111. // p2pmsg.From = req.UserId
  112. // p2pmsg.FromName = user.Nickname
  113. // p2pmsg.FromAvatar = user.Avatar
  114. // if err = services.Publish(common.MQTTClient, p2pmsg); err != nil {
  115. // common.ReturnFailure(c, common.SendMessageError)
  116. // } else {
  117. // common.ReturnSuccess(c, nil)
  118. // }
  119. // return
  120. //}
  121. roomMsg := wrapper.NewMessage()
  122. roomMsg.Target = req.RoomId
  123. roomMsg.MsgType = req.MessageType
  124. roomMsg.TargetType = "liveroom"
  125. roomMsg.Msg = req.Message
  126. roomMsg.From = req.UserId
  127. roomMsg.FromName = user.UserName
  128. roomMsg.FromAvatar = user.UserAvatar
  129. roomMsg.MsgId = message.MsgId
  130. //tmp := "2006-01-02 15:04:05"
  131. //res, _ := time.ParseInLocation(tmp,message.CreatedAt, time.Local)
  132. timeRes := strings.Split(message.CreatedAt,"Z")
  133. timeRes = strings.Split(timeRes[0],"T")
  134. var timeRes3 string
  135. timeRes3 = timeRes[0]+" "+timeRes[1]
  136. //fmt.Println(timeRes3)
  137. //timestamp := time.Now().Unix()
  138. //fmt.Println(timestamp)
  139. //timeLayout := "2006-01-02 15:04:05"
  140. //roomMsg.CreatedAt = time.Unix(timestamp, 0).Format(timeLayout)
  141. roomMsg.CreatedAt = timeRes3
  142. fmt.Println(3,message.CreatedAt)
  143. if err = services.Publish(common.MQTTClient, roomMsg); err != nil {
  144. common.ReturnFailure(c, common.SendMessageError)
  145. return
  146. }
  147. common.ReturnSuccess(c, roomMsg)
  148. }
  149. // @tags 消息管理
  150. // @Summary 获取历史消息记录
  151. // @Description 获取历史消息记录
  152. // @Produce json
  153. // @Param userId query string false "用户id"
  154. // @Param roomId query string true "房间id"
  155. // @Param pageIndex query int true "当前页"
  156. // @Param pageSize query int true "每页数量"
  157. // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}"
  158. // @Router /api/message/timeline [get]
  159. func GetMessages(c *gin.Context) {
  160. userId := c.Query("userId")
  161. roomId := c.Query("roomId")
  162. if len(userId) <= 0 {
  163. common.ReturnFailure(c, common.InvalidParameters)
  164. return
  165. }
  166. if len(roomId) <= 0 {
  167. common.ReturnFailure(c, common.InvalidParameters)
  168. return
  169. }
  170. pageIndex := utils.GetPageIndex(c)
  171. pageSize := utils.GetPageSize(c)
  172. // 判断房间是否存在
  173. room := services.GetChatroom(common.DB, roomId)
  174. if len(room.RoomId) <= 0 {
  175. common.ReturnFailure(c, common.InvalidRoom)
  176. return
  177. }
  178. var db *gorm.DB
  179. // 获取数量
  180. var count int64
  181. db = common.DB.Table("lzyd_live_message")
  182. if len(userId) > 0 {
  183. db = db.Where("(review_state = 1 OR from_uid = ?)", userId)
  184. } else {
  185. db = db.Where("review_state = 1")
  186. }
  187. db.Where("target_id = ?", roomId).
  188. Where("target_type = ?", "chatroom").
  189. Where("deleted_at IS NULL").
  190. Count(&count)
  191. var messages models.Messages
  192. db = common.DB.
  193. Select(`lzyd_live_message.content, lzyd_live_message.msg_type, lzyd_live_message.created_at,
  194. lzyd_user.user_id, lzyd_user.username, lzyd_user.user_avatar`).
  195. Joins("JOIN lzyd_user ON lzyd_user.user_id = lzyd_live_message.from_uid")
  196. //if len(userId) > 0 {
  197. // db = db.Where("(lzyd_live_message.review_state = 1 OR lzyd_live_message.from_uid = ?)", userId)
  198. //} else {
  199. // db = db.Where("lzyd_live_message.review_state = 1")
  200. //}
  201. db.Debug().Where("lzyd_live_message.target_id = ?", roomId).
  202. Where("lzyd_live_message.target_type = ?", "chatroom").
  203. Where("lzyd_live_message.deleted_at IS NULL").
  204. Order("lzyd_live_message.id DESC").
  205. Limit(pageSize).Offset((pageIndex - 1) * pageSize).
  206. Find(&messages)
  207. common.ReturnSuccess(c, &models.ListResp{
  208. Total: count,
  209. List: messages,
  210. })
  211. }
  212. // @tags 消息管理
  213. // @Summary 获取公告
  214. // @Description 获取公告
  215. // @Produce json
  216. // @Param userId query string false "用户id"
  217. // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}"
  218. // @Router /api/message/notice [get]
  219. func GetNoticeType(c *gin.Context) {
  220. userCode := c.Query("userId")
  221. //if len(userCode) <= 0 {
  222. // common.ReturnFailure(c, common.InvalidParameters)
  223. // return
  224. //}
  225. fmt.Println(userCode)
  226. var (
  227. model models.NoticeTypeResp
  228. )
  229. model = services.QueryNoticeType(userCode)
  230. common.ReturnSuccess(c, model)
  231. return
  232. }
  233. // @tags 消息管理
  234. // @Description 公告阅读
  235. // @Summary 公告阅读
  236. // @Produce json
  237. // @Param body body models.NoticeReadReq true "阅读公告"
  238. // @Success 200 {string} json "{"code":200,"message":"success","data":{}}"
  239. // @Router /api/message/notice/read [post]
  240. func ReadNotice(c *gin.Context) {
  241. var req models.NoticeReadReq
  242. err := c.ShouldBindJSON(&req)
  243. if err != nil {
  244. panic(err)
  245. }
  246. services.QueryReadNotice(req.NoticeId,req.UserId,req.AppId)
  247. common.ReturnSuccess(c, nil)
  248. return
  249. }
  250. // @tags 消息管理
  251. // @Summary 获取互动
  252. // @Description 获取互动
  253. // @Produce json
  254. // @Param userId query string true "用户id"
  255. // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}"
  256. // @Router /api/message/interact [get]
  257. func GetInteract(c *gin.Context) {
  258. userCode := c.Query("userId")
  259. //if len(userCode) <= 0 {
  260. // common.ReturnFailure(c, common.InvalidParameters)
  261. // return
  262. //}
  263. var (
  264. model []models.InteractResp
  265. )
  266. model = services.QueryInteract(userCode)
  267. common.ReturnSuccess(c, model)
  268. return
  269. }
  270. // @tags 消息管理
  271. // @Summary 互动删除
  272. // @Description 互动删除
  273. // @Produce json
  274. // @Param id query string true "id"
  275. // @Param int_type query string true "互动类型"
  276. // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}"
  277. // @Router /api/message/deleteInteract [get]
  278. func DeleteInteract(c *gin.Context) {
  279. id := c.Query("id")
  280. intType := c.Query("int_type")
  281. err := services.UpdateInteract(id,intType)
  282. if err {
  283. common.ReturnSuccess(c, common.Success)
  284. }
  285. return
  286. }