diff --git a/CHANGELOG.md b/CHANGELOG.md index 50a207e1..867f9b27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * 功能优化:重构找回密码模块,支持通过手机或者邮箱找回密码 * 功能优化:管理后台给可以拖动排序的组件添加拖动图标 * 功能优化:Suno 支持合成完整歌曲,和上传自己的音乐作品进行二次创作 +* Bug修复:手机端角色和模型选择不生效 +* Bug修复:用户登录过期之后聊天页面出现大量报错,需要刷新页面才能正常 * 功能新增:支持 Luma 文生视频功能 ## v4.1.2 diff --git a/api/core/app_server.go b/api/core/app_server.go index 92967067..0eebbc93 100644 --- a/api/core/app_server.go +++ b/api/core/app_server.go @@ -201,7 +201,6 @@ func needLogin(c *gin.Context) bool { c.Request.URL.Path == "/api/admin/logout" || c.Request.URL.Path == "/api/admin/login/captcha" || 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/detail" || c.Request.URL.Path == "/api/chat/list" || diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index 6b1e9f8e..ef45883d 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -270,7 +270,8 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio tks, _ := utils.CalcTokens(utils.JsonEncode(req.Tools), req.Model) 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) // 上下文 token 超出了模型的最大上下文长度 if tokens+tks >= session.Model.MaxContext { diff --git a/web/src/assets/css/index.styl b/web/src/assets/css/index.styl index f2e8aa0c..c63c22d7 100644 --- a/web/src/assets/css/index.styl +++ b/web/src/assets/css/index.styl @@ -54,9 +54,10 @@ padding 10px 10px 0 10px } - .el-image { + .logo { height 50px background-color #ffffff + border-radius 50% } .el-button { diff --git a/web/src/components/BindEmail.vue b/web/src/components/BindEmail.vue index 1ae9e773..719f9c70 100644 --- a/web/src/components/BindEmail.vue +++ b/web/src/components/BindEmail.vue @@ -41,7 +41,7 @@ import {computed, ref, watch} from "vue"; import SendMsg from "@/components/SendMsg.vue"; import {ElMessage} from "element-plus"; import {httpPost} from "@/utils/http"; -import {checkSession, removeUserInfo} from "@/store/cache"; +import {checkSession} from "@/store/cache"; const props = defineProps({ show: Boolean, @@ -76,7 +76,6 @@ const save = () => { } httpPost('/api/user/bind/email', form.value).then(() => { - removeUserInfo() ElMessage.success("绑定成功") emits('hide') }).catch(e => { diff --git a/web/src/components/BindMobile.vue b/web/src/components/BindMobile.vue index 41eceb3f..3f6e15e1 100644 --- a/web/src/components/BindMobile.vue +++ b/web/src/components/BindMobile.vue @@ -41,7 +41,7 @@ import {computed, ref, watch} from "vue"; import SendMsg from "@/components/SendMsg.vue"; import {ElMessage} from "element-plus"; import {httpPost} from "@/utils/http"; -import {checkSession, removeUserInfo} from "@/store/cache"; +import {checkSession} from "@/store/cache"; const props = defineProps({ show: Boolean, @@ -79,7 +79,6 @@ const save = () => { } httpPost('/api/user/bind/mobile', form.value).then(() => { - removeUserInfo() ElMessage.success("绑定成功") emits('hide') }).catch(e => { diff --git a/web/src/router.js b/web/src/router.js index 7d2a2908..5d0ff0ac 100644 --- a/web/src/router.js +++ b/web/src/router.js @@ -26,6 +26,12 @@ const routes = [ meta: {title: '创作中心'}, component: () => import('@/views/ChatPlus.vue'), }, + { + name: 'chat-id', + path: '/chat/:id', + meta: {title: '创作中心'}, + component: () => import('@/views/ChatPlus.vue'), + }, { name: 'image-mj', path: '/mj', diff --git a/web/src/store/cache.js b/web/src/store/cache.js index 46206354..368a770e 100644 --- a/web/src/store/cache.js +++ b/web/src/store/cache.js @@ -6,29 +6,15 @@ const adminDataKey = "ADMIN_INFO_CACHE_KEY" const systemInfoKey = "SYSTEM_INFO_CACHE_KEY" const licenseInfoKey = "LICENSE_INFO_CACHE_KEY" 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) => { httpGet('/api/user/session').then(res => { - item.data = res.data - // cache expires after 10 secs - item.expire = Date.now() + 1000 * 30 - Storage.set(userDataKey, item) - resolve(item.data) + resolve(res.data) }).catch(e => { Storage.remove(userDataKey) reject(e) }) }) } - -export function removeUserInfo() { - Storage.remove(userDataKey) -} - export function checkAdminSession() { const item = Storage.get(adminDataKey) ?? {expire:0, data:null} if (item.expire > Date.now()) { @@ -63,7 +49,7 @@ export function getSystemInfo() { Storage.set(systemInfoKey, item) resolve(item.data) }).catch(err => { - resolve(err) + reject(err) }) }) } diff --git a/web/src/store/session.js b/web/src/store/session.js index 3f023555..4d4f9d84 100644 --- a/web/src/store/session.js +++ b/web/src/store/session.js @@ -1,6 +1,6 @@ import {randString} from "@/utils/libs"; import Storage from "good-storage"; -import {checkAdminSession, checkSession, removeAdminInfo, removeUserInfo} from "@/store/cache"; +import {removeAdminInfo} from "@/store/cache"; /** * storage handler @@ -24,7 +24,6 @@ export function setUserToken(token) { export function removeUserToken() { Storage.remove(UserTokenKey) - removeUserInfo() } export function getAdminToken() { diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index 1bb04204..c77a9972 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -3,7 +3,7 @@
- + @@ -23,7 +23,7 @@
-
@@ -211,7 +211,7 @@ import { UUID } from "@/utils/libs"; 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 {useRouter} from "vue-router"; import Clipboard from "clipboard"; @@ -222,7 +222,6 @@ import FileSelect from "@/components/FileSelect.vue"; import FileList from "@/components/FileList.vue"; import ChatSetting from "@/components/ChatSetting.vue"; import BackTop from "@/components/BackTop.vue"; -import {showMessageError} from "@/utils/dialog"; const title = ref('GeekAI-智能助手'); const models = ref([]) @@ -230,15 +229,15 @@ const modelID = ref(0) const chatData = ref([]); const allChats = ref([]); // 会话列表 const chatList = ref(allChats.value); -const activeChat = ref({}); const mainWinHeight = ref(0); // 主窗口高度 const chatBoxHeight = ref(0); // 聊天内容框高度 const leftBoxHeight = ref(0); -const loading = ref(true); +const loading = ref(false); const loginUser = ref(null); const roles = ref([]); const router = useRouter(); const roleId = ref(0) +const chatId = ref(); const newChatItem = ref(null); const isLogin = ref(false) const showHello = ref(true) @@ -255,6 +254,11 @@ watch(() => store.chatListStyle, (newValue) => { listStyle.value = newValue }); +// 初始化 ChatID +chatId.value = router.currentRoute.value.params.id +if (!chatId.value) { + chatId.value = UUID() +} if (isMobile()) { router.replace("/mobile/chat") @@ -351,7 +355,6 @@ const initData = () => { ElMessage.error("加载会话列表失败!") }) }).catch(() => { - loading.value = false // 加载模型 httpGet('/api/model/list',{id:roleId.value}).then(res => { models.value = res.data @@ -418,6 +421,7 @@ const resizeElement = function () { const _newChat = () => { if (isLogin.value) { + chatId.value = UUID() newChat() } } @@ -428,6 +432,7 @@ const newChat = () => { store.setShowLoginDialog(true) return; } + const role = getRoleById(roleId.value) showHello.value = role.key === 'gpt'; // if the role bind a model, disable model change @@ -457,9 +462,9 @@ const newChat = () => { edit: false, removing: false, }; - activeChat.value = {} //取消激活的会话高亮 showStopGenerate.value = false; - connect(null, roleId.value) + router.push(`/chat/${chatId.value}`) + connect() } @@ -470,16 +475,17 @@ const loadChat = function (chat) { return; } - if (activeChat.value['chat_id'] === chat.chat_id) { + if (chatId.value === chat.chat_id) { return; } - activeChat.value = chat newChatItem.value = null; roleId.value = chat.role_id; modelID.value = chat.model_id; + chatId.value = chat.chat_id; 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) => { chat.edit = true; tmpChatTitle.value = chat.title; - console.log(chat.chat_id) nextTick(() => { document.getElementById('chat-' + chat.chat_id).focus() }) @@ -557,23 +562,10 @@ const prompt = ref(''); const showStopGenerate = ref(false); // 停止生成 const lineBuffer = ref(''); // 输出缓冲行 const socket = ref(null); -const activelyClose = ref(false); // 主动关闭 const canSend = ref(true); -const heartbeatHandle = ref(null) const sessionId = ref("") -const connect = function (chat_id, role_id) { - let isNewChat = false; - if (!chat_id) { - isNewChat = true; - chat_id = UUID(); - } - // 先关闭已有连接 - if (socket.value !== null) { - activelyClose.value = true; - socket.value.close(); - } - - const _role = getRoleById(role_id); +const connect = function () { + const chatRole = getRoleById(roleId.value); // 初始化 WebSocket 对象 sessionId.value = getSessionId(); 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', () => { - chatData.value = []; // 初始化聊天数据 enableInput() - activelyClose.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); - } + loadChatHistory(chatId.value) + loading.value = false }); _socket.addEventListener('message', event => { @@ -619,17 +597,16 @@ const connect = function (chat_id, role_id) { chatData.value.push({ type: "reply", id: randString(32), - icon: _role['icon'], + icon: chatRole['icon'], prompt:prePrompt, content: "", }); } else if (data.type === 'end') { // 消息接收完毕 // 追加当前会话到会话列表 - if (isNewChat && newChatItem.value !== null) { + if (newChatItem.value !== null) { newChatItem.value['title'] = tmpChatTitle.value; - newChatItem.value['chat_id'] = chat_id; + newChatItem.value['chat_id'] = chatId.value; chatList.value.unshift(newChatItem.value); - activeChat.value = newChatItem.value; newChatItem.value = null; // 只追加一次 } @@ -641,7 +618,7 @@ const connect = function (chat_id, role_id) { httpPost("/api/chat/tokens", { text: "", model: getModelValue(modelID.value), - chat_id: chat_id + chat_id: chatId.value, }).then(res => { reply['created_at'] = new Date().getTime(); reply['tokens'] = res.data; @@ -662,7 +639,7 @@ const connect = function (chat_id, role_id) { // 将聊天框的滚动条滑动到最底部 nextTick(() => { 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', () => { - if (activelyClose.value || socket.value === null) { // 忽略主动关闭 - return; - } - // 停止发送消息 disableInput(true) - loading.value = true; - checkSession().then(() => { - connect(chat_id, role_id) - }).catch(() => { - loading.value = true - showMessageError("会话已断开,刷新页面...") - }); + connect() }); 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) { + chatData.value = [] httpGet('/api/chat/history?chat_id=' + chatId).then(res => { const data = res.data - if (!data) { - loading.value = false + if (!data || data.length === 0) { // 加载打招呼信息 + 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 } showHello.value = false @@ -829,7 +795,6 @@ const loadChatHistory = function (chatId) { nextTick(() => { document.getElementById('chat-box').scrollTo(0, document.getElementById('chat-box').scrollHeight) }) - loading.value = false }).catch(e => { // TODO: 显示重新加载按钮 ElMessage.error('加载聊天记录失败:' + e.message); @@ -882,7 +847,6 @@ const shareChat = (chat) => { } const url = location.protocol + '//' + location.host + '/chat/export?chat_id=' + chat.chat_id - // console.log(url) window.open(url, '_blank'); } diff --git a/web/src/views/Index.vue b/web/src/views/Index.vue index fdbd6e93..31573579 100644 --- a/web/src/views/Index.vue +++ b/web/src/views/Index.vue @@ -7,7 +7,7 @@ :ellipsis="false" >