wx-server/internal/server/chat_record/upload.go

52 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-06-04 21:22:50 +08:00
package chatrecord
import (
"fmt"
"mini_server/internal/dao"
"mini_server/internal/server/help"
"mini_server/internal/server/httpreply"
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
type UploadRequest struct {
RoleId int32 `json:"role_id" binding:"required,gt=0"`
Direction *int `json:"direction" binding:"required,oneof=0 1"` // 整数required时无法接收0值会报错。需要定义为指针类型
Message string `json:"message" binding:"required,min=1"`
}
// 上传聊天记录
func Upload(c *gin.Context) {
reply := httpreply.NewDefaultReplyData()
defer httpreply.Reply(c, reply)
userId, ok := help.GetUserId(c)
if !ok {
reply.Status = http.StatusNotFound
reply.Message = "无法获取用户信息"
return
}
var info UploadRequest
if err := c.ShouldBindJSON(&info); err != nil {
reply.Status = http.StatusBadRequest
reply.Message = fmt.Sprintf("invalid json data:%v", err)
return
}
data := dao.ChatRecord{
UserId: userId,
CharacterId: info.RoleId,
Direction: int8(*info.Direction),
Message: info.Message,
}
if err := dao.ChatRecord_Insert(&data); err != nil {
logrus.Errorf("保存聊天记录失败info:%v,err:%v", info, err)
reply.Status = http.StatusInternalServerError
reply.Message = "聊天记录保存失败"
return
}
}