获取session添加类型获取

This commit is contained in:
junleea 2025-04-03 19:16:14 +08:00
parent e478c973ea
commit 2d7c14087d
3 changed files with 13 additions and 12 deletions

View File

@ -49,12 +49,12 @@ func FindSessionByID(id int) Session {
}
// 获取用户最新的创建时间会话列表 50个
func FindSessionByUserID(userID int) []Session {
func FindSessionByUserID(userID, sessionType int) []Session {
var sessions []Session
if proto.Config.SERVER_SQL_LOG {
DB.Debug().Where("user_id = ?", userID).Order("created_at DESC").Limit(50).Find(&sessions)
DB.Debug().Where("user_id = ? and type = ?", userID, sessionType).Order("created_at DESC").Limit(50).Find(&sessions)
} else {
DB.Where("user_id = ?", userID).Order("created_at DESC").Limit(50).Find(&sessions)
DB.Where("user_id = ? and type = ?", userID, sessionType).Order("created_at DESC").Limit(50).Find(&sessions)
}
return sessions
}

View File

@ -16,10 +16,11 @@ func SetUpSessionGroup(router *gin.Engine) {
}
type Session struct {
ID int `json:"id" form:"id"`
UserID int `json:"user_id" form:"user_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
ID int `json:"id" form:"id"`
UserID int `json:"user_id" form:"user_id"`
Name string `json:"name" form:"name"`
Type string `json:"type" form:"type"`
SessionType int `json:"session_type" form:"session_type"`
}
func CreateSession(c *gin.Context) {
@ -46,14 +47,14 @@ func FindSession(c *gin.Context) {
if err := c.ShouldBind(&req); err == nil {
// 查找会话
if req.Type == "ID" {
err2, session := service.FindSessionByID(req.ID, userID)
err2, session := service.FindSessionByID(req.ID, userID, req.SessionType)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"error": err2.Error(), "code": proto.SessionSearchFailed, "message": "failed"})
return
}
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": session})
} else if req.Type == "UserID" {
sessions := service.FindSessionByUserID(userID)
sessions := service.FindSessionByUserID(userID, req.SessionType)
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": sessions})
} else {
c.JSON(http.StatusOK, gin.H{"error": "type error", "code": proto.SessionSearchFailed, "message": "failed"})

View File

@ -19,8 +19,8 @@ func CreateSession(userID, Type int, name string) (error, uint) {
}
// 查找用户的会话列表
func FindSessionByUserID(userID int) []proto.SessionResponse {
sessions := dao.FindSessionByUserID(userID)
func FindSessionByUserID(userID, sessionType int) []proto.SessionResponse {
sessions := dao.FindSessionByUserID(userID, sessionType)
sessionsMsgCounts := dao.FindSessionMessageCountByUserID(userID)
sessionsTokens := dao.FindModelTotalTokensBySessionID(userID)
smap := make(map[int]int)
@ -45,7 +45,7 @@ func FindSessionByUserID(userID int) []proto.SessionResponse {
}
return res
}
func FindSessionByID(id, userID int) (error, []dao.Session) {
func FindSessionByID(id, userID, sessionType int) (error, []dao.Session) {
session := dao.FindSessionByID(id)
var res []dao.Session
if session.UserID != userID {