添加聊天信息创建及部分表

This commit is contained in:
junleea 2024-08-02 09:47:33 +08:00
parent 63b5b968b4
commit 261ea9a0e0
3 changed files with 66 additions and 1 deletions

View File

@ -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
}

56
dao/im.go Normal file
View File

@ -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
}

View File

@ -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