Files
37duAutomation/desktop/electron-shell/frontend/src/composables/useShellUpdate.js
T
puddy 9db8c74b7f feat(shell): 插件密码加密、失败熔断与客户端自动更新(v1.11.3)
- 插件配置中 format=password 字段改为 AES-256-GCM 密文落盘(本机密钥,历史明文自动迁移)
- 同一插件连续失败 3 次自动禁用(仅统计进程启动后失败),升级自动解除
- 检测到新版本后台下载,空闲窗口提前提示并自动重启升级,设置页可配安全窗口
2026-11-26 09:28:18 +08:00

127 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
// 下载完成提示去重:后端自动下载时前端没走 beginDownload(inFlight 恒 false),
// useDownloadJob 的跃迁检测会漏报;用 onTick 兜底,同一版本只提示一次。
let _notifiedVersion = ''
function notifyDownloaded(s) {
if (!s.downloaded || !s.new || _notifiedVersion === s.new) return
_notifiedVersion = s.new
if (s.auto_update) {
ElMessage.success(`新版本 v${s.new} 已下载完成,将在空闲时自动重启升级(可到设置关闭自动更新)`)
log('INFO', `客户端新版本 v${s.new} 已就绪,等待空闲窗口自动升级`)
} else {
ElMessage.success(`新版本 v${s.new} 已下载完成,点击顶部「重启更新」完成升级`)
log('INFO', `客户端新版本 v${s.new} 已就绪,可重启更新`)
}
}
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)
notifyDownloaded(s)
},
onTick: (s) => { if (s.downloaded) notifyDownloaded(s) }, // 自动下载兜底
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) {
if (s.auto_update) {
ElMessage.info(`发现新版本 v${s.latest},已开始自动下载,空闲时将自动升级`)
} else {
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
}