From b994db575b6dc48f89d405771ab1b42011fa9892 Mon Sep 17 00:00:00 2001 From: Rewrite0 <49576640+Rewrite0@users.noreply.github.com> Date: Wed, 31 May 2023 22:49:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20api=20=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/config.ts | 82 ++++++++++---------------------------------- src/store/log.ts | 39 +++++++++++---------- src/store/program.ts | 70 ++++++++++++++++--------------------- types/config.ts | 49 ++++++++++++++++++++++++++ types/error.ts | 2 +- 5 files changed, 120 insertions(+), 122 deletions(-) diff --git a/src/store/config.ts b/src/store/config.ts index e60ffbe3..17174efc 100644 --- a/src/store/config.ts +++ b/src/store/config.ts @@ -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({ - 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(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 = (key: Tkey) => { + function getSettingGroup(key: Tkey) { return computed(() => config.value[key]); - }; + } return { config, diff --git a/src/store/log.ts b/src/store/log.ts index f759f224..b1fe8566 100644 --- a/src/store/log.ts +++ b/src/store/log.ts @@ -1,33 +1,36 @@ export const useLogStore = defineStore('log', () => { const log = ref(''); - const timer = ref(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, diff --git a/src/store/program.ts b/src/store/program.ts index 0a03b6c9..d0859618 100644 --- a/src/store/program.ts +++ b/src/store/program.ts @@ -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(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, diff --git a/types/config.ts b/types/config.ts index 229cbbcd..3982628f 100644 --- a/types/config.ts +++ b/types/config.ts @@ -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 = Pick[T]; export type Program = getItem<'program'>; diff --git a/types/error.ts b/types/error.ts index 405071df..6fd6324c 100644 --- a/types/error.ts +++ b/types/error.ts @@ -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; }