diff --git a/api/handler/chat_history_handler.go b/api/handler/chat_history_handler.go index 954d09e6..c32972a8 100644 --- a/api/handler/chat_history_handler.go +++ b/api/handler/chat_history_handler.go @@ -10,44 +10,6 @@ import ( "github.com/gin-gonic/gin" ) -// List 获取会话列表 -func (h *ChatHandler) List(c *gin.Context) { - userId := h.GetInt(c, "user_id", 0) - if userId == 0 { - resp.ERROR(c, "The parameter 'user_id' is needed.") - return - } - var items = make([]vo.ChatItem, 0) - var chats []model.ChatItem - res := h.db.Where("user_id = ?", userId).Order("id DESC").Find(&chats) - if res.Error == nil { - var roleIds = make([]uint, 0) - for _, chat := range chats { - roleIds = append(roleIds, chat.RoleId) - } - var roles []model.ChatRole - res = h.db.Find(&roles, roleIds) - if res.Error == nil { - roleMap := make(map[uint]model.ChatRole) - for _, role := range roles { - roleMap[role.Id] = role - } - - for _, chat := range chats { - var item vo.ChatItem - err := utils.CopyObject(chat, &item) - if err == nil { - item.Id = chat.Id - item.Icon = roleMap[chat.RoleId].Icon - items = append(items, item) - } - } - } - - } - resp.SUCCESS(c, items) -} - // Update 更新会话标题 func (h *ChatHandler) Update(c *gin.Context) { var data struct { diff --git a/api/handler/chat_item_handler.go b/api/handler/chat_item_handler.go new file mode 100644 index 00000000..0bf1d759 --- /dev/null +++ b/api/handler/chat_item_handler.go @@ -0,0 +1,71 @@ +package handler + +import ( + "chatplus/store/model" + "chatplus/store/vo" + "chatplus/utils" + "chatplus/utils/resp" + "github.com/gin-gonic/gin" +) + +// List 获取会话列表 +func (h *ChatHandler) List(c *gin.Context) { + userId := h.GetInt(c, "user_id", 0) + if userId == 0 { + resp.ERROR(c, "The parameter 'user_id' is needed.") + return + } + var items = make([]vo.ChatItem, 0) + var chats []model.ChatItem + res := h.db.Where("user_id = ?", userId).Order("id DESC").Find(&chats) + if res.Error == nil { + var roleIds = make([]uint, 0) + for _, chat := range chats { + roleIds = append(roleIds, chat.RoleId) + } + var roles []model.ChatRole + res = h.db.Find(&roles, roleIds) + if res.Error == nil { + roleMap := make(map[uint]model.ChatRole) + for _, role := range roles { + roleMap[role.Id] = role + } + + for _, chat := range chats { + var item vo.ChatItem + err := utils.CopyObject(chat, &item) + if err == nil { + item.Id = chat.Id + item.Icon = roleMap[chat.RoleId].Icon + items = append(items, item) + } + } + } + + } + resp.SUCCESS(c, items) +} + +func (h *ChatHandler) Detail(c *gin.Context) { + chatId := h.GetTrim(c, "chat_id") + if utils.IsEmptyValue(chatId) { + resp.ERROR(c, "Invalid chatId") + return + } + + var chatItem model.ChatItem + res := h.db.Where("chat_id = ?", chatId).First(&chatItem) + if res.Error != nil { + resp.ERROR(c, "No chat found") + return + } + + var chatItemVo vo.ChatItem + err := utils.CopyObject(chatItem, &chatItemVo) + if err != nil { + resp.ERROR(c, err.Error()) + return + } + + resp.SUCCESS(c, chatItemVo) +} diff --git a/api/main.go b/api/main.go index cc77da2e..c713a480 100644 --- a/api/main.go +++ b/api/main.go @@ -169,6 +169,7 @@ func main() { group := s.Engine.Group("/api/chat/") group.Any("new", h.ChatHandle) group.GET("list", h.List) + group.GET("detail", h.Detail) group.POST("update", h.Update) group.GET("remove", h.Remove) group.GET("history", h.History) diff --git a/web/src/router.js b/web/src/router.js index 67d150b6..b6324d2b 100644 --- a/web/src/router.js +++ b/web/src/router.js @@ -26,6 +26,12 @@ const routes = [ meta: {title: 'ChatGPT-智能助手V3'}, component: () => import('@/views/ChatPlus.vue'), }, + { + name: 'chat-export', + path: '/chat/export', + meta: {title: '导出会话记录'}, + component: () => import('@/views/ChatExport.vue'), + }, { path: '/admin/login', name: 'admin-login', diff --git a/web/src/views/ChatExport.vue b/web/src/views/ChatExport.vue new file mode 100644 index 00000000..60264505 --- /dev/null +++ b/web/src/views/ChatExport.vue @@ -0,0 +1,155 @@ + + + \ No newline at end of file diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index 3f795993..357784cb 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -779,8 +779,10 @@ const exportChat = () => { if (!activeChat.value['chat_id']) { return ElMessage.error("请先选中一个会话") } - - window.open(location.protocol + location.host + '/chat/export?chat_id=' + activeChat.value['chat_id'], '_blank'); + + const url = location.protocol + '//' + location.host + '/chat/export?chat_id=' + activeChat.value['chat_id'] + // console.log(url) + window.open(url, '_blank'); }