chatroom.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package controllers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gopkg"
  5. "lzyd-message-api/common"
  6. "lzyd-message-api/models"
  7. "lzyd-message-api/services"
  8. )
  9. // @tags 房间管理
  10. // @Description 加入房间
  11. // @Summary 加入房间
  12. // @Accept json
  13. // @Produce json
  14. // @Param body body models.JoinChatroomParam true "加入房间请求body"
  15. // @Success 200 {string} json "{"code":200, "message":"success"}"
  16. // @Router /api/chatroom/join [post]
  17. func JoinChatroom(c *gin.Context) {
  18. var req models.JoinChatroomParam
  19. err := c.ShouldBindJSON(&req)
  20. if err != nil {
  21. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  22. return
  23. }
  24. if len(req.RoomId) <= 0 || len(req.UserId) <= 0 {
  25. common.ReturnFailure(c, common.InvalidParameters)
  26. return
  27. }
  28. // 判断用户是否存在
  29. user := services.GetUser(common.DB, req.UserId)
  30. if len(user.UserId) <= 0 {
  31. common.ReturnFailure(c, common.InvalidUser)
  32. return
  33. }
  34. // 判断房间是否存在
  35. room := services.GetChatroom(common.DB, req.RoomId)
  36. if len(room.RoomId) <= 0 {
  37. common.ReturnFailure(c, common.InvalidRoom)
  38. return
  39. }
  40. chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.UserId)
  41. // 如果没有离开房间, 直接返回成功
  42. if chatroomUser.ID > 0 && chatroomUser.LeaveTime == nil {
  43. common.ReturnSuccess(c, nil)
  44. return
  45. }
  46. // 如果离开了房间, 需要添加一条加入房间的记录
  47. chatroomUser.RoomId = req.RoomId
  48. chatroomUser.UserId = req.UserId
  49. if err = chatroomUser.Join(common.DB); err != nil {
  50. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  51. return
  52. }
  53. common.ReturnSuccess(c, nil)
  54. }
  55. // @tags 房间管理
  56. // @Description 离开房间
  57. // @Summary 离开房间
  58. // @Accept json
  59. // @Produce json
  60. // @Param body body models.LeaveChatroomParam true "离开房间请求body"
  61. // @Success 200 {string} json "{"code":200, "message":"success"}"
  62. // @Router /api/chatroom/leave [post]
  63. func LeaveChatroom(c *gin.Context) {
  64. var req models.LeaveChatroomParam
  65. err := c.ShouldBindJSON(&req)
  66. if err != nil {
  67. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  68. return
  69. }
  70. if len(req.RoomId) <= 0 || len(req.UserId) <= 0 {
  71. common.ReturnFailure(c, common.InvalidParameters)
  72. return
  73. }
  74. // 判断用户是否存在
  75. user := services.GetUser(common.DB, req.UserId)
  76. if len(user.UserId) <= 0 {
  77. common.ReturnFailure(c, common.InvalidUser)
  78. return
  79. }
  80. // 判断房间是否存在
  81. room := services.GetChatroom(common.DB, req.RoomId)
  82. if len(room.RoomId) <= 0 {
  83. common.ReturnFailure(c, common.InvalidRoom)
  84. return
  85. }
  86. chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.UserId)
  87. // 如果已经离开房间, 直接返回成功
  88. if chatroomUser.LeaveTime != nil {
  89. common.ReturnSuccess(c, nil)
  90. return
  91. }
  92. // 如果没有离开了房间, 需要修改对应的记录, 把离开房间的时间设置为当前时间
  93. if err = chatroomUser.Leave(common.DB,req.UserId); err != nil {
  94. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  95. return
  96. }
  97. common.ReturnSuccess(c, nil)
  98. }
  99. // @tags 房间管理
  100. // @Description 创建聊天室
  101. // @Summary 创建聊天室
  102. // @Accept json
  103. // @Produce json
  104. // @Param body body models.CreateChatroomReq true "创建聊天室请求body"
  105. // @Success 200 {string} json "{"code":200, "message":"success"}"
  106. // @Router /api/chatroom/create [post]
  107. func CreateChatroom(c *gin.Context) {
  108. var req models.CreateChatroomReq
  109. err := c.ShouldBindJSON(&req)
  110. if err != nil {
  111. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  112. return
  113. }
  114. if len(req.BCreatorId) <= 0 || len(req.CreatorId) <= 0 {
  115. common.ReturnFailure(c, common.InvalidParameters)
  116. return
  117. }
  118. // 判断用户是否存在
  119. creatorId := services.GetUser(common.DB, req.CreatorId)
  120. if len(creatorId.UserId) <= 0 {
  121. //fmt.Println(111)
  122. common.ReturnFailure(c, common.InvalidUser)
  123. return
  124. }
  125. bCreatorId := services.GetUser(common.DB, req.BCreatorId)
  126. if len(bCreatorId.UserId) <= 0 {
  127. common.ReturnFailure(c, common.InvalidUser)
  128. return
  129. }
  130. var (
  131. model models.ChatroomInfo
  132. code = common.Success
  133. )
  134. model.RoomId = gopkg.GenUUID()
  135. model.CreatorId = req.CreatorId
  136. model.BCreatorId = req.BCreatorId
  137. model.UserCount = 2
  138. model.CreatorName = creatorId.UserName
  139. model.BCreatorName = bCreatorId.UserName
  140. model.CreatorAvatar = creatorId.UserAvatar
  141. model.BCreatorAvatar = bCreatorId.UserAvatar
  142. if err := model.CreateRoom(common.DB); err != nil {
  143. code = common.InvalidParameters
  144. }
  145. if code != common.Success {
  146. return
  147. }
  148. var ret = models.ChatroomInfo{
  149. RoomId: model.RoomId,
  150. CreatorId: model.CreatorId,
  151. CreatorName: model.CreatorName,
  152. CreatorAvatar: model.CreatorAvatar,
  153. BCreatorId: model.BCreatorId,
  154. BCreatorName: model.BCreatorName,
  155. BCreatorAvatar: model.BCreatorAvatar,
  156. UserCount: model.UserCount,
  157. }
  158. common.ReturnSuccess(c, ret)
  159. }
  160. // @tags 房间管理
  161. // @Description 获取聊天室列表
  162. // @Summary 获取聊天室列表
  163. // @Accept json
  164. // @Produce json
  165. // @Param userId query string true "用户ID"
  166. // @Success 200 {string} json "{"code":200, "message":"success"}"
  167. // @Router /api/chatroom/list [get]
  168. func GetChatroomList(c *gin.Context) {
  169. userId := c.Query("userId")
  170. if len(userId) <= 0 {
  171. common.ReturnFailure(c, common.InvalidParameters)
  172. return
  173. }
  174. // 判断用户是否存在
  175. creatorId := services.GetUser(common.DB, userId)
  176. if len(creatorId.UserId) <= 0 {
  177. common.ReturnFailure(c, common.InvalidUser)
  178. return
  179. }
  180. var (
  181. model []models.ChatroomInfo
  182. )
  183. model = services.FindUser(common.DB, userId)
  184. common.ReturnSuccess(c, model)
  185. }
  186. // @tags 房间管理
  187. // @Description 删除聊天室
  188. // @Summary 删除聊天室
  189. // @Accept json
  190. // @Produce json
  191. // @Param userId query string true "用户ID"
  192. // @Param roomId query string true "房间ID"
  193. // @Success 200 {string} json "{"code":200, "message":"success"}"
  194. // @Router /api/chatroom/delete [get]
  195. func DeleteChatroom(c *gin.Context) {
  196. userId := c.Query("userId")
  197. roomId := c.Query("roomId")
  198. if len(userId) <= 0 {
  199. common.ReturnFailure(c, common.InvalidParameters)
  200. return
  201. }
  202. // 判断用户是否存在
  203. creatorId := services.GetUser(common.DB, userId)
  204. if len(creatorId.UserId) <= 0 {
  205. common.ReturnFailure(c, common.InvalidUser)
  206. return
  207. }
  208. deleteRoom := services.DeleteRoom(common.DB,userId,roomId)
  209. if deleteRoom {
  210. common.ReturnSuccess(c, common.Success)
  211. }
  212. }
  213. // @tags 房间管理
  214. // @Description 创建并加入房间
  215. // @Summary 创建并加入房间
  216. // @Accept json
  217. // @Produce json
  218. // @Param body body models.JoinChatroomReq true "创建并加入房间请求body"
  219. // @Success 200 {string} json "{"code":200, "message":"success"}"
  220. // @Router /api/chatroom/joinCreate [post]
  221. func JoinCreateChatroom(c *gin.Context) {
  222. var req models.JoinChatroomReq
  223. err := c.ShouldBindJSON(&req)
  224. if err != nil {
  225. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  226. return
  227. }
  228. if len(req.CreatorId) <= 0 || len(req.BCreatorId) <= 0{
  229. common.ReturnFailure(c, common.InvalidParameters)
  230. return
  231. }
  232. // 获取创建人信息
  233. creatorId := services.GetUser(common.DB, req.CreatorId)
  234. //fmt.Println(creatorId)
  235. if len(creatorId.UserId) <= 0 {
  236. //fmt.Println(111)
  237. common.ReturnFailure(c, common.InvalidUser)
  238. return
  239. }
  240. // 获取被创建人信息
  241. bCreatorId := services.GetUser(common.DB, req.BCreatorId)
  242. if len(bCreatorId.UserId) <= 0 {
  243. common.ReturnFailure(c, common.InvalidUser)
  244. return
  245. }
  246. var (
  247. model models.ChatroomInfo
  248. code = common.Success
  249. )
  250. model.CreatorId = req.CreatorId
  251. model.BCreatorId = req.BCreatorId
  252. model.UserCount = req.UserCount
  253. model.CreatorName = creatorId.UserName
  254. model.BCreatorName = bCreatorId.UserName
  255. model.CreatorAvatar = creatorId.UserAvatar
  256. model.BCreatorAvatar = bCreatorId.UserAvatar
  257. // 判断当前两用户是否存在对应房间
  258. exist := services.UserInRoom(req.CreatorId,req.BCreatorId)
  259. // 存在则赋值roomid
  260. if exist != nil {
  261. model.RoomId = exist.RoomId
  262. if err := model.UploadLiveUser(common.DB); err != nil {
  263. code = common.InvalidParameters
  264. }
  265. }else {
  266. // 不存在就创建一个room
  267. model.RoomId = gopkg.GenUUID()
  268. if err := model.CreateRoom(common.DB); err != nil {
  269. code = common.InvalidParameters
  270. }else {
  271. if code != common.Success {
  272. return
  273. }
  274. }
  275. }
  276. var ret = models.ChatroomInfo{
  277. RoomId: model.RoomId,
  278. CreatorId: model.CreatorId,
  279. CreatorName: model.CreatorName,
  280. CreatorAvatar: model.CreatorAvatar,
  281. BCreatorId: model.BCreatorId,
  282. BCreatorName: model.BCreatorName,
  283. BCreatorAvatar: model.BCreatorAvatar,
  284. UserCount: model.UserCount,
  285. }
  286. // 判断用户是否存在
  287. //user := services.GetUser(common.DB, req.CreatorId)
  288. //buser := services.GetUser(common.DB, req.BCreatorId)
  289. //
  290. //fmt.Println(user)
  291. //fmt.Println(buser)
  292. // 判断房间是否存在
  293. room := services.GetChatroom(common.DB, model.RoomId)
  294. //if len(room.RoomId) <= 0 {
  295. // common.ReturnFailure(c, common.InvalidRoom)
  296. // return
  297. //}
  298. chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.CreatorId)
  299. // 如果没有离开房间, 直接返回成功
  300. if chatroomUser.ID > 0 && chatroomUser.LeaveTime == nil {
  301. common.ReturnSuccess(c, ret)
  302. return
  303. }
  304. // 如果离开了房间, 需要添加一条加入房间的记录
  305. chatroomUser.RoomId = ret.RoomId
  306. chatroomUser.UserId = req.CreatorId
  307. if err = chatroomUser.Join(common.DB); err != nil {
  308. common.ReturnFailureMsg(c, common.InternalError, err.Error())
  309. return
  310. }
  311. common.ReturnSuccess(c, ret)
  312. }