package controllers import ( "fmt" "github.com/gin-gonic/gin" "github.com/jinzhu/gorm" "lzyd-message-api/common" "lzyd-message-api/models" "lzyd-message-api/services" "lzyd-message-api/utils" "lzyd-message-api/wrapper" "strings" ) // @tags 消息管理 // @Description 发送消息 // @Summary 发送消息 // @Accept json // @Produce json // @Param body body models.MessageParam true "发送请求body" // @Success 200 {string} json "{"code":200, "message":"success"}" // @Router /api/message/send [post] func SendMessage(c *gin.Context) { var req models.MessageParam err := c.ShouldBindJSON(&req) //fmt.Println(err) if err != nil { common.ReturnFailureMsg(c, common.InternalError, err.Error()) return } if len(req.Message) <= 0 || len(req.UserId) <= 0 || len(req.Message) <= 0 || len(req.RoomId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } // 判断用户是否存在 user := services.GetUser(common.DB, req.UserId) //fmt.Println(req.UserId,user) if len(user.UserId) <= 0 { common.ReturnFailure(c, common.InvalidUser) return } //room := services.GetUser(common.DB, req.RoomId) //if len(room.UserId) <= 0 { // common.ReturnFailure(c, common.InvalidUser) // return //} // 判断房间是否存在 //fmt.Println(1,req.RoomId) room := services.GetChatroom(common.DB, req.RoomId) //fmt.Println(room) if len(room.RoomId) <= 0 { common.ReturnFailure(c, common.InvalidRoom) return } // 判断该用户是否在这个房间 state := models.ChatroomUser{RoomId: req.RoomId, UserId: req.UserId} if !state.IsInChatroom(common.DB) { common.ReturnFailure(c, common.UserIsNotInRoom) return } // userBlack 为true 表示双方存在一方有拉黑对方 userBlack := services.UserBlack(room.CreatorId,room.BCreatorId) if userBlack { common.ReturnFailure(c, common.UserIsBlackAtten) return }else { // relation 为true 表示双方互相关注 relation := services.UserRelation(room.CreatorId,room.BCreatorId,req.UserId) if !relation { counts := services.UserSendNum(room.RoomId,req.UserId) // counts 表示发送者给接收方发送的消息数量 if counts >= 3 { common.ReturnFailure(c, common.UserIsNotAttention) return } } // 打开双方的聊天列表可见 liveUser := services.UploadLiveUser(room.RoomId,room.CreatorId,room.BCreatorId) if liveUser { } } // 记录到数据库 reviewState := 0 if user.Role == 1 { reviewState = 1 // 直接审核通过 } messageObject := services.MessageSql{ Content: req.Message, MsgType: req.MessageType, FromUid: req.UserId, TargetId: req.RoomId, TargetType: "chatroom", ReviewState: reviewState, } message := messageObject.SaveMessage(common.DB,req.UserId,req.RoomId) //if len(room.RoomId) <= 0 { // common.ReturnFailure(c, common.InvalidRoom) // return //} //if err := common.DB.Save(&messageObject).Error; err != nil { // common.ReturnFailure(c, common.SaveMessageError) // return //} // 发送消息 // 如果是真实用户, 发送的消息需要先审核才能显示, 此时不需要发送到房间, 只单发给该用户即可 // 如果是模拟用户, 直接发送 //if user.Type == 0 { // p2pmsg := wrapper.NewMessage() // p2pmsg.Target = req.UserId // p2pmsg.MsgType = req.MessageType // p2pmsg.TargetType = "user" // p2pmsg.Msg = req.Message // p2pmsg.From = req.UserId // p2pmsg.FromName = user.Nickname // p2pmsg.FromAvatar = user.Avatar // if err = services.Publish(common.MQTTClient, p2pmsg); err != nil { // common.ReturnFailure(c, common.SendMessageError) // } else { // common.ReturnSuccess(c, nil) // } // return //} roomMsg := wrapper.NewMessage() roomMsg.Target = req.RoomId roomMsg.MsgType = req.MessageType roomMsg.TargetType = "liveroom" roomMsg.Msg = req.Message roomMsg.From = req.UserId roomMsg.FromName = user.UserName roomMsg.FromAvatar = user.UserAvatar roomMsg.MsgId = message.MsgId //tmp := "2006-01-02 15:04:05" //res, _ := time.ParseInLocation(tmp,message.CreatedAt, time.Local) timeRes := strings.Split(message.CreatedAt,"Z") timeRes = strings.Split(timeRes[0],"T") var timeRes3 string timeRes3 = timeRes[0]+" "+timeRes[1] //fmt.Println(timeRes3) //timestamp := time.Now().Unix() //fmt.Println(timestamp) //timeLayout := "2006-01-02 15:04:05" //roomMsg.CreatedAt = time.Unix(timestamp, 0).Format(timeLayout) roomMsg.CreatedAt = timeRes3 fmt.Println(3,message.CreatedAt) if err = services.Publish(common.MQTTClient, roomMsg); err != nil { common.ReturnFailure(c, common.SendMessageError) return } common.ReturnSuccess(c, roomMsg) } // @tags 消息管理 // @Summary 获取历史消息记录 // @Description 获取历史消息记录 // @Produce json // @Param userId query string false "用户id" // @Param roomId query string true "房间id" // @Param pageIndex query int true "当前页" // @Param pageSize query int true "每页数量" // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}" // @Router /api/message/timeline [get] func GetMessages(c *gin.Context) { userId := c.Query("userId") roomId := c.Query("roomId") if len(userId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } if len(roomId) <= 0 { common.ReturnFailure(c, common.InvalidParameters) return } pageIndex := utils.GetPageIndex(c) pageSize := utils.GetPageSize(c) // 判断房间是否存在 room := services.GetChatroom(common.DB, roomId) if len(room.RoomId) <= 0 { common.ReturnFailure(c, common.InvalidRoom) return } var db *gorm.DB // 获取数量 var count int64 db = common.DB.Table("lzyd_live_message") if len(userId) > 0 { db = db.Where("(review_state = 1 OR from_uid = ?)", userId) } else { db = db.Where("review_state = 1") } db.Where("target_id = ?", roomId). Where("target_type = ?", "chatroom"). Where("deleted_at IS NULL"). Count(&count) var messages models.Messages db = common.DB. Select(`lzyd_live_message.content, lzyd_live_message.msg_type, lzyd_live_message.created_at, lzyd_user.user_id, lzyd_user.username, lzyd_user.user_avatar`). Joins("JOIN lzyd_user ON lzyd_user.user_id = lzyd_live_message.from_uid") //if len(userId) > 0 { // db = db.Where("(lzyd_live_message.review_state = 1 OR lzyd_live_message.from_uid = ?)", userId) //} else { // db = db.Where("lzyd_live_message.review_state = 1") //} db.Debug().Where("lzyd_live_message.target_id = ?", roomId). Where("lzyd_live_message.target_type = ?", "chatroom"). Where("lzyd_live_message.deleted_at IS NULL"). Order("lzyd_live_message.id DESC"). Limit(pageSize).Offset((pageIndex - 1) * pageSize). Find(&messages) common.ReturnSuccess(c, &models.ListResp{ Total: count, List: messages, }) } // @tags 消息管理 // @Summary 获取公告 // @Description 获取公告 // @Produce json // @Param userId query string false "用户id" // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}" // @Router /api/message/notice [get] func GetNoticeType(c *gin.Context) { userCode := c.Query("userId") //if len(userCode) <= 0 { // common.ReturnFailure(c, common.InvalidParameters) // return //} fmt.Println(userCode) var ( model models.NoticeTypeResp ) model = services.QueryNoticeType(userCode) common.ReturnSuccess(c, model) return } // @tags 消息管理 // @Description 公告阅读 // @Summary 公告阅读 // @Produce json // @Param body body models.NoticeReadReq true "阅读公告" // @Success 200 {string} json "{"code":200,"message":"success","data":{}}" // @Router /api/message/notice/read [post] func ReadNotice(c *gin.Context) { var req models.NoticeReadReq err := c.ShouldBindJSON(&req) if err != nil { panic(err) } services.QueryReadNotice(req.NoticeId,req.UserId,req.AppId) common.ReturnSuccess(c, nil) return } // @tags 消息管理 // @Summary 获取互动 // @Description 获取互动 // @Produce json // @Param userId query string true "用户id" // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}" // @Router /api/message/interact [get] func GetInteract(c *gin.Context) { userCode := c.Query("userId") //if len(userCode) <= 0 { // common.ReturnFailure(c, common.InvalidParameters) // return //} var ( model []models.InteractResp ) model = services.QueryInteract(userCode) common.ReturnSuccess(c, model) return } // @tags 消息管理 // @Summary 互动删除 // @Description 互动删除 // @Produce json // @Param id query string true "id" // @Param int_type query string true "互动类型" // @Success 200 {string} json "{"code":200, "message":"success", "data":{}}" // @Router /api/message/deleteInteract [get] func DeleteInteract(c *gin.Context) { id := c.Query("id") intType := c.Query("int_type") err := services.UpdateInteract(id,intType) if err { common.ReturnSuccess(c, common.Success) } return }