From e5b90372a666a7a2a6e3808870b2cfe0fd9c9adc Mon Sep 17 00:00:00 2001 From: dylanmay <670374839@qq.com> Date: Tue, 12 Nov 2024 09:12:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/store/indexedDB.ts | 72 ++++++++++++++++ src/views/chat/ChatPage/Index.vue | 15 ++-- src/views/chat/api/messageApi.ts | 2 +- .../chat/components/ChatMessage/Index.vue | 2 +- .../Index.vue => Conversation/index.vue} | 8 +- .../Index.vue => ConversationItem/index.vue} | 7 +- .../chat/components/InputSection/Index.vue | 18 +++- src/views/chat/components/Message/BaseMsg.vue | 2 +- .../chat/components/Message/ImageMsg.vue | 2 +- .../chat/components/ToolSection/Index.vue | 2 +- src/views/chat/model/BaseConversation.ts | 2 +- src/views/chat/model/BaseMessage.ts | 5 +- src/views/chat/model/ImageMessage.ts | 2 +- src/views/chat/model/TextMessage.ts | 27 +++++- src/views/chat/store/chatstore.ts | 84 ++++++++++++++++++- 16 files changed, 222 insertions(+), 29 deletions(-) create mode 100644 src/store/indexedDB.ts rename src/views/chat/components/{Session/Index.vue => Conversation/index.vue} (77%) rename src/views/chat/components/{SessionItem/Index.vue => ConversationItem/index.vue} (95%) diff --git a/package.json b/package.json index ba5400092..c1b8c358f 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "element-plus": "2.8.4", "fast-xml-parser": "^4.3.2", "highlight.js": "^11.9.0", + "idb": "^8.0.0", "jsencrypt": "^3.3.2", "lodash-es": "^4.17.21", "markdown-it": "^14.1.0", diff --git a/src/store/indexedDB.ts b/src/store/indexedDB.ts new file mode 100644 index 000000000..d14278996 --- /dev/null +++ b/src/store/indexedDB.ts @@ -0,0 +1,72 @@ +import { ConversationModelType } from '@/views/chat/types/types' +import { openDB, DBSchema, IDBPDatabase } from 'idb' + +// Define your database schema +interface MyDB extends DBSchema { + Conversations: { + key: string + value: ConversationModelType + } +} + +let dbPromise: Promise> + +export const initDB = () => { + if (!dbPromise) { + try { + dbPromise = openDB('yudao-im-indexeddb', 1, { + upgrade(db) { + db.createObjectStore('Conversations', { keyPath: 'conversationNo' }) + } + }) + } catch (error) { + console.log(error) + } + } + return dbPromise +} + +export const addConversation = async (conversation: ConversationModelType) => { + + try { + const db = await initDB() + await db.put('Conversations', conversation) + } catch (error) { + console.error(conversation) + console.error(error) + } + +} + +export const getConversation = async (conversationNo: string) => { + + try { + const db = await initDB() + return await db.get('Conversations', conversationNo) + } catch (error) { + console.error(error) + } + +} + +export const deleteConversation = async (conversationNo: string) => { + + try { + const db = await initDB() + await db.delete('Conversations', conversationNo) + } catch (error) { + console.error(error) + } + +} + +export const getAllConversations = async () => { + + try { + const db = await initDB() + return await db.getAll('Conversations') + } catch (error) { + console.log(error) + } + +} diff --git a/src/views/chat/ChatPage/Index.vue b/src/views/chat/ChatPage/Index.vue index 3a1a604f2..dae352481 100644 --- a/src/views/chat/ChatPage/Index.vue +++ b/src/views/chat/ChatPage/Index.vue @@ -18,17 +18,22 @@ */ import ToolSection from '../components/ToolSection/Index.vue' -import Session from '../components/Session/Index.vue' +import Session from '../components/Conversation/index.vue' import Friends from '../components/Friends/Index.vue' -import ChatHeader from '../components/ChatHeader/Index.vue' // TODO @dylan:为啥这个 index.vue 是大写哈?可以搞成小写哇? -import ChatMessage from '../components/ChatMessage/Index.vue' -import InputSection from '../components/InputSection/Index.vue' +import ChatHeader from '../components/ChatHeader/index.vue' +import ChatMessage from '../components/ChatMessage/index.vue' +import InputSection from '../components/InputSection/index.vue' import FriendDetail from '../components/FriendDetail/Index.vue' -import { MENU_LIST_ENUM } from '../types/index.d.ts' +import { MENU_LIST_ENUM } from '../types/types' +import { useWebSocketStore } from '../store/websocketStore' defineOptions({ name: 'ChatPage' }) const bussinessType = ref(1) +const webSocketStore = useWebSocketStore(); +onMounted(() => { + webSocketStore.connect() +}) const toolMenuSelectChange = (value) => { bussinessType.value = value diff --git a/src/views/chat/api/messageApi.ts b/src/views/chat/api/messageApi.ts index d086b6396..5a1290fa2 100644 --- a/src/views/chat/api/messageApi.ts +++ b/src/views/chat/api/messageApi.ts @@ -6,7 +6,7 @@ */ import request from '@/config/axios' -import { MessageModelType } from '../types' +import { MessageModelType } from '../types/types' export interface SendMsg { clientMessageId: string diff --git a/src/views/chat/components/ChatMessage/Index.vue b/src/views/chat/components/ChatMessage/Index.vue index 06027f838..b972b03f6 100644 --- a/src/views/chat/components/ChatMessage/Index.vue +++ b/src/views/chat/components/ChatMessage/Index.vue @@ -24,7 +24,7 @@ import { useChatStore } from '../../store/chatstore' import TextMsg from '@/views/chat/components/Message/TextMsg.vue' import ImageMsg from '@/views/chat/components/Message/ImageMsg.vue' -import { ContentType } from '../../types/index.d.ts' +import { ContentType } from '../../types/types' defineOptions({ name: 'ChatMessage' }) diff --git a/src/views/chat/components/Session/Index.vue b/src/views/chat/components/Conversation/index.vue similarity index 77% rename from src/views/chat/components/Session/Index.vue rename to src/views/chat/components/Conversation/index.vue index 1db5009fe..6aa3e1945 100644 --- a/src/views/chat/components/Session/Index.vue +++ b/src/views/chat/components/Conversation/index.vue @@ -3,7 +3,7 @@