From 14a351b4773570a76edcdbda26b5c3b946b79036 Mon Sep 17 00:00:00 2001 From: RockYang Date: Wed, 19 Apr 2023 15:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E8=BF=9B=E8=A1=8C=20socket=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=89=8D=E5=85=88=E5=85=B3=E9=97=AD=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E8=BF=9E=E6=8E=A5=E3=80=82=E5=AE=9E=E7=8E=B0=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E4=BC=9A=E8=AF=9D=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config_handler.go | 11 ++++ server/server.go | 2 +- utils/utils.go | 5 -- web/src/components/ChatReply.vue | 2 +- web/src/utils/storage.js | 28 +++++++- web/src/views/Chat.vue | 11 ++++ web/src/views/ChatFree.vue | 109 +++++++++++++++++++++++++++---- web/src/views/ChatPlus.vue | 12 ++++ 8 files changed, 158 insertions(+), 22 deletions(-) diff --git a/server/config_handler.go b/server/config_handler.go index 19f939d3..12545f50 100644 --- a/server/config_handler.go +++ b/server/config_handler.go @@ -290,6 +290,17 @@ func (s *Server) RemoveUserHandle(c *gin.Context) { // GetUserListHandle 获取用户列表 func (s *Server) GetUserListHandle(c *gin.Context) { + username := c.PostForm("username") + if username != "" { + user, err := GetUser(username) + if err != nil { + c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "User not exists"}) + } else { + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: user}) + } + return + } + c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: GetUsers()}) } diff --git a/server/server.go b/server/server.go index e8aede47..f8558a65 100644 --- a/server/server.go +++ b/server/server.go @@ -129,7 +129,7 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) { for { for key, ctx := range s.ChatContexts { // 清理超过 60min 没有更新,则表示为过期会话 - if time.Now().Unix()-ctx.LastAccessTime > int64(s.Config.Chat.ChatContextExpireTime) { + if time.Now().Unix()-ctx.LastAccessTime >= int64(s.Config.Chat.ChatContextExpireTime) { logger.Infof("清理会话上下文: %s", key) delete(s.ChatContexts, key) } diff --git a/utils/utils.go b/utils/utils.go index cf4d315c..c3c2e322 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,7 +3,6 @@ package utils import ( "math/rand" "strconv" - "strings" "time" ) @@ -27,10 +26,6 @@ func Long2IP(ipInt int64) string { return b0 + "." + b1 + "." + b2 + "." + b3 } -func IsBlank(value string) bool { - return len(strings.TrimSpace(value)) == 0 -} - func ContainsStr(slice []string, item string) bool { for _, e := range slice { if e == item { diff --git a/web/src/components/ChatReply.vue b/web/src/components/ChatReply.vue index 3dd41586..ca3009f5 100644 --- a/web/src/components/ChatReply.vue +++ b/web/src/components/ChatReply.vue @@ -13,7 +13,7 @@ class="box-item" effect="dark" content="复制回答" - placement="top" + placement="bottom" > diff --git a/web/src/utils/storage.js b/web/src/utils/storage.js index 64963557..f5775b0e 100644 --- a/web/src/utils/storage.js +++ b/web/src/utils/storage.js @@ -1,12 +1,13 @@ /* eslint-disable no-constant-condition */ import {dateFormat} from "@/utils/libs"; +import Storage from 'good-storage' /** * storage handler */ const SessionUserKey = 'LOGIN_USER'; -export const Global = {} +const ChatHistoryKey = "CHAT_HISTORY"; export function getSessionId() { const user = getLoginUser(); @@ -35,4 +36,27 @@ export function getUserInfo() { return user; } return {} -} \ No newline at end of file +} + +// 追加历史记录 +export function appendChatHistory(chatId, message) { + let history = Storage.get(ChatHistoryKey); + if (!history) { + history = {}; + } + if (!history[chatId]) { + history[chatId] = [message]; + } else { + history[chatId].push(message); + } + Storage.set(ChatHistoryKey, history); +} + +// 获取指定会话的历史记录 +export function getChatHistory(chatId) { + const history = Storage.get(ChatHistoryKey); + if (history && history[chatId]) { + return history[chatId]; + } + return []; +} diff --git a/web/src/views/Chat.vue b/web/src/views/Chat.vue index 7ad9ad43..6d595f49 100644 --- a/web/src/views/Chat.vue +++ b/web/src/views/Chat.vue @@ -208,6 +208,12 @@ export default defineComponent({ }, // 创建 socket 会话连接 connect: function () { + // 先关闭已有连接 + if (this.socket !== null) { + this.activelyClose = true; + this.socket.close(); + } + // 初始化 WebSocket 对象 const sessionId = getSessionId(); const socket = new WebSocket(process.env.VUE_APP_WS_HOST + `/api/chat?sessionId=${sessionId}&role=${this.role}`); @@ -226,6 +232,7 @@ export default defineComponent({ } this.sending = false; // 允许用户发送消息 + this.activelyClose = false; if (this.errorMessage !== null) { this.errorMessage.close(); // 关闭错误提示信息 } @@ -282,6 +289,10 @@ export default defineComponent({ }); socket.addEventListener('close', () => { + if (this.activelyClose) { // 忽略主动关闭 + return; + } + // 停止送消息 this.sending = true; this.checkSession(); diff --git a/web/src/views/ChatFree.vue b/web/src/views/ChatFree.vue index 1f9a84cd..65556809 100644 --- a/web/src/views/ChatFree.vue +++ b/web/src/views/ChatFree.vue @@ -8,13 +8,23 @@ 新建会话 -
  • +
  • - {{ session.title }} - - - + + + + {{ chat.title }} + + + + + + + + + +
  • @@ -123,7 +133,7 @@ @@ -522,6 +590,13 @@ export default defineComponent({ &:hover { background-color #2E2F39 + + a { + .btn { + display block + background-color: #282A32; + } + } } a { @@ -539,11 +614,13 @@ export default defineComponent({ font-size 14px; padding-top 2px; overflow hidden + white-space: nowrap; + text-overflow: ellipsis; + max-width 200px; } .btn { - //display none - + display none position absolute right 0; top 2px; @@ -558,8 +635,14 @@ export default defineComponent({ } } - } + .btn-check { + .el-icon { + margin-left 10px; + font-size 18px; + } + } + } } diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index ba9cdb91..1a007ede 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -238,6 +238,7 @@ export default defineComponent({ connectingMessageBox: null, // 保存重连的消息框对象 errorMessage: null, // 错误信息提示框 socket: null, + activelyClose: false, // 主动关闭 mainWinHeight: 0, // 主窗口高度 chatBoxHeight: 0, // 聊天内容框高度 leftBoxHeight: 0, @@ -284,6 +285,12 @@ export default defineComponent({ }, // 创建 socket 会话连接 connect: function () { + // 先关闭已有连接 + if (this.socket !== null) { + this.activelyClose = true; + this.socket.close(); + } + // 初始化 WebSocket 对象 const sessionId = getSessionId(); const socket = new WebSocket(process.env.VUE_APP_WS_HOST + `/api/chat?sessionId=${sessionId}&role=${this.role}`); @@ -303,6 +310,7 @@ export default defineComponent({ } this.sending = false; // 允许用户发送消息 + this.activelyClose = false; if (this.errorMessage !== null) { this.errorMessage.close(); // 关闭错误提示信息 } @@ -359,6 +367,10 @@ export default defineComponent({ }); socket.addEventListener('close', () => { + if (this.activelyClose) { // 忽略主动关闭 + return; + } + // 停止送消息 this.sending = true; this.checkSession();