optimize the code for change chat session

This commit is contained in:
RockYang
2023-04-19 21:45:37 +08:00
parent 673cdada5a
commit efc1ea5d8b
2 changed files with 585 additions and 588 deletions

View File

@@ -61,18 +61,18 @@ func (s *Server) ChatHandle(c *gin.Context) {
return return
} }
logger.Info("Receive a message: ", string(message)) logger.Info("Receive a message: ", string(message))
replyMessage(client, "当前 TOKEN 无效,请使用合法的 TOKEN 登录!", false) //replyMessage(client, "当前 TOKEN 无效,请使用合法的 TOKEN 登录!", false)
//replyMessage(client, "![](images/wx.png)", true) //replyMessage(client, "![](images/wx.png)", true)
//ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
//s.ReqCancelFunc[sessionId] = cancel s.ReqCancelFunc[sessionId] = cancel
//// 回复消息 // 回复消息
//err = s.sendMessage(ctx, session, chatRole, string(message), client, false) err = s.sendMessage(ctx, session, chatRole, string(message), client, false)
//if err != nil { if err != nil {
// logger.Error(err) logger.Error(err)
//} else { } else {
// replyChunkMessage(client, types.WsMessage{Type: types.WsEnd, IsHelloMsg: false}) replyChunkMessage(client, types.WsMessage{Type: types.WsEnd, IsHelloMsg: false})
// logger.Info("回答完毕: " + string(message)) logger.Info("回答完毕: " + string(message))
//} }
} }
}() }()

View File

@@ -111,7 +111,8 @@
<div class="btn-container"> <div class="btn-container">
<el-row> <el-row>
<el-button type="success" class="send" :disabled="sending" v-on:click="sendMessage">发送</el-button> <el-button type="success" class="send" :disabled="sending" v-on:click="sendMessage">发送
</el-button>
</el-row> </el-row>
</div> </div>
</div> </div>
@@ -286,8 +287,11 @@ export default defineComponent({
this.errorMessage.close(); // 关闭错误提示信息 this.errorMessage.close(); // 关闭错误提示信息
} }
this.activelyClose = false; this.activelyClose = false;
// 加载聊天记录 // 加载聊天列表
this.fetchChatHistory(); const chatList = getChatList();
if (chatList) {
this.chatList = chatList;
}
}); });
socket.addEventListener('message', event => { socket.addEventListener('message', event => {
@@ -387,14 +391,11 @@ export default defineComponent({
}, },
// 从后端获取聊天历史记录 // 从后端获取聊天历史记录
fetchChatHistory: function () { fetchChatHistory: function (chatId) {
const chatList = getChatList(); const list = getChatHistory(chatId);
if (chatList) {
this.chatList = chatList;
const list = getChatHistory(this.curChat.id);
if (list) { if (list) {
const md = require('markdown-it')(); const md = require('markdown-it')();
console.log(list)
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
if (list[i].type === "prompt") { if (list[i].type === "prompt") {
this.chatData.push(list[i]); this.chatData.push(list[i]);
@@ -413,9 +414,6 @@ export default defineComponent({
}) })
}) })
} }
}
}, },
inputKeyDown: function (e) { inputKeyDown: function (e) {
@@ -589,6 +587,9 @@ export default defineComponent({
setChat(chat) setChat(chat)
} else if (this.curOpt === 'remove') { } else if (this.curOpt === 'remove') {
delete this.chatList[chat.id]; delete this.chatList[chat.id];
if (this.curChat.id === chat.id) {
this.chatData = [];
}
removeChat(chat.id); removeChat(chat.id);
chat.removing = false; chat.removing = false;
} }
@@ -613,11 +614,7 @@ export default defineComponent({
} }
this.curChat = chat; this.curChat = chat;
const chatHistory = getChatHistory(chat.id); this.fetchChatHistory(chat.id);
if (!chatHistory) {
return;
}
this.chatData = chatHistory;
}, },
// 退出登录 // 退出登录