diff --git a/ui/src/pages/Settings.vue b/ui/src/pages/Settings.vue index d3ec8dd..70edeeb 100644 --- a/ui/src/pages/Settings.vue +++ b/ui/src/pages/Settings.vue @@ -3,6 +3,7 @@ import { Sync } from '@vicons/ionicons5' import { form_item_label_style } from '@/styles/styles.js' import BaseMirrorOption from '@/components/BaseMirrorOption.vue' import { notify } from '@/utils/naiveDiscrete.js' +import { pywebviewAPI } from '@/utils/pywebview/api.js' const update_able = inject('update_able') const running = inject('running') @@ -15,12 +16,13 @@ const update_self_running = ref(false) async function update_self() { running.value = true update_self_running.value = true - const response = await pywebview.api.update_self( - new_version.value['assets'][0]['browser_download_url'] - ) - notify.error(response) - update_self_running.value = false - running.value = false + + try { + await pywebviewAPI.updateSelf(new_version.value.assets[0].browser_download_url) + } finally { + update_self_running.value = false + running.value = false + } } async function open_new_version_html() { @@ -30,16 +32,23 @@ async function open_new_version_html() { async function check_update() { running.value = true check_running.value = true - new_version.value = await pywebview.api.get_new_version() - if (new_version.value.tag_name > version.value) { - update_able.value = true - notify.info('有新版本可更新') - } else { - update_able.value = false - notify.info('当前已是最新版本') + + try { + const result = await pywebviewAPI.getNewVersion({ + showSuccess: false + }) + new_version.value = result + if (result.tag_name > version.value) { + update_able.value = true + notify.info('有新版本可更新') + } else { + update_able.value = false + notify.info('当前已是最新版本') + } + } finally { + check_running.value = false + running.value = false } - check_running.value = false - running.value = false } diff --git a/ui/src/utils/pywebview/api.js b/ui/src/utils/pywebview/api.js new file mode 100644 index 0000000..5a0195c --- /dev/null +++ b/ui/src/utils/pywebview/api.js @@ -0,0 +1,26 @@ +import { callPywebview } from '@/utils/pywebview/core.js' + +export const pywebviewAPI = { + // 获取最新版本信息 + getNewVersion: (config) => + callPywebview( + 'get_new_version', + {}, + { + successMessage: '已获取最新版本信息', + errorMessage: '获取最新版本信息失败', + ...config + } + ), + + // 自更新 + updateSelf: (url, config) => + callPywebview( + 'update_self', + { url }, + { + errorMessage: '更新失败', + ...config + } + ) +} diff --git a/ui/src/utils/pywebview/core.js b/ui/src/utils/pywebview/core.js new file mode 100644 index 0000000..2bc3ba0 --- /dev/null +++ b/ui/src/utils/pywebview/core.js @@ -0,0 +1,55 @@ +import { notify } from '../naiveDiscrete.js' + +const DEFAULT_CONFIG = { + successMessage: '操作成功', // 默认成功提示 + errorMessage: '操作失败', // 默认错误提示 + showSuccess: true, // 默认显示成功提示 + showError: true, // 默认显示错误提示 + timeout: 30000, // 默认超时时间(毫秒) + throwOnError: true // 默认抛出错误 +} + +export const callPywebview = async (method, params = {}, config = {}) => { + const mergedConfig = { ...DEFAULT_CONFIG, ...config } + + let timeoutId + if (mergedConfig.timeout > 0) { + timeoutId = setTimeout(() => { + throw new Error(`调用 ${method} 超时`) + }, mergedConfig.timeout) + } + + try { + if (!window.pywebview || !window.pywebview.api[method]) { + throw new Error(`pywebview.api.${method} 方法不存在`) + } + + const result = await window.pywebview.api[method](...Object.values(params)) + clearTimeout(timeoutId) + + if (mergedConfig.showSuccess) { + notify.success(mergedConfig.successMessage) + } + if (mergedConfig.onSuccess) { + mergedConfig.onSuccess(result) + } + + return result + } catch (error) { + if (timeoutId) { + clearTimeout(timeoutId) + } + + if (mergedConfig.showError) { + notify.error(mergedConfig.errorMessage) + } + if (mergedConfig.onError) { + mergedConfig.onError(error) + } + if (mergedConfig.throwOnError) { + throw error + } + + return null + } +}