package controllers import ( "github.com/gin-gonic/gin" "gopkg" "lzyd-message-api/common" "lzyd-message-api/models" "lzyd-message-api/services" ) // @tags 房间管理 // @Description 加入房间 // @Summary 加入房间 // @Accept json // @Produce json // @Param body body models.JoinChatroomParam true "加入房间请求body" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/join [post] func JoinChatroom(c *gin.Context) { var req models.JoinChatroomParam err := c.ShouldBindJSON(&req) if err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } if len(req.RoomId) <= 0 || len(req.UserId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 user := services.GetUser(common.DB, req.UserId) if len(user.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } // 判断房间是否存在 room := services.GetChatroom(common.DB, req.RoomId) if len(room.RoomId) <= 0 { common.ReturnFailure(c, common.InvalidRoom) return } chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.UserId) // 如果没有离开房间, 直接返回成功 if chatroomUser.ID > 0 && chatroomUser.LeaveTime == nil { common.ReturnSuccess(c, nil) return } // 如果离开了房间, 需要添加一条加入房间的记录 chatroomUser.RoomId = req.RoomId chatroomUser.UserId = req.UserId if err = chatroomUser.Join(common.DB); err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } common.ReturnSuccess(c, nil) } // @tags 房间管理 // @Description 离开房间 // @Summary 离开房间 // @Accept json // @Produce json // @Param body body models.LeaveChatroomParam true "离开房间请求body" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/leave [post] func LeaveChatroom(c *gin.Context) { var req models.LeaveChatroomParam err := c.ShouldBindJSON(&req) if err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } if len(req.RoomId) <= 0 || len(req.UserId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 user := services.GetUser(common.DB, req.UserId) if len(user.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } // 判断房间是否存在 room := services.GetChatroom(common.DB, req.RoomId) if len(room.RoomId) <= 0 { common.ReturnFailure(c, common.InvalidRoom) return } chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.UserId) // 如果已经离开房间, 直接返回成功 if chatroomUser.LeaveTime != nil { common.ReturnSuccess(c, nil) return } // 如果没有离开了房间, 需要修改对应的记录, 把离开房间的时间设置为当前时间 if err = chatroomUser.Leave(common.DB,req.UserId); err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } common.ReturnSuccess(c, nil) } // @tags 房间管理 // @Description 创建聊天室 // @Summary 创建聊天室 // @Accept json // @Produce json // @Param body body models.CreateChatroomReq true "创建聊天室请求body" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/create [post] func CreateChatroom(c *gin.Context) { var req models.CreateChatroomReq err := c.ShouldBindJSON(&req) if err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } if len(req.BCreatorId) <= 0 || len(req.CreatorId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 creatorId := services.GetUser(common.DB, req.CreatorId) if len(creatorId.UserId) <= 0 { //fmt.Println(111) common.ReturnFailure(c, common.InvalidUser) return } bCreatorId := services.GetUser(common.DB, req.BCreatorId) if len(bCreatorId.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } var ( model models.ChatroomInfo code = common.Success ) model.RoomId = gopkg.GenUUID() model.CreatorId = req.CreatorId model.BCreatorId = req.BCreatorId model.UserCount = 2 model.CreatorName = creatorId.UserName model.BCreatorName = bCreatorId.UserName model.CreatorAvatar = creatorId.UserAvatar model.BCreatorAvatar = bCreatorId.UserAvatar if err := model.CreateRoom(common.DB); err != nil { code = common.InvalidParameters } if code != common.Success { return } var ret = models.ChatroomInfo{ RoomId: model.RoomId, CreatorId: model.CreatorId, CreatorName: model.CreatorName, CreatorAvatar: model.CreatorAvatar, BCreatorId: model.BCreatorId, BCreatorName: model.BCreatorName, BCreatorAvatar: model.BCreatorAvatar, UserCount: model.UserCount, } common.ReturnSuccess(c, ret) } // @tags 房间管理 // @Description 获取聊天室列表 // @Summary 获取聊天室列表 // @Accept json // @Produce json // @Param userId query string true "用户ID" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/list [get] func GetChatroomList(c *gin.Context) { userId := c.Query("userId") if len(userId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 creatorId := services.GetUser(common.DB, userId) if len(creatorId.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } var ( model []models.ChatroomInfo ) model = services.FindUser(common.DB, userId) common.ReturnSuccess(c, model) } // @tags 房间管理 // @Description 删除聊天室 // @Summary 删除聊天室 // @Accept json // @Produce json // @Param userId query string true "用户ID" // @Param roomId query string true "房间ID" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/delete [get] func DeleteChatroom(c *gin.Context) { userId := c.Query("userId") roomId := c.Query("roomId") if len(userId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 creatorId := services.GetUser(common.DB, userId) if len(creatorId.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } deleteRoom := services.DeleteRoom(common.DB,userId,roomId) if deleteRoom { common.ReturnSuccess(c, common.Success) } } // @tags 房间管理 // @Description 创建并加入房间 // @Summary 创建并加入房间 // @Accept json // @Produce json // @Param body body models.JoinChatroomReq true "创建并加入房间请求body" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/chatroom/joinCreate [post] func JoinCreateChatroom(c *gin.Context) { var req models.JoinChatroomReq err := c.ShouldBindJSON(&req) if err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } if len(req.CreatorId) <= 0 || len(req.BCreatorId) <= 0{ common.ReturnFailure(c, common.InvalidParameters) return } // 获取创建人信息 creatorId := services.GetUser(common.DB, req.CreatorId) //fmt.Println(creatorId) if len(creatorId.UserId) <= 0 { //fmt.Println(111) common.ReturnFailure(c, common.InvalidUser) return } // 获取被创建人信息 bCreatorId := services.GetUser(common.DB, req.BCreatorId) if len(bCreatorId.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } var ( model models.ChatroomInfo code = common.Success ) model.CreatorId = req.CreatorId model.BCreatorId = req.BCreatorId model.UserCount = req.UserCount model.CreatorName = creatorId.UserName model.BCreatorName = bCreatorId.UserName model.CreatorAvatar = creatorId.UserAvatar model.BCreatorAvatar = bCreatorId.UserAvatar // 判断当前两用户是否存在对应房间 exist := services.UserInRoom(req.CreatorId,req.BCreatorId) // 存在则赋值roomid if exist != nil { model.RoomId = exist.RoomId if err := model.UploadLiveUser(common.DB); err != nil { code = common.InvalidParameters } }else { // 不存在就创建一个room model.RoomId = gopkg.GenUUID() if err := model.CreateRoom(common.DB); err != nil { code = common.InvalidParameters }else { if code != common.Success { return } } } var ret = models.ChatroomInfo{ RoomId: model.RoomId, CreatorId: model.CreatorId, CreatorName: model.CreatorName, CreatorAvatar: model.CreatorAvatar, BCreatorId: model.BCreatorId, BCreatorName: model.BCreatorName, BCreatorAvatar: model.BCreatorAvatar, UserCount: model.UserCount, } // 判断用户是否存在 //user := services.GetUser(common.DB, req.CreatorId) //buser := services.GetUser(common.DB, req.BCreatorId) // //fmt.Println(user) //fmt.Println(buser) // 判断房间是否存在 room := services.GetChatroom(common.DB, model.RoomId) //if len(room.RoomId) <= 0 { // common.ReturnFailure(c, common.InvalidRoom) // return //} chatroomUser := services.GetChatroomUser(common.DB, room.ID, req.CreatorId) // 如果没有离开房间, 直接返回成功 if chatroomUser.ID > 0 && chatroomUser.LeaveTime == nil { common.ReturnSuccess(c, ret) return } // 如果离开了房间, 需要添加一条加入房间的记录 chatroomUser.RoomId = ret.RoomId chatroomUser.UserId = req.CreatorId if err = chatroomUser.Join(common.DB); err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } common.ReturnSuccess(c, ret) }