添加消息发送及获取接口
This commit is contained in:
parent
7cef0ec8db
commit
fc12e233ae
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -41,4 +41,7 @@ const (
|
|||
|
||||
// UUID相关错误码
|
||||
UUIDNotFound = 18 // uuid不存在
|
||||
|
||||
//消息错误码
|
||||
MsgSendFailed = 61 // 消息发送失败
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue