From fc12e233ae490182f4098e9007e03475ac623244 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 2 Aug 2024 16:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B6=88=E6=81=AF=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=8F=8A=E8=8E=B7=E5=8F=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/im.go | 4 ++-- handler/im.go | 45 +++++++++++++++++++++++++++++++++++++++++++- proto/conf.go | 2 +- proto/status.go | 3 +++ service/imService.go | 37 ++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 service/imService.go diff --git a/dao/im.go b/dao/im.go index 9af0c27..dd6c42e 100644 --- a/dao/im.go +++ b/dao/im.go @@ -50,9 +50,9 @@ func CreateGeneralMessage(from_user_id, to_user_id, msg_type, status int, messag } // 每20个消息一组,请求index*20 -func GetMsgUserByIndex(from_user_id, to_user_id, index int) ([]Message, error) { +func GetMsgUserByIndex(from_user_id, to_user_id, msg_type, index int) ([]Message, error) { var msgs []Message - res := DB.Debug().Where("from_user_id = ? and to_user_id = ?", from_user_id, to_user_id).Find(&msgs).Order("order by createAt DESC").Limit(20 * index) + res := DB.Debug().Where("from_user_id = ? and to_user_id = ? and type = ?", from_user_id, to_user_id, msg_type).Find(&msgs).Order("order by createAt DESC").Limit(20 * index) return msgs, res.Error } diff --git a/handler/im.go b/handler/im.go index 32eaf08..926774e 100644 --- a/handler/im.go +++ b/handler/im.go @@ -13,9 +13,23 @@ import ( "sync" "time" "videoplayer/proto" + "videoplayer/service" "videoplayer/worker" ) +type SMessage struct { + To_user_id int `json:"to_user_id" form:"to_user_id" binding:"required"` + Type int `json:"type" form:"type" binding:"required"` + Msg string `json:"msg" form:"msg" binding:"required"` +} + +type Message struct { + To_user_id int `json:"to_user_id"` + From_user_id int `json:"from_user_id"` + Index int `json:"index"` + Type int `json:"type"` +} + var ( upgrader = websocket.Upgrader{ ReadBufferSize: 1024, @@ -38,6 +52,7 @@ func SetUpIMGroup(router *gin.Engine) { imGroup.POST("/get_imKey", GetImKey) imGroup.GET("/ws", SRMessage) imGroup.POST("/send_message", SendMessage) + imGroup.POST("/get_message", GetMessage) } func generateRandomHexString(length int) (string, error) { bytes := make([]byte, length/2) // 16字节的字符串需要32个十六进制字符,即16个字节 @@ -46,9 +61,37 @@ func generateRandomHexString(length int) (string, error) { } return hex.EncodeToString(bytes), nil } +func GetMessage(c *gin.Context) { + var req Message + user_id, _ := c.Get("id") + id := int(user_id.(float64)) + if err := c.ShouldBind(&req); err == nil { + msgs, err2 := service.GetMsgUserByIndexService(id, req.To_user_id, req.Index, req.Type) + if err2 == nil { + c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": msgs, "message": "success"}) + } else { + c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"}) + } + } else { + c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) + } +} func SendMessage(c *gin.Context) { - + var req SMessage + user_id, _ := c.Get("id") + id := int(user_id.(float64)) + if err := c.ShouldBind(&req); err == nil { + var err2 error + err2 = service.CreateGeneralMessageService(id, req.To_user_id, req.Type, req.Msg) + if err2 == nil { + c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"}) + } else { + c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.MsgSendFailed, "message": "failed"}) + } + } else { + c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) + } } func GetImKey(c *gin.Context) { diff --git a/proto/conf.go b/proto/conf.go index ee2e550..d553741 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -11,7 +11,7 @@ const ( MYSQL_DSN = MYSQL_USER + ":" + MYSQL_PASSWORD + "@tcp(" + MYSQL_HOST + ":" + MYSQL_PORT + ")/" + MYSQL_DB + "?charset=utf8mb4&parseTime=True&loc=Local" REDIS_ADDR = "127.0.0.1:6379" - REDIS_PASSWORD = "lj502138" + REDIS_PASSWORD = "" REIDS_DB = 2 TOKEN_SECRET = "mfjurnc_32ndj9dfhj" diff --git a/proto/status.go b/proto/status.go index 51478e9..c3ad35f 100644 --- a/proto/status.go +++ b/proto/status.go @@ -41,4 +41,7 @@ const ( // UUID相关错误码 UUIDNotFound = 18 // uuid不存在 + + //消息错误码 + MsgSendFailed = 61 // 消息发送失败 ) diff --git a/service/imService.go b/service/imService.go new file mode 100644 index 0000000..f5f3ba3 --- /dev/null +++ b/service/imService.go @@ -0,0 +1,37 @@ +package service + +import ( + "errors" + "videoplayer/dao" +) + +func CreateGeneralMessageService(from_id, to_id, msg_type int, content string) error { + // 业务逻辑 + var err error + switch msg_type { + case 1: + err = dao.CreateSimpleMessage(from_id, to_id, content) + case 2, 3: + err = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, content) + case 4: + err = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, content) + default: + // 未知消息类型 + err = errors.New("unknown message type") + } + return err +} + +func GetMsgUserByIndexService(from_id, to_id, index, msq_type int) ([]dao.Message, error) { + // 业务逻辑 + if index <= 0 || index > 100 { + return nil, errors.New("index out of range") + } + msgs, err := dao.GetMsgUserByIndex(from_id, to_id, index, msq_type) + return msgs, err +} + +func GetFriendGroupReqService(user_id int) ([]dao.Message, error) { + // 业务逻辑 + return nil, nil +}