重构项目,方便开发
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { ref } from 'vue'
|
||||
import { fetchVersion, startUpdate } from '@/shared/api/update'
|
||||
|
||||
export function useUpdateCheck() {
|
||||
const loading = ref(false)
|
||||
const version = ref('1.0.0')
|
||||
const hint = ref('')
|
||||
const downloadVisible = ref(false)
|
||||
const downloadLoading = ref(false)
|
||||
const fileUrl = ref('')
|
||||
|
||||
const check = async () => {
|
||||
loading.value = true
|
||||
hint.value = '正在检测更新...'
|
||||
downloadVisible.value = false
|
||||
|
||||
try {
|
||||
const result = await fetchVersion()
|
||||
version.value = (result.version || version.value).replace(/^v/i, '')
|
||||
|
||||
if (result.has_update && result.latest_version) {
|
||||
hint.value = `发现新版本 v${result.latest_version}${result.desc ? `:${result.desc}` : ''}`
|
||||
fileUrl.value = result.file_url || ''
|
||||
downloadVisible.value = true
|
||||
return
|
||||
}
|
||||
|
||||
hint.value = '已是最新版本'
|
||||
} catch (error) {
|
||||
hint.value = error instanceof Error ? error.message : '检测更新失败'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const runUpdate = async () => {
|
||||
if (!fileUrl.value) {
|
||||
hint.value = '暂无下载地址,请关注官方渠道。'
|
||||
return false
|
||||
}
|
||||
|
||||
downloadLoading.value = true
|
||||
hint.value = '正在下载并准备更新,程序将自动退出...'
|
||||
|
||||
try {
|
||||
const result = await startUpdate(fileUrl.value)
|
||||
if (result.success) {
|
||||
hint.value = '更新已启动,程序即将退出...'
|
||||
return true
|
||||
}
|
||||
|
||||
hint.value = result.error || '更新启动失败'
|
||||
return false
|
||||
} catch (error) {
|
||||
hint.value = error instanceof Error ? error.message : '请求更新失败,请重试'
|
||||
return false
|
||||
} finally {
|
||||
downloadLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
loading,
|
||||
version,
|
||||
hint,
|
||||
downloadVisible,
|
||||
downloadLoading,
|
||||
check,
|
||||
runUpdate,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user