diff --git a/src/components/chat/Sidebar.vue b/src/components/chat/Sidebar.vue index cb2a8049..0f95b8c6 100644 --- a/src/components/chat/Sidebar.vue +++ b/src/components/chat/Sidebar.vue @@ -113,7 +113,7 @@ export default defineComponent({ async onFetchApplications() { this.initializing = true; const { data: applications } = await applicationOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, api_id: [ CHAT_MODEL_CHATGPT.apiId, CHAT_MODEL_CHATGPT_16K.apiId, @@ -141,7 +141,7 @@ export default defineComponent({ const { data: { items: apiUsages } } = await apiUsageOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, // @ts-ignore application_id: this.applications?.map((application) => application.id), offset: 0, diff --git a/src/components/common/Navigator.vue b/src/components/common/Navigator.vue index cc33fd0e..399c5eca 100644 --- a/src/components/common/Navigator.vue +++ b/src/components/common/Navigator.vue @@ -51,7 +51,7 @@ export default defineComponent({ methods: { onLogout() { console.debug('logout'); - this.$store.dispatch('resetToken'); + this.$store.dispatch('common/resetToken'); this.$router.push({ name: ROUTE_AUTH_LOGIN }); } } diff --git a/src/components/common/SettingPanel.vue b/src/components/common/SettingPanel.vue index d02fd910..648990cf 100644 --- a/src/components/common/SettingPanel.vue +++ b/src/components/common/SettingPanel.vue @@ -43,20 +43,20 @@ export default defineComponent({ computed: { stream: { get() { - return this.$store.getters.setting.stream; + return this.$store.getters['common/setting'].stream; }, set(value: boolean) { - this.$store.dispatch('setSetting', { + this.$store.dispatch('common/setSetting', { stream: value }); } }, endpoint: { get() { - return this.$store.getters.setting.endpoint; + return this.$store.getters['common/setting'].endpoint; }, set(value: string) { - this.$store.dispatch('setSetting', { + this.$store.dispatch('common/setSetting', { endpoint: value }); } diff --git a/src/components/midjourney/PresetPanel.vue b/src/components/midjourney/PresetPanel.vue index af3eeef2..abaae5d2 100644 --- a/src/components/midjourney/PresetPanel.vue +++ b/src/components/midjourney/PresetPanel.vue @@ -64,8 +64,8 @@ export default defineComponent({ preset: { handler(val) { this.$emit('update:modelValue', val); - this.$store.dispatch('setMidjourney', { - preset: val + this.$store.dispatch('midjourney/setPreset', { + ...val }); }, deep: true, diff --git a/src/components/midjourney/ReferenceImage.vue b/src/components/midjourney/ReferenceImage.vue index 777715d9..0df469fd 100644 --- a/src/components/midjourney/ReferenceImage.vue +++ b/src/components/midjourney/ReferenceImage.vue @@ -50,7 +50,7 @@ export default defineComponent({ computed: { headers() { return { - Authorization: `Bearer ${this.$store.state.token.access}` + Authorization: `Bearer ${this.$store.state.common.token.access}` }; }, urls() { diff --git a/src/components/midjourney/tasks/TaskBriefList.vue b/src/components/midjourney/tasks/TaskBriefList.vue index 22c8a46d..97849a02 100644 --- a/src/components/midjourney/tasks/TaskBriefList.vue +++ b/src/components/midjourney/tasks/TaskBriefList.vue @@ -79,7 +79,7 @@ export default defineComponent({ const { data: { items: apiUsages } } = await apiUsageOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, application_id: this.applications?.map((application) => application.id), offset: 0, limit: 5, diff --git a/src/components/midjourney/tasks/TaskFullList.vue b/src/components/midjourney/tasks/TaskFullList.vue index 2a26f992..e69a4934 100644 --- a/src/components/midjourney/tasks/TaskFullList.vue +++ b/src/components/midjourney/tasks/TaskFullList.vue @@ -116,7 +116,7 @@ export default defineComponent({ const { data: { items: apiUsages, count } } = await apiUsageOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, application_id: this.applications?.map((application) => application.id), offset: (this.page - 1) * this.limit, limit: this.limit, diff --git a/src/operators/instance.ts b/src/operators/instance.ts index 97d07342..2a85a701 100644 --- a/src/operators/instance.ts +++ b/src/operators/instance.ts @@ -15,7 +15,7 @@ const httpClient: AxiosInstance = axios.create({ }); httpClient.interceptors.request.use((config) => { - const accessToken = store.getters.token.access; + const accessToken = store.state.common.token?.access; if (!config.headers) { config.headers = {}; } @@ -31,7 +31,7 @@ httpClient.interceptors.response.use( }, (error) => { if (error?.response?.status === 401) { - store.dispatch('resetAuth'); + store.dispatch('common/resetAuth'); router.push({ name: ROUTE_AUTH_LOGIN, query: { diff --git a/src/pages/auth/Callback.vue b/src/pages/auth/Callback.vue index fff0f4a6..5ce93b25 100644 --- a/src/pages/auth/Callback.vue +++ b/src/pages/auth/Callback.vue @@ -33,18 +33,18 @@ export default defineComponent({ } if (this.accessToken && this.refreshToken) { // store token to global store - await this.$store.dispatch('setToken', { + await this.$store.dispatch('common/setToken', { refresh: this.refreshToken, access: this.accessToken }); console.debug('set token successfully'); const { data: user } = await userOperator.getMe(); - await this.$store.dispatch('setUser', user); + await this.$store.dispatch('common/setUser', user); console.debug('set user successfully'); const { data: applications } = await applicationOperator.getAll({ user_id: user.id }); - await this.$store.dispatch('setApplications', applications); + await this.$store.dispatch('common/setApplications', applications); console.debug('set applications successfully'); if (this.redirect) { this.$router.push(this.redirect); diff --git a/src/pages/chat/Conversation.vue b/src/pages/chat/Conversation.vue index 14bd0c3c..a067bf66 100644 --- a/src/pages/chat/Conversation.vue +++ b/src/pages/chat/Conversation.vue @@ -105,7 +105,7 @@ export default defineComponent({ async onFetchApplications() { this.initializing = true; const { data: applications } = await applicationOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, api_id: [ CHAT_MODEL_CHATGPT.apiId, CHAT_MODEL_CHATGPT_16K.apiId, diff --git a/src/pages/midjourney/History.vue b/src/pages/midjourney/History.vue index a76dbcda..77dd6e73 100644 --- a/src/pages/midjourney/History.vue +++ b/src/pages/midjourney/History.vue @@ -73,7 +73,7 @@ export default defineComponent({ return { applications: [], channel: MIDJOURNEY_CHANNEL_FAST, - preset: this.$store.getters.midjourney.preset, + preset: this.$store.state.midjourney.preset, prompt: '', elements: [], ignore: '', @@ -103,7 +103,7 @@ export default defineComponent({ async onFetchApplications() { this.initializing = true; const { data: applications } = await applicationOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, api_id: [MIDJOURNEY_CHANNEL_FAST.apiId, MIDJOURNEY_CHANNEL_RELAX.apiId, MIDJOURNEY_CHANNEL_TURBO.apiId] }); this.initializing = false; diff --git a/src/pages/midjourney/Index.vue b/src/pages/midjourney/Index.vue index 4132e03f..9f2b9bc4 100644 --- a/src/pages/midjourney/Index.vue +++ b/src/pages/midjourney/Index.vue @@ -90,7 +90,7 @@ export default defineComponent({ return { applications: [], channel: MIDJOURNEY_CHANNEL_FAST, - preset: this.$store.getters.midjourney.preset || {}, + preset: this.$store.state.midjourney.preset || {}, prompt: '', elements: [], ignore: '', @@ -177,7 +177,7 @@ export default defineComponent({ async onFetchApplications() { this.initializing = true; const { data: applications } = await applicationOperator.getAll({ - user_id: this.$store.state.user.id, + user_id: this.$store.state.common.user.id, api_id: [MIDJOURNEY_CHANNEL_FAST.apiId, MIDJOURNEY_CHANNEL_RELAX.apiId, MIDJOURNEY_CHANNEL_TURBO.apiId] }); this.initializing = false; diff --git a/src/store/chat.ts b/src/store/chat.ts new file mode 100644 index 00000000..f462c654 --- /dev/null +++ b/src/store/chat.ts @@ -0,0 +1,22 @@ +import { IApplication } from '@/operators'; +import { Module } from 'vuex'; +import { IChatState } from './models'; + +export const chat: Module = { + namespaced: true, + state(): IChatState { + return { + applications: undefined + }; + }, + mutations: { + setApplications(state: IChatState, payload: IApplication[]): void { + state.applications = payload; + } + }, + actions: { + setApplications({ commit }: any, payload: IApplication[]) { + commit('setApplications', payload); + } + } +}; diff --git a/src/store/common.ts b/src/store/common.ts new file mode 100644 index 00000000..da941a89 --- /dev/null +++ b/src/store/common.ts @@ -0,0 +1,79 @@ +import { ENDPOINT } from '@/constants'; +import { IApplication, IUser } from '@/operators'; +import { ActionContext, Module } from 'vuex'; +import { ICommonState, IRootState, ISetting, IToken } from './models'; + +export const common: Module = { + namespaced: true, + state(): ICommonState { + return { + user: {}, + token: { + access: undefined, + refresh: undefined + }, + setting: { + endpoint: ENDPOINT, + stream: false + }, + applications: undefined + }; + }, + mutations: { + setUser(state: ICommonState, payload: IUser): void { + state.user = { + ...state.user, + ...payload + }; + }, + setToken(state: ICommonState, payload: IToken): void { + state.token = { + ...state.token, + ...payload + }; + }, + resetToken(state: ICommonState): void { + state.token = {}; + }, + setSetting(state: ICommonState, payload: ISetting): void { + state.setting = { + ...state.setting, + ...payload + }; + }, + setApplications(state: ICommonState, payload: IApplication[]): void { + state.applications = payload; + } + }, + actions: { + resetToken({ commit }: ActionContext) { + commit('resetToken'); + }, + setToken({ commit }: ActionContext, payload: IToken) { + commit('setToken', payload); + }, + setUser({ commit }: ActionContext, payload: IUser) { + commit('setUser', payload); + }, + setApplications({ commit }: ActionContext, payload: IApplication[]) { + commit('setApplications', payload); + } + }, + getters: { + authenticated(state: ICommonState): boolean { + return !!state.token.access; + }, + user(state: ICommonState): IUser { + return state.user; + }, + token(state: ICommonState): IToken { + return state.token; + }, + setting(state: ICommonState): ISetting { + return state.setting; + }, + applications(state: ICommonState): IApplication[] | undefined { + return state.applications; + } + } +}; diff --git a/src/store/index.ts b/src/store/index.ts index 3e6882c2..0357f3e3 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,111 +1,14 @@ import { createStore, ActionContext } from 'vuex'; import createPersistedState from 'vuex-persistedstate'; -import { ENDPOINT } from '@/constants'; -import { IApplication, IUser } from '@/operators'; -import { ISetting, IState, IToken } from './models'; +import { common } from './common'; +import { midjourney } from './midjourney'; +import { chat } from './chat'; const store = createStore({ - state(): IState { - return { - user: {}, - token: { - access: undefined, - refresh: undefined - }, - setting: { - endpoint: ENDPOINT, - stream: false - }, - applications: [], - apps: { - chat: {}, - midjourney: {} - } - }; - }, - mutations: { - setUser(state: IState, payload: IUser): void { - state.user = { - ...state.user, - ...payload - }; - }, - setToken(state: IState, payload: IToken): void { - state.token = { - ...state.token, - ...payload - }; - }, - resetToken(state: IState): void { - state.token = {}; - }, - setSetting(state: IState, payload: ISetting): void { - state.setting = { - ...state.setting, - ...payload - }; - }, - setApplications(state: IState, payload: IApplication[]): void { - state.applications = payload; - }, - setMidjourney(state: IState, payload: any): void { - state.apps.midjourney = { - ...state.apps.midjourney, - ...payload - }; - }, - setChat(state: IState, payload: any): void { - state.apps.chat = { - ...state.apps.chat, - ...payload - }; - } - }, - actions: { - resetToken({ commit }) { - commit('resetToken'); - }, - setUser({ commit }: ActionContext, payload: IUser) { - commit('setUser', payload); - }, - setToken({ commit }: ActionContext, payload: IToken) { - commit('setToken', payload); - }, - setSetting({ commit }: ActionContext, payload: ISetting) { - commit('setSetting', payload); - }, - setApplications({ commit }: ActionContext, payload: IApplication[]) { - commit('setApplications', payload); - }, - setMidjourney({ commit }: ActionContext, payload: any) { - commit('setMidjourney', payload); - }, - setChat({ commit }: ActionContext, payload: any) { - commit('setChat', payload); - } - }, - getters: { - authenticated(state): boolean { - return !!state.token.access; - }, - user(state): IUser { - return state.user; - }, - token(state): IToken { - return state.token; - }, - setting(state): ISetting | undefined { - return state.setting; - }, - applications(state): IApplication[] | undefined { - return state.applications; - }, - midjourney(state): any { - return state.apps.midjourney; - }, - chat(state): any { - return state.apps.chat; - } + modules: { + common, + midjourney, + chat }, plugins: [createPersistedState()] }); diff --git a/src/store/midjourney.ts b/src/store/midjourney.ts new file mode 100644 index 00000000..18c142d0 --- /dev/null +++ b/src/store/midjourney.ts @@ -0,0 +1,29 @@ +import { IApplication, IMidjourneyPreset } from '@/operators'; +import { Module } from 'vuex'; +import { IMidjourneyState } from './models'; + +export const midjourney: Module = { + namespaced: true, + state(): IMidjourneyState { + return { + applications: undefined, + preset: {} + }; + }, + mutations: { + setApplications(state: IMidjourneyState, payload: IApplication[]): void { + state.applications = payload; + }, + setPreset(state: IMidjourneyState, payload: IMidjourneyPreset): void { + state.preset = payload; + } + }, + actions: { + setPreset({ commit }: any, payload: IMidjourneyPreset) { + commit('setPreset', payload); + }, + setApplications({ commit }: any, payload: IApplication[]) { + commit('setApplications', payload); + } + } +}; diff --git a/src/store/models.ts b/src/store/models.ts index 233e42d7..d52f7c3c 100644 --- a/src/store/models.ts +++ b/src/store/models.ts @@ -1,4 +1,4 @@ -import { IApplication, IUser } from '@/operators'; +import { IApplication, IMidjourneyPreset, IUser } from '@/operators'; export interface IToken { access?: string; @@ -10,13 +10,24 @@ export interface ISetting { endpoint?: string; } -export interface IState { +export interface ICommonState { token: IToken; user: IUser; setting: ISetting; - applications: IApplication[]; - apps: { - chat: {}; - midjourney: {}; - }; + applications: IApplication[] | undefined; +} + +export interface IMidjourneyState { + applications: IApplication[] | undefined; + preset: IMidjourneyPreset; +} + +export interface IChatState { + applications: IApplication[] | undefined; +} + +export interface IRootState { + common: ICommonState; + midjourney: IMidjourneyState; + chat: IChatState; }