41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"StuAcaWorksAI/proto"
|
|
"StuAcaWorksAI/service"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
type SMessage struct {
|
|
Type int `json:"type" form:"type"`
|
|
Msg string `json:"msg" form:"msg"`
|
|
ToID int `json:"to_id" form:"to_id"`
|
|
SessionID int `json:"session_id" form:"session_id"`
|
|
}
|
|
|
|
func SetUpIMGroup(router *gin.Engine) {
|
|
imGroup := router.Group("/im")
|
|
imGroup.POST("/send_message", SendMessage)
|
|
}
|
|
|
|
func SendMessage(c *gin.Context) {
|
|
var req SMessage
|
|
id, _ := c.Get("id")
|
|
userID := int(id.(float64))
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
var err2 error
|
|
var mid uint
|
|
err2, mid = service.CreateGeneralMessageService(userID, req.ToID, req.Type, req.SessionID, req.Msg)
|
|
if err2 == nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": mid})
|
|
} 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"})
|
|
}
|
|
}
|
|
|
|
//ws长连接
|