From de088445f68f02b5e68e2ec11a7aaf1cd613adbe Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Fri, 7 Apr 2023 11:25:49 +0800 Subject: [PATCH] add nav --- src/components/common/LeftNavigator.vue | 96 +++++++++++++++++++++++++ src/components/common/NavigatorItem.vue | 59 +++++++++++++++ src/i18n/zh/common/nav.ts | 6 +- src/layouts/Conversation.vue | 9 ++- src/operators/conversation/models.ts | 7 ++ src/pages/conversation/Main.vue | 41 +++++++++++ src/store/index.ts | 14 +++- 7 files changed, 227 insertions(+), 5 deletions(-) create mode 100644 src/components/common/LeftNavigator.vue create mode 100644 src/components/common/NavigatorItem.vue create mode 100644 src/operators/conversation/models.ts diff --git a/src/components/common/LeftNavigator.vue b/src/components/common/LeftNavigator.vue new file mode 100644 index 00000000..92c3c43c --- /dev/null +++ b/src/components/common/LeftNavigator.vue @@ -0,0 +1,96 @@ + + + + + diff --git a/src/components/common/NavigatorItem.vue b/src/components/common/NavigatorItem.vue new file mode 100644 index 00000000..4476b43b --- /dev/null +++ b/src/components/common/NavigatorItem.vue @@ -0,0 +1,59 @@ + + + + + diff --git a/src/i18n/zh/common/nav.ts b/src/i18n/zh/common/nav.ts index 4f10a3fd..48d403a0 100644 --- a/src/i18n/zh/common/nav.ts +++ b/src/i18n/zh/common/nav.ts @@ -9,5 +9,9 @@ export default { category: '分类', service: '服务', document: '文档', - console: '控制台' + console: '控制台', + clearConversations: '清除对话', + newChat: '新建对话', + logOut: '退出登录', + confirmClearConversations: '确认清除' }; diff --git a/src/layouts/Conversation.vue b/src/layouts/Conversation.vue index 24299abb..2e1cdf2c 100644 --- a/src/layouts/Conversation.vue +++ b/src/layouts/Conversation.vue @@ -1,5 +1,8 @@ @@ -7,11 +10,13 @@ @@ -33,8 +38,6 @@ $width: 300px; width: $width; height: 100%; background-color: white; - padding: 15px; - padding-top: 50px; } .header { diff --git a/src/operators/conversation/models.ts b/src/operators/conversation/models.ts new file mode 100644 index 00000000..86afdd47 --- /dev/null +++ b/src/operators/conversation/models.ts @@ -0,0 +1,7 @@ +import { IMessage } from '../message/models'; + +export interface IConversation { + messages: IMessage[]; + title: string; + id: string; +} diff --git a/src/pages/conversation/Main.vue b/src/pages/conversation/Main.vue index b787734f..355d0d26 100644 --- a/src/pages/conversation/Main.vue +++ b/src/pages/conversation/Main.vue @@ -56,6 +56,7 @@ import { import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL } from '@/router/constants'; import { ERROR_CODE_DUPLICATION, ERROR_CODE_UNVERIFIED } from '@/constants'; import { getVerificationUrl } from '@/utils'; +import { IConversation } from '@/operators/conversation/models'; export interface IData { input: string; @@ -94,6 +95,13 @@ export default defineComponent({ return this.$store.getters.user; } }, + watch: { + conversationId: { + handler() { + this.restoreMessages(); + } + } + }, async mounted() { if (!this.$store.getters.authenticated) { this.$router.push({ @@ -101,11 +109,24 @@ export default defineComponent({ query: { redirect: this.$route.fullPath } }); } else { + this.restoreMessages(); await this.onGetApiInfo(); await this.onCheckApplication(); } }, methods: { + restoreMessages() { + if (this.conversationId) { + const conversations = this.$store.getters.conversations; + const hitConversations = conversations.filter((c: IConversation) => c.id === this.conversationId); + if (hitConversations && hitConversations.length > 0) { + const hitConversation = hitConversations[0]; + this.messages = hitConversation.messages; + } + } else { + this.messages = []; + } + }, async onGetApiInfo() { return new Promise((resolve, reject) => { const id = '1d58971c-e3cd-4713-a3ce-854a731adb14'; @@ -242,6 +263,26 @@ export default defineComponent({ ) .then(() => { this.answering = false; + const conversations: IConversation[] = this.$store.getters.conversations; + const hitConversations = conversations.filter((c: IConversation) => c.id === this.conversationId); + if (!hitConversations || hitConversations.length === 0) { + // append a new conversation to conversations + const newConversations = [ + ...conversations, + { + id: this.conversationId, + messages: this.messages, + title: this.conversationId + } + ]; + this.$store.dispatch('setConversations', newConversations); + } else { + // update one of existing conversations + conversations.forEach((conversation: IConversation) => { + conversation.messages = this.messages; + }); + this.$store.dispatch('setConversations', conversations); + } }) .catch((error) => { this.answering = false; diff --git a/src/store/index.ts b/src/store/index.ts index 87552b31..61145161 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -2,11 +2,13 @@ import { createStore, ActionContext } from 'vuex'; import createPersistedState from 'vuex-persistedstate'; import { userOperator, IUser } from '@/operators'; import { removeCookies } from '@/utils/cookie'; +import { IConversation } from '@/operators/conversation/models'; export interface IState { accessToken: string | undefined; refreshToken: string | undefined; user: IUser | undefined; + conversations: IConversation[]; } const store = createStore({ @@ -14,7 +16,8 @@ const store = createStore({ return { accessToken: undefined, refreshToken: undefined, - user: undefined + user: undefined, + conversations: [] }; }, mutations: { @@ -26,6 +29,9 @@ const store = createStore({ }, setUser(state: IState, payload: IUser): void { state.user = payload; + }, + setConversations(state: IState, payload: IConversation[]): void { + state.conversations = payload; } }, actions: { @@ -44,6 +50,9 @@ const store = createStore({ setUser({ commit }: ActionContext, payload: IUser) { commit('setUser', payload); }, + setConversations({ commit }: ActionContext, payload: IConversation[]) { + commit('setConversations', payload); + }, getMe({ commit }: ActionContext) { userOperator .getMe() @@ -67,6 +76,9 @@ const store = createStore({ }, user(state): IUser | undefined { return state.user; + }, + conversations(state): IConversation[] { + return state.conversations || []; } }, plugins: [createPersistedState()]