139 lines
4.0 KiB
JavaScript
139 lines
4.0 KiB
JavaScript
/**
|
||
* 更新管理 Mixin
|
||
*/
|
||
export default {
|
||
computed: {
|
||
updateDialogVisible() {
|
||
return this.$store ? this.$store.state.update.updateDialogVisible : false;
|
||
},
|
||
updateInfo() {
|
||
return this.$store ? this.$store.state.update.updateInfo : null;
|
||
},
|
||
updateProgress() {
|
||
return this.$store ? this.$store.state.update.updateProgress : 0;
|
||
},
|
||
isDownloading() {
|
||
return this.$store ? this.$store.state.update.isDownloading : false;
|
||
},
|
||
downloadState() {
|
||
return this.$store ? this.$store.state.update.downloadState : {
|
||
progress: 0,
|
||
downloadedBytes: 0,
|
||
totalBytes: 0
|
||
};
|
||
}
|
||
},
|
||
methods: {
|
||
// 接收主进程推送的更新信息(不做处理,直接更新 store)
|
||
onUpdateAvailable(updateInfo) {
|
||
if (!updateInfo) {
|
||
return;
|
||
}
|
||
|
||
if (this.$store) {
|
||
this.$store.dispatch('update/setUpdateInfo', updateInfo);
|
||
this.$store.dispatch('update/showUpdateDialog');
|
||
}
|
||
|
||
if (this.addLog) {
|
||
this.addLog('info', `发现新版本: ${updateInfo.version || '未知'}`);
|
||
}
|
||
},
|
||
|
||
// 接收主进程推送的更新进度(不做处理,直接更新 store)
|
||
onUpdateProgress(progressData) {
|
||
if (this.$store && progressData) {
|
||
this.$store.dispatch('update/setDownloadState', {
|
||
progress: progressData.progress || 0,
|
||
downloadedBytes: progressData.downloadedBytes || 0,
|
||
totalBytes: progressData.totalBytes || 0
|
||
});
|
||
this.$store.dispatch('update/setUpdateProgress', progressData.progress || 0);
|
||
this.$store.dispatch('update/setDownloading', true);
|
||
}
|
||
},
|
||
|
||
// 接收主进程推送的下载完成通知(不做处理,直接更新 store)
|
||
onUpdateDownloaded(data) {
|
||
if (this.$store) {
|
||
this.$store.dispatch('update/setDownloading', false);
|
||
this.$store.dispatch('update/setUpdateProgress', 100);
|
||
}
|
||
if (this.addLog) {
|
||
this.addLog('success', '更新包下载完成');
|
||
}
|
||
if (this.showNotification) {
|
||
this.showNotification('更新下载完成', '更新包已下载完成,是否立即安装?');
|
||
}
|
||
},
|
||
|
||
// 接收主进程推送的更新错误(不做处理,直接显示)
|
||
onUpdateError(errorData) {
|
||
if (this.$store) {
|
||
this.$store.dispatch('update/setDownloading', false);
|
||
}
|
||
const errorMsg = errorData?.error || '更新失败';
|
||
if (this.addLog) {
|
||
this.addLog('error', `更新错误: ${errorMsg}`);
|
||
}
|
||
if (this.showNotification) {
|
||
this.showNotification('更新失败', errorMsg);
|
||
}
|
||
},
|
||
|
||
closeUpdateDialog() {
|
||
if (this.$store) {
|
||
this.$store.dispatch('update/hideUpdateDialog');
|
||
}
|
||
},
|
||
|
||
// 下载更新(调用主进程接口,不做业务逻辑处理)
|
||
async startDownload() {
|
||
const updateInfoData = this.updateInfo;
|
||
if (!updateInfoData || !updateInfoData.downloadUrl) {
|
||
if (this.addLog) {
|
||
this.addLog('error', '更新信息不存在');
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (!window.electronAPI) {
|
||
if (this.addLog) {
|
||
this.addLog('error', 'Electron API不可用');
|
||
}
|
||
return;
|
||
}
|
||
|
||
try {
|
||
await window.electronAPI.invoke('update:download', updateInfoData.downloadUrl);
|
||
} catch (error) {
|
||
if (this.addLog) {
|
||
this.addLog('error', `下载更新失败: ${error.message}`);
|
||
}
|
||
}
|
||
},
|
||
|
||
// 安装更新(调用主进程接口,不做业务逻辑处理)
|
||
async installUpdate() {
|
||
if (!window.electronAPI) {
|
||
if (this.addLog) {
|
||
this.addLog('error', 'Electron API不可用');
|
||
}
|
||
return;
|
||
}
|
||
|
||
try {
|
||
await window.electronAPI.invoke('update:install');
|
||
setTimeout(() => {
|
||
this.closeUpdateDialog();
|
||
}, 1000);
|
||
} catch (error) {
|
||
if (this.addLog) {
|
||
this.addLog('error', `安装更新失败: ${error.message}`);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|