Compare commits

..

1 commit
main ... dev

Author SHA1 Message Date
66db711f76 代码重构:初步封装前端pywebview.api 2025-07-14 00:58:58 +08:00
3 changed files with 105 additions and 15 deletions

View file

@ -3,6 +3,7 @@ import { Sync } from '@vicons/ionicons5'
import { form_item_label_style } from '@/styles/styles.js' import { form_item_label_style } from '@/styles/styles.js'
import BaseMirrorOption from '@/components/BaseMirrorOption.vue' import BaseMirrorOption from '@/components/BaseMirrorOption.vue'
import { notify } from '@/utils/naiveDiscrete.js' import { notify } from '@/utils/naiveDiscrete.js'
import { pywebviewAPI } from '@/utils/pywebview/api.js'
const update_able = inject('update_able') const update_able = inject('update_able')
const running = inject('running') const running = inject('running')
@ -15,12 +16,13 @@ const update_self_running = ref(false)
async function update_self() { async function update_self() {
running.value = true running.value = true
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'] try {
) await pywebviewAPI.updateSelf(new_version.value.assets[0].browser_download_url)
notify.error(response) } finally {
update_self_running.value = false update_self_running.value = false
running.value = false running.value = false
}
} }
async function open_new_version_html() { async function open_new_version_html() {
@ -30,16 +32,23 @@ async function open_new_version_html() {
async function check_update() { async function check_update() {
running.value = true running.value = true
check_running.value = true check_running.value = true
new_version.value = await pywebview.api.get_new_version()
if (new_version.value.tag_name > version.value) { try {
update_able.value = true const result = await pywebviewAPI.getNewVersion({
notify.info('有新版本可更新') showSuccess: false
} else { })
update_able.value = false new_version.value = result
notify.info('当前已是最新版本') 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
} }
</script> </script>

View file

@ -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
}
)
}

View file

@ -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
}
}