migrate vuex modules

This commit is contained in:
AceDataCloud committed 2023-12-28 03:11:04 +08:00
1 parent 9c5ce9acf3
commit f39e155301
17 files changed
+177 -133

No files matched your search

+2 -2
View File
@@ -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,
+1 -1
View File
@@ -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 });
}
}
+4 -4
View File
@@ -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
});
}
+2 -2
View File
@@ -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,
+1 -1
View File
@@ -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() {
@@ -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,
@@ -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,
+2 -2
View File
@@ -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: {
+3 -3
View File
@@ -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);
+1 -1
View File
@@ -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,
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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;
+22
View File
@@ -0,0 +1,22 @@
import { IApplication } from '@/operators';
import { Module } from 'vuex';
import { IChatState } from './models';
export const chat: Module<IChatState, any> = {
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);
}
}
};
+79
View File
@@ -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<ICommonState, IRootState> = {
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<ICommonState, IRootState>) {
commit('resetToken');
},
setToken({ commit }: ActionContext<ICommonState, IRootState>, payload: IToken) {
commit('setToken', payload);
},
setUser({ commit }: ActionContext<ICommonState, IRootState>, payload: IUser) {
commit('setUser', payload);
},
setApplications({ commit }: ActionContext<ICommonState, IRootState>, 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;
}
}
};
+7 -104
View File
@@ -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<IState, IState>, payload: IUser) {
commit('setUser', payload);
},
setToken({ commit }: ActionContext<IState, IState>, payload: IToken) {
commit('setToken', payload);
},
setSetting({ commit }: ActionContext<IState, IState>, payload: ISetting) {
commit('setSetting', payload);
},
setApplications({ commit }: ActionContext<IState, IState>, payload: IApplication[]) {
commit('setApplications', payload);
},
setMidjourney({ commit }: ActionContext<IState, IState>, payload: any) {
commit('setMidjourney', payload);
},
setChat({ commit }: ActionContext<IState, IState>, 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()]
});
+29
View File
@@ -0,0 +1,29 @@
import { IApplication, IMidjourneyPreset } from '@/operators';
import { Module } from 'vuex';
import { IMidjourneyState } from './models';
export const midjourney: Module<IMidjourneyState, any> = {
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);
}
}
};
+18 -7
View File
@@ -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;
}