feat(electron-shell): 1.11.0 新增设置中心与执行超时/内存分层,组件运行状态与运行历史峰值内存

This commit is contained in:
2026-11-13 13:53:20 +08:00
parent 43926ac456
commit 9bbb54a62b
29 changed files with 2200 additions and 572 deletions
@@ -0,0 +1,106 @@
import { computed, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDownloadJob } from '../useDownloadJob'
import { useShellCheck } from './useShellCheck'
import { getShellStatus, downloadShellUpdate, applyShellUpdate } from '../api'
import { store, log } from '../store'
// 客户端(壳子)自更新状态:模块级单例。
// 顶部更新条(App.vue)与设置页「本机组件」(MachineComponents.vue)共用同一份状态和同一个轮询器,
// 避免两处各起一套定时器 / 各弹一次提示。首次调用发生在 App.vue setup 内(onUnmounted 钩子挂在那里)。
let _singleton = null
export function useShellUpdate() {
if (_singleton) return _singleton
const shellJob = useDownloadJob({
fetchStatus: getShellStatus,
enabled: () => !!store.current, // 状态接口需登录;未登录/登出后暂停,避免持续打 401
pollMs: 15000,
downloadingPollMs: 400, // 下载中加快轮询,进度条更连续
isDownloading: (s) => s.dl?.status === 'downloading',
isCompleted: (s) => !!s.downloaded,
onCompleted: (s) => {
// 内网直链下载很快(80MB 秒下),延迟收窗让人看清 100% 完成态,再转顶部「重启更新」条
setTimeout(() => { shellJob.dialog.value = false }, 1500)
ElMessage.success(`新版本 v${s.new} 已下载完成,点击顶部「重启更新」完成升级`)
log('INFO', `客户端新版本 v${s.new} 已就绪,可重启更新`)
},
onFailed: (s) => ElMessage.warning('新版本下载失败:' + (s.dl?.error || '')),
})
const shellUpdate = shellJob.state
const updating = ref(false)
const downloading = shellJob.busy
const shellDismissed = ref(false) // 用户点了"暂不",本会话内隐藏"立即下载"提示
// 手动「检查更新」:秒级比对 + 轮询检查结果(见 composables/useShellCheck)
const { checking: shellChecking, check: onShellCheck } = useShellCheck(shellUpdate, handleCheckResult)
const updDl = computed(() => shellUpdate.value.dl || {})
const updDlg = shellJob.dialog // 下载进度弹窗(点击「立即下载」后弹出)
const shellDlText = computed(() => (shellUpdate.value.downloaded ? '已下载完成' : '正在准备下载…'))
const updBarVisible = computed(() =>
shellUpdate.value.downloaded ||
(updDl.value.status === 'failed' && !updDlg.value) ||
(shellUpdate.value.available && !shellDismissed.value)
)
async function doShellUpdate() {
updating.value = true
try {
await applyShellUpdate() // 后端运行静默安装并退出进程,升级后自动重启
} catch (e) {
updating.value = false
ElMessage.error('更新失败:' + (e.message || e))
}
}
async function onShellDownload() {
if (downloading.value) return
shellDismissed.value = false
const v = shellUpdate.value.latest || shellUpdate.value.new || ''
try {
// 点击即进入下载中(不等轮询),后端后台流式下载,进度经轮询展示
await shellJob.beginDownload({ version: v, start: downloadShellUpdate })
shellJob.start()
} catch (e) {
ElMessage.error('开始下载失败:' + (e.message || e))
}
}
function handleCheckResult(s, opts = {}) {
// 批量检查(「全部检查」):结果直接呈现在本机组件面板里,不弹确认框打断
if (opts.silent) return
if (!s.current) {
ElMessage.info('当前为源码开发模式,不参与客户端自更新')
return
}
if (s.downloaded) {
ElMessage.info(`新版本 v${s.new} 已就绪,点击顶部「重启更新」完成升级`)
return
}
if (updDl.value.status === 'downloading') return // 正在下载,进度条展示中
if (s.error) {
ElMessage.warning('检查更新失败:' + s.error)
log('WARN', `检查客户端更新失败:${s.error}`)
return
}
if (s.available) {
ElMessageBox.confirm(
`发现新版本 v${s.latest},是否立即下载?${s.notes ? '\n\n更新说明:' + s.notes : ''}`,
'发现新版本',
{ confirmButtonText: '立即下载', cancelButtonText: '稍后再说', type: 'info' }
).then(() => onShellDownload()).catch(() => {})
} else {
ElMessage.info(`已是最新版本 v${s.current}`)
}
}
_singleton = {
shellJob, shellUpdate, shellChecking, onShellCheck,
updating, downloading, shellDismissed,
updDl, updDlg, shellDlText, updBarVisible,
doShellUpdate, onShellDownload, handleCheckResult,
}
return _singleton
}