重构 api 调用操作

This commit is contained in:
Rewrite0
2023-05-31 22:49:09 +08:00
parent 73ecf25d9e
commit b994db575b
5 changed files with 120 additions and 122 deletions

View File

@@ -1,73 +1,29 @@
import type { Config } from '#/config';
import { initConfig, type Config } from '#/config';
export const useConfigStore = defineStore('config', () => {
const message = useMessage();
const config = ref<Config>({
program: {
rss_time: 0,
rename_time: 0,
webui_port: 0,
},
downloader: {
type: 'qbittorrent',
host: '',
username: '',
password: '',
path: '',
ssl: false,
},
rss_parser: {
enable: true,
type: 'mikan',
token: '',
custom_url: '',
filter: [],
language: 'zh',
parser_type: 'parser',
},
bangumi_manage: {
enable: true,
eps_complete: true,
rename_method: 'normal',
group_tag: true,
remove_bad_torrent: true,
},
log: {
debug_enable: false,
},
proxy: {
enable: false,
type: 'http',
host: '',
port: 0,
username: '',
password: '',
},
notification: {
enable: false,
type: 'telegram',
token: '',
chat_id: '',
const config = ref<Config>(initConfig);
const { execute: getConfig, onResult: onGetConfigRusult } = useApi(
apiConfig.getConfig
);
onGetConfigRusult((res) => {
config.value = res;
});
const { execute: set } = useApi(apiConfig.updateConfig, {
failRule: (res) => !res,
message: {
success: 'Apply Success!',
fail: 'Apply Failed!',
},
});
const getConfig = async () => {
const res = await apiConfig.getConfig();
config.value = res;
};
const setConfig = () => set(config.value);
const setConfig = async () => {
const status = await apiConfig.updateConfig(config.value);
if (status) {
message.success('Apply Success!');
} else {
message.error('Apply Failed!');
}
};
const getSettingGroup = <Tkey extends keyof Config>(key: Tkey) => {
function getSettingGroup<Tkey extends keyof Config>(key: Tkey) {
return computed<Config[Tkey]>(() => config.value[key]);
};
}
return {
config,

View File

@@ -1,33 +1,36 @@
export const useLogStore = defineStore('log', () => {
const log = ref('');
const timer = ref<NodeJS.Timer | null>(null);
const { auth } = useAuth();
const message = useMessage();
const get = async () => {
function get() {
const { execute, onResult } = useApi(apiLog.getLog);
onResult((value) => {
log.value = value;
});
if (auth.value !== '') {
log.value = await apiLog.getLog();
execute();
}
};
}
const onUpdate = () => {
get();
timer.value = setInterval(() => get(), 3000);
};
const { execute: reset, onResult: onClearLogResult } = useApi(
apiLog.clearLog
);
const offUpdate = () => {
clearInterval(Number(timer.value));
timer.value = null;
};
const reset = async () => {
const res = await apiLog.clearLog();
onClearLogResult((res) => {
if (res) {
log.value = '';
}
};
});
const copy = () => {
const { pause: offUpdate, resume: onUpdate } = useIntervalFn(get, 3000, {
immediate: false,
immediateCallback: true,
});
function copy() {
const { copy: copyLog, isSupported } = useClipboard({ source: log });
if (isSupported) {
copyLog();
@@ -35,7 +38,7 @@ export const useLogStore = defineStore('log', () => {
} else {
message.error('Your browser does not support Clipboard API!');
}
};
}
return {
log,

View File

@@ -1,52 +1,42 @@
export const useProgramStore = defineStore('program', () => {
const message = useMessage();
export const useProgramStore = defineStore('program', function () {
const { auth } = useAuth();
const running = ref(false);
const timer = ref<NodeJS.Timer | null>(null);
const getStatus = async () => {
function getStatus() {
const { execute, onResult } = useApi(apiProgram.status);
onResult((res) => {
running.value = res;
});
if (auth.value !== '') {
running.value = await apiProgram.status();
}
};
const onUpdate = () => {
getStatus();
timer.value = setInterval(() => getStatus(), 3000);
};
const offUpdate = () => {
clearInterval(Number(timer.value));
timer.value = null;
};
function handleMessage(handle: string, success: boolean) {
if (success) {
message.success(`${handle} Success!`);
} else {
message.error(`${handle} Failed!`);
execute();
}
}
const start = async () => {
const res = await apiProgram.start();
handleMessage('Start', res);
};
const { pause: offUpdate, resume: onUpdate } = useIntervalFn(
getStatus,
3000,
{
immediate: false,
immediateCallback: true,
}
);
const pause = async () => {
const res = await apiProgram.stop();
handleMessage('Pause', res);
};
function opts(handle: string) {
return {
failRule: (res: boolean) => !res,
message: {
success: `${handle} Success!`,
fail: `${handle} Failed!`,
},
};
}
const shutdown = async () => {
const res = await apiProgram.shutdown();
handleMessage('Shutdown', res);
};
const restart = async () => {
const res = await apiProgram.restart();
handleMessage('Restart', res);
};
const { execute: start } = useApi(apiProgram.start, opts('Start'));
const { execute: pause } = useApi(apiProgram.stop, opts('Pause'));
const { execute: shutdown } = useApi(apiProgram.shutdown, opts('Shutdown'));
const { execute: restart } = useApi(apiProgram.restart, opts('Restart'));
return {
running,

View File

@@ -49,6 +49,55 @@ export interface Config {
};
}
export const initConfig: Config = {
program: {
rss_time: 0,
rename_time: 0,
webui_port: 0,
},
downloader: {
type: 'qbittorrent',
host: '',
username: '',
password: '',
path: '',
ssl: false,
},
rss_parser: {
enable: true,
type: 'mikan',
token: '',
custom_url: '',
filter: [],
language: 'zh',
parser_type: 'parser',
},
bangumi_manage: {
enable: true,
eps_complete: true,
rename_method: 'normal',
group_tag: true,
remove_bad_torrent: true,
},
log: {
debug_enable: false,
},
proxy: {
enable: false,
type: 'http',
host: '',
port: 0,
username: '',
password: '',
},
notification: {
enable: false,
type: 'telegram',
token: '',
chat_id: '',
},
};
type getItem<T extends keyof Config> = Pick<Config, T>[T];
export type Program = getItem<'program'>;

View File

@@ -5,6 +5,6 @@ export type LoginError = 'Password error' | 'User not found';
export type ApiErrorMessage = AuthError | LoginError;
export interface ApiError {
status: 401 | 404 | 422;
status: 401 | 404 | 422 | 500;
detail: ApiErrorMessage;
}