From 261ea9a0e0ffc3eecc5a61d6f94c8b736f03a6fe Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 2 Aug 2024 09:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=81=8A=E5=A4=A9=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=88=9B=E5=BB=BA=E5=8F=8A=E9=83=A8=E5=88=86=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/db.go | 5 +++++ dao/im.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ handler/im.go | 6 +++++- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 dao/im.go diff --git a/dao/db.go b/dao/db.go index 5bf9c7f..bf432cc 100644 --- a/dao/db.go +++ b/dao/db.go @@ -40,6 +40,11 @@ func Init() { if err != nil { fmt.Println("cidrunlog table:", err) } // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表 + + err = db.AutoMigrate(&Message{}) + if err != nil { + fmt.Println("message table:", err) + } // 自动迁移,创建表,如果表已经存在,会自动更新表结构,不会删除表,只会创建不存在的表 DB = db } diff --git a/dao/im.go b/dao/im.go new file mode 100644 index 0000000..f17944a --- /dev/null +++ b/dao/im.go @@ -0,0 +1,56 @@ +package dao + +import "gorm.io/gorm" + +type Message struct { + gorm.Model + FromUserID int `gorm:"column:from_user_id"` + ToUserID int `gorm:"column:to_user_id"` + GroupID int `gorm:"column:group_id"` + Msg string `gorm:"column:msg"` + Status int `gorm:"column:status"` //单聊时才有0,已读,2未读 + Type int `gorm:"column:type"` //1为单聊,2为群聊,3为系统消息 +} + +type Group struct { + gorm.Model + GroupName string `gorm:"column:group_name"` +} + +type GroupUser struct { + gorm.Model + GroupID int `gorm:"column:group_id"` + UserID int `gorm:"column:user_id"` + IsMaster int `gorm:"column:is_master"` +} + +type Friend struct { + gorm.Model + UserID int `gorm:"column:user_id"` + FriendID int `gorm:"column:friend_id"` +} + +// 创建单聊消息 +func CreateSimpleMessage(from_user_id, to_user_id int, message string) error { + msg := Message{FromUserID: from_user_id, ToUserID: to_user_id, Msg: message, Type: 1, Status: 0} + res := DB.Debug().Create(msg) + return res.Error +} + +// 每20个消息一组,请求index*20 +func GetMsgUserByIndex(from_user_id, to_user_id, 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) + return msgs, res.Error +} + +// 修改信息 +func UpdateMessage() { + +} + +func FindMessageByID(id uint) []Message { + var msgs []Message + DB.Debug().Where("id = ?", id).Find(msgs) + return msgs +} diff --git a/handler/im.go b/handler/im.go index 4fc96ee..32eaf08 100644 --- a/handler/im.go +++ b/handler/im.go @@ -37,7 +37,7 @@ func SetUpIMGroup(router *gin.Engine) { imGroup := router.Group("/im") imGroup.POST("/get_imKey", GetImKey) imGroup.GET("/ws", SRMessage) - + imGroup.POST("/send_message", SendMessage) } func generateRandomHexString(length int) (string, error) { bytes := make([]byte, length/2) // 16字节的字符串需要32个十六进制字符,即16个字节 @@ -47,6 +47,10 @@ func generateRandomHexString(length int) (string, error) { return hex.EncodeToString(bytes), nil } +func SendMessage(c *gin.Context) { + +} + func GetImKey(c *gin.Context) { id, _ := c.Get("id") var req proto.ImKeyReq