123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- 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)
- }
|