optimize ChatPlus page, fixed bug for websocket reconnection

This commit is contained in:
RockYang 2024-09-02 16:35:15 +08:00
parent b63e01225e
commit dfc6c87250
15 changed files with 64 additions and 110 deletions

View File

@ -6,6 +6,8 @@
* 功能优化:重构找回密码模块,支持通过手机或者邮箱找回密码 * 功能优化:重构找回密码模块,支持通过手机或者邮箱找回密码
* 功能优化:管理后台给可以拖动排序的组件添加拖动图标 * 功能优化:管理后台给可以拖动排序的组件添加拖动图标
* 功能优化Suno 支持合成完整歌曲,和上传自己的音乐作品进行二次创作 * 功能优化Suno 支持合成完整歌曲,和上传自己的音乐作品进行二次创作
* Bug修复手机端角色和模型选择不生效
* Bug修复用户登录过期之后聊天页面出现大量报错需要刷新页面才能正常
* 功能新增:支持 Luma 文生视频功能 * 功能新增:支持 Luma 文生视频功能
## v4.1.2 ## v4.1.2

View File

@ -201,7 +201,6 @@ func needLogin(c *gin.Context) bool {
c.Request.URL.Path == "/api/admin/logout" || c.Request.URL.Path == "/api/admin/logout" ||
c.Request.URL.Path == "/api/admin/login/captcha" || c.Request.URL.Path == "/api/admin/login/captcha" ||
c.Request.URL.Path == "/api/user/register" || c.Request.URL.Path == "/api/user/register" ||
c.Request.URL.Path == "/api/user/session" ||
c.Request.URL.Path == "/api/chat/history" || c.Request.URL.Path == "/api/chat/history" ||
c.Request.URL.Path == "/api/chat/detail" || c.Request.URL.Path == "/api/chat/detail" ||
c.Request.URL.Path == "/api/chat/list" || c.Request.URL.Path == "/api/chat/list" ||

View File

@ -270,7 +270,8 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
tks, _ := utils.CalcTokens(utils.JsonEncode(req.Tools), req.Model) tks, _ := utils.CalcTokens(utils.JsonEncode(req.Tools), req.Model)
tokens += tks + promptTokens tokens += tks + promptTokens
for _, v := range messages { for i := len(messages) - 1; i >= 0; i-- {
v := messages[i]
tks, _ := utils.CalcTokens(v.Content, req.Model) tks, _ := utils.CalcTokens(v.Content, req.Model)
// 上下文 token 超出了模型的最大上下文长度 // 上下文 token 超出了模型的最大上下文长度
if tokens+tks >= session.Model.MaxContext { if tokens+tks >= session.Model.MaxContext {

View File

@ -54,9 +54,10 @@
padding 10px 10px 0 10px padding 10px 10px 0 10px
} }
.el-image { .logo {
height 50px height 50px
background-color #ffffff background-color #ffffff
border-radius 50%
} }
.el-button { .el-button {

View File

@ -41,7 +41,7 @@ import {computed, ref, watch} from "vue";
import SendMsg from "@/components/SendMsg.vue"; import SendMsg from "@/components/SendMsg.vue";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {httpPost} from "@/utils/http"; import {httpPost} from "@/utils/http";
import {checkSession, removeUserInfo} from "@/store/cache"; import {checkSession} from "@/store/cache";
const props = defineProps({ const props = defineProps({
show: Boolean, show: Boolean,
@ -76,7 +76,6 @@ const save = () => {
} }
httpPost('/api/user/bind/email', form.value).then(() => { httpPost('/api/user/bind/email', form.value).then(() => {
removeUserInfo()
ElMessage.success("绑定成功") ElMessage.success("绑定成功")
emits('hide') emits('hide')
}).catch(e => { }).catch(e => {

View File

@ -41,7 +41,7 @@ import {computed, ref, watch} from "vue";
import SendMsg from "@/components/SendMsg.vue"; import SendMsg from "@/components/SendMsg.vue";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {httpPost} from "@/utils/http"; import {httpPost} from "@/utils/http";
import {checkSession, removeUserInfo} from "@/store/cache"; import {checkSession} from "@/store/cache";
const props = defineProps({ const props = defineProps({
show: Boolean, show: Boolean,
@ -79,7 +79,6 @@ const save = () => {
} }
httpPost('/api/user/bind/mobile', form.value).then(() => { httpPost('/api/user/bind/mobile', form.value).then(() => {
removeUserInfo()
ElMessage.success("绑定成功") ElMessage.success("绑定成功")
emits('hide') emits('hide')
}).catch(e => { }).catch(e => {

View File

@ -26,6 +26,12 @@ const routes = [
meta: {title: '创作中心'}, meta: {title: '创作中心'},
component: () => import('@/views/ChatPlus.vue'), component: () => import('@/views/ChatPlus.vue'),
}, },
{
name: 'chat-id',
path: '/chat/:id',
meta: {title: '创作中心'},
component: () => import('@/views/ChatPlus.vue'),
},
{ {
name: 'image-mj', name: 'image-mj',
path: '/mj', path: '/mj',

View File

@ -6,29 +6,15 @@ const adminDataKey = "ADMIN_INFO_CACHE_KEY"
const systemInfoKey = "SYSTEM_INFO_CACHE_KEY" const systemInfoKey = "SYSTEM_INFO_CACHE_KEY"
const licenseInfoKey = "LICENSE_INFO_CACHE_KEY" const licenseInfoKey = "LICENSE_INFO_CACHE_KEY"
export function checkSession() { export function checkSession() {
const item = Storage.get(userDataKey) ?? {expire:0, data:null}
if (item.expire > Date.now()) {
return Promise.resolve(item.data)
}
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
httpGet('/api/user/session').then(res => { httpGet('/api/user/session').then(res => {
item.data = res.data resolve(res.data)
// cache expires after 10 secs
item.expire = Date.now() + 1000 * 30
Storage.set(userDataKey, item)
resolve(item.data)
}).catch(e => { }).catch(e => {
Storage.remove(userDataKey) Storage.remove(userDataKey)
reject(e) reject(e)
}) })
}) })
} }
export function removeUserInfo() {
Storage.remove(userDataKey)
}
export function checkAdminSession() { export function checkAdminSession() {
const item = Storage.get(adminDataKey) ?? {expire:0, data:null} const item = Storage.get(adminDataKey) ?? {expire:0, data:null}
if (item.expire > Date.now()) { if (item.expire > Date.now()) {
@ -63,7 +49,7 @@ export function getSystemInfo() {
Storage.set(systemInfoKey, item) Storage.set(systemInfoKey, item)
resolve(item.data) resolve(item.data)
}).catch(err => { }).catch(err => {
resolve(err) reject(err)
}) })
}) })
} }

View File

@ -1,6 +1,6 @@
import {randString} from "@/utils/libs"; import {randString} from "@/utils/libs";
import Storage from "good-storage"; import Storage from "good-storage";
import {checkAdminSession, checkSession, removeAdminInfo, removeUserInfo} from "@/store/cache"; import {removeAdminInfo} from "@/store/cache";
/** /**
* storage handler * storage handler
@ -24,7 +24,6 @@ export function setUserToken(token) {
export function removeUserToken() { export function removeUserToken() {
Storage.remove(UserTokenKey) Storage.remove(UserTokenKey)
removeUserInfo()
} }
export function getAdminToken() { export function getAdminToken() {

View File

@ -3,7 +3,7 @@
<el-container> <el-container>
<el-aside> <el-aside>
<div class="chat-list"> <div class="chat-list">
<el-button @click="newChat" color="#21aa93"> <el-button @click="_newChat" color="#21aa93">
<el-icon style="margin-right: 5px"> <el-icon style="margin-right: 5px">
<Plus/> <Plus/>
</el-icon> </el-icon>
@ -23,7 +23,7 @@
<div class="content" :style="{height: leftBoxHeight+'px'}"> <div class="content" :style="{height: leftBoxHeight+'px'}">
<el-row v-for="chat in chatList" :key="chat.chat_id"> <el-row v-for="chat in chatList" :key="chat.chat_id">
<div :class="chat.chat_id === activeChat.chat_id?'chat-list-item active':'chat-list-item'" <div :class="chat.chat_id === chatId?'chat-list-item active':'chat-list-item'"
@click="loadChat(chat)"> @click="loadChat(chat)">
<el-image :src="chat.icon" class="avatar"/> <el-image :src="chat.icon" class="avatar"/>
<span class="chat-title-input" v-if="chat.edit"> <span class="chat-title-input" v-if="chat.edit">
@ -211,7 +211,7 @@ import {
UUID UUID
} from "@/utils/libs"; } from "@/utils/libs";
import {ElMessage, ElMessageBox} from "element-plus"; import {ElMessage, ElMessageBox} from "element-plus";
import {getSessionId, getUserToken, removeUserToken} from "@/store/session"; import {getSessionId, getUserToken} from "@/store/session";
import {httpGet, httpPost} from "@/utils/http"; import {httpGet, httpPost} from "@/utils/http";
import {useRouter} from "vue-router"; import {useRouter} from "vue-router";
import Clipboard from "clipboard"; import Clipboard from "clipboard";
@ -222,7 +222,6 @@ import FileSelect from "@/components/FileSelect.vue";
import FileList from "@/components/FileList.vue"; import FileList from "@/components/FileList.vue";
import ChatSetting from "@/components/ChatSetting.vue"; import ChatSetting from "@/components/ChatSetting.vue";
import BackTop from "@/components/BackTop.vue"; import BackTop from "@/components/BackTop.vue";
import {showMessageError} from "@/utils/dialog";
const title = ref('GeekAI-智能助手'); const title = ref('GeekAI-智能助手');
const models = ref([]) const models = ref([])
@ -230,15 +229,15 @@ const modelID = ref(0)
const chatData = ref([]); const chatData = ref([]);
const allChats = ref([]); // const allChats = ref([]); //
const chatList = ref(allChats.value); const chatList = ref(allChats.value);
const activeChat = ref({});
const mainWinHeight = ref(0); // const mainWinHeight = ref(0); //
const chatBoxHeight = ref(0); // const chatBoxHeight = ref(0); //
const leftBoxHeight = ref(0); const leftBoxHeight = ref(0);
const loading = ref(true); const loading = ref(false);
const loginUser = ref(null); const loginUser = ref(null);
const roles = ref([]); const roles = ref([]);
const router = useRouter(); const router = useRouter();
const roleId = ref(0) const roleId = ref(0)
const chatId = ref();
const newChatItem = ref(null); const newChatItem = ref(null);
const isLogin = ref(false) const isLogin = ref(false)
const showHello = ref(true) const showHello = ref(true)
@ -255,6 +254,11 @@ watch(() => store.chatListStyle, (newValue) => {
listStyle.value = newValue listStyle.value = newValue
}); });
// ChatID
chatId.value = router.currentRoute.value.params.id
if (!chatId.value) {
chatId.value = UUID()
}
if (isMobile()) { if (isMobile()) {
router.replace("/mobile/chat") router.replace("/mobile/chat")
@ -351,7 +355,6 @@ const initData = () => {
ElMessage.error("加载会话列表失败!") ElMessage.error("加载会话列表失败!")
}) })
}).catch(() => { }).catch(() => {
loading.value = false
// //
httpGet('/api/model/list',{id:roleId.value}).then(res => { httpGet('/api/model/list',{id:roleId.value}).then(res => {
models.value = res.data models.value = res.data
@ -418,6 +421,7 @@ const resizeElement = function () {
const _newChat = () => { const _newChat = () => {
if (isLogin.value) { if (isLogin.value) {
chatId.value = UUID()
newChat() newChat()
} }
} }
@ -428,6 +432,7 @@ const newChat = () => {
store.setShowLoginDialog(true) store.setShowLoginDialog(true)
return; return;
} }
const role = getRoleById(roleId.value) const role = getRoleById(roleId.value)
showHello.value = role.key === 'gpt'; showHello.value = role.key === 'gpt';
// if the role bind a model, disable model change // if the role bind a model, disable model change
@ -457,9 +462,9 @@ const newChat = () => {
edit: false, edit: false,
removing: false, removing: false,
}; };
activeChat.value = {} //
showStopGenerate.value = false; showStopGenerate.value = false;
connect(null, roleId.value) router.push(`/chat/${chatId.value}`)
connect()
} }
@ -470,16 +475,17 @@ const loadChat = function (chat) {
return; return;
} }
if (activeChat.value['chat_id'] === chat.chat_id) { if (chatId.value === chat.chat_id) {
return; return;
} }
activeChat.value = chat
newChatItem.value = null; newChatItem.value = null;
roleId.value = chat.role_id; roleId.value = chat.role_id;
modelID.value = chat.model_id; modelID.value = chat.model_id;
chatId.value = chat.chat_id;
showStopGenerate.value = false; showStopGenerate.value = false;
connect(chat.chat_id, chat.role_id) router.push(`/chat/${chatId.value}`)
socket.value.close()
} }
// //
@ -487,7 +493,6 @@ const tmpChatTitle = ref('');
const editChatTitle = (chat) => { const editChatTitle = (chat) => {
chat.edit = true; chat.edit = true;
tmpChatTitle.value = chat.title; tmpChatTitle.value = chat.title;
console.log(chat.chat_id)
nextTick(() => { nextTick(() => {
document.getElementById('chat-' + chat.chat_id).focus() document.getElementById('chat-' + chat.chat_id).focus()
}) })
@ -557,23 +562,10 @@ const prompt = ref('');
const showStopGenerate = ref(false); // const showStopGenerate = ref(false); //
const lineBuffer = ref(''); // const lineBuffer = ref(''); //
const socket = ref(null); const socket = ref(null);
const activelyClose = ref(false); //
const canSend = ref(true); const canSend = ref(true);
const heartbeatHandle = ref(null)
const sessionId = ref("") const sessionId = ref("")
const connect = function (chat_id, role_id) { const connect = function () {
let isNewChat = false; const chatRole = getRoleById(roleId.value);
if (!chat_id) {
isNewChat = true;
chat_id = UUID();
}
//
if (socket.value !== null) {
activelyClose.value = true;
socket.value.close();
}
const _role = getRoleById(role_id);
// WebSocket // WebSocket
sessionId.value = getSessionId(); sessionId.value = getSessionId();
let host = process.env.VUE_APP_WS_HOST let host = process.env.VUE_APP_WS_HOST
@ -585,26 +577,12 @@ const connect = function (chat_id, role_id) {
} }
} }
const _socket = new WebSocket(host + `/api/chat/new?session_id=${sessionId.value}&role_id=${role_id}&chat_id=${chat_id}&model_id=${modelID.value}&token=${getUserToken()}`); loading.value = true
const _socket = new WebSocket(host + `/api/chat/new?session_id=${sessionId.value}&role_id=${roleId.value}&chat_id=${chatId.value}&model_id=${modelID.value}&token=${getUserToken()}`);
_socket.addEventListener('open', () => { _socket.addEventListener('open', () => {
chatData.value = []; //
enableInput() enableInput()
activelyClose.value = false; loadChatHistory(chatId.value)
loading.value = false
if (isNewChat) { //
loading.value = false;
chatData.value.push({
chat_id: chat_id,
role_id: role_id,
type: "reply",
id: randString(32),
icon: _role['icon'],
content: _role['hello_msg'],
})
ElMessage.success({message: "对话连接成功!", duration: 1000})
} else { //
loadChatHistory(chat_id);
}
}); });
_socket.addEventListener('message', event => { _socket.addEventListener('message', event => {
@ -619,17 +597,16 @@ const connect = function (chat_id, role_id) {
chatData.value.push({ chatData.value.push({
type: "reply", type: "reply",
id: randString(32), id: randString(32),
icon: _role['icon'], icon: chatRole['icon'],
prompt:prePrompt, prompt:prePrompt,
content: "", content: "",
}); });
} else if (data.type === 'end') { // } else if (data.type === 'end') { //
// //
if (isNewChat && newChatItem.value !== null) { if (newChatItem.value !== null) {
newChatItem.value['title'] = tmpChatTitle.value; newChatItem.value['title'] = tmpChatTitle.value;
newChatItem.value['chat_id'] = chat_id; newChatItem.value['chat_id'] = chatId.value;
chatList.value.unshift(newChatItem.value); chatList.value.unshift(newChatItem.value);
activeChat.value = newChatItem.value;
newChatItem.value = null; // newChatItem.value = null; //
} }
@ -641,7 +618,7 @@ const connect = function (chat_id, role_id) {
httpPost("/api/chat/tokens", { httpPost("/api/chat/tokens", {
text: "", text: "",
model: getModelValue(modelID.value), model: getModelValue(modelID.value),
chat_id: chat_id chat_id: chatId.value,
}).then(res => { }).then(res => {
reply['created_at'] = new Date().getTime(); reply['created_at'] = new Date().getTime();
reply['tokens'] = res.data; reply['tokens'] = res.data;
@ -662,7 +639,7 @@ const connect = function (chat_id, role_id) {
// //
nextTick(() => { nextTick(() => {
document.getElementById('chat-box').scrollTo(0, document.getElementById('chat-box').scrollHeight) document.getElementById('chat-box').scrollTo(0, document.getElementById('chat-box').scrollHeight)
localStorage.setItem("chat_id", chat_id) localStorage.setItem("chat_id", chatId.value)
}) })
}; };
} }
@ -673,18 +650,8 @@ const connect = function (chat_id, role_id) {
}); });
_socket.addEventListener('close', () => { _socket.addEventListener('close', () => {
if (activelyClose.value || socket.value === null) { //
return;
}
//
disableInput(true) disableInput(true)
loading.value = true; connect()
checkSession().then(() => {
connect(chat_id, role_id)
}).catch(() => {
loading.value = true
showMessageError("会话已断开,刷新页面...")
});
}); });
socket.value = _socket; socket.value = _socket;
@ -801,21 +768,20 @@ const clearAllChats = function () {
}) })
} }
const logout = function () {
activelyClose.value = true;
httpGet('/api/user/logout').then(() => {
removeUserToken()
router.push("/login")
}).catch(() => {
ElMessage.error('注销失败!');
})
}
const loadChatHistory = function (chatId) { const loadChatHistory = function (chatId) {
chatData.value = []
httpGet('/api/chat/history?chat_id=' + chatId).then(res => { httpGet('/api/chat/history?chat_id=' + chatId).then(res => {
const data = res.data const data = res.data
if (!data) { if (!data || data.length === 0) { //
loading.value = false const _role = getRoleById(roleId.value)
chatData.value.push({
chat_id: chatId,
role_id: roleId.value,
type: "reply",
id: randString(32),
icon: _role['icon'],
content: _role['hello_msg'],
})
return return
} }
showHello.value = false showHello.value = false
@ -829,7 +795,6 @@ const loadChatHistory = function (chatId) {
nextTick(() => { nextTick(() => {
document.getElementById('chat-box').scrollTo(0, document.getElementById('chat-box').scrollHeight) document.getElementById('chat-box').scrollTo(0, document.getElementById('chat-box').scrollHeight)
}) })
loading.value = false
}).catch(e => { }).catch(e => {
// TODO: // TODO:
ElMessage.error('加载聊天记录失败:' + e.message); ElMessage.error('加载聊天记录失败:' + e.message);
@ -882,7 +847,6 @@ const shareChat = (chat) => {
} }
const url = location.protocol + '//' + location.host + '/chat/export?chat_id=' + chat.chat_id const url = location.protocol + '//' + location.host + '/chat/export?chat_id=' + chat.chat_id
// console.log(url)
window.open(url, '_blank'); window.open(url, '_blank');
} }

View File

@ -7,7 +7,7 @@
:ellipsis="false" :ellipsis="false"
> >
<div class="menu-item"> <div class="menu-item">
<el-image :src="logo" alt="Geek-AI"/> <el-image :src="logo" class="logo" alt="Geek-AI"/>
<div class="title" :style="{color:theme.textColor}">{{ title }}</div> <div class="title" :style="{color:theme.textColor}">{{ title }}</div>
</div> </div>
<div class="menu-item"> <div class="menu-item">

View File

@ -42,7 +42,7 @@ import {setUserToken} from "@/store/session";
import Clipboard from "clipboard"; import Clipboard from "clipboard";
import {showMessageError, showMessageOK} from "@/utils/dialog"; import {showMessageError, showMessageOK} from "@/utils/dialog";
import {getRoute} from "@/store/system"; import {getRoute} from "@/store/system";
import {checkSession, removeUserInfo} from "@/store/cache"; import {checkSession} from "@/store/cache";
const winHeight = ref(window.innerHeight) const winHeight = ref(window.innerHeight)
const loading = ref(true) const loading = ref(true)
@ -68,7 +68,6 @@ if (code === "") {
const doLogin = (userId) => { const doLogin = (userId) => {
// //
httpGet("/api/user/clogin/callback",{login_type: "wx",code: code, action:action, user_id: userId}).then(res => { httpGet("/api/user/clogin/callback",{login_type: "wx",code: code, action:action, user_id: userId}).then(res => {
removeUserInfo()
if (res.data.token) { if (res.data.token) {
setUserToken(res.data.token) setUserToken(res.data.token)
} }

View File

@ -225,10 +225,7 @@ const newChat = (item) => {
} }
showPicker.value = false showPicker.value = false
const options = item.selectedOptions const options = item.selectedOptions
router.push({ router.push(`/mobile/chat/session?title=新对话&role_id=${options[0].value}&model_id=${options[1].value}&chat_id=0}`)
name: "mobile-chat-session",
params: {role_id: options[0].value, model_id: options[1].value, title: '新建会话', chat_id: 0}
})
} }
const changeChat = (chat) => { const changeChat = (chat) => {

View File

@ -102,6 +102,7 @@
<van-popup v-model:show="showPicker" position="bottom" class="popup"> <van-popup v-model:show="showPicker" position="bottom" class="popup">
<van-picker <van-picker
:columns="columns" :columns="columns"
v-model="selectedValues"
title="选择模型和角色" title="选择模型和角色"
@cancel="showPicker = false" @cancel="showPicker = false"
@confirm="newChat" @confirm="newChat"
@ -153,6 +154,7 @@ const loginUser = ref(null)
// const showMic = ref(false) // const showMic = ref(false)
const showPicker = ref(false) const showPicker = ref(false)
const columns = ref([roles.value, models.value]) const columns = ref([roles.value, models.value])
const selectedValues = ref([roleId.value, modelId.value])
checkSession().then(user => { checkSession().then(user => {
loginUser.value = user loginUser.value = user

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="mobile-user-profile container"> <div class="mobile-user-profile container">
<div class="content"> <div class="content">
<van-form> <van-form v-if="isLogin">
<div class="avatar"> <div class="avatar">
<van-image :src="fileList[0].url" size="80" width="80" fit="cover" round /> <van-image :src="fileList[0].url" size="80" width="80" fit="cover" round />
<!-- <van-uploader v-model="fileList"--> <!-- <van-uploader v-model="fileList"-->