ota
This commit is contained in:
+150
@@ -0,0 +1,150 @@
|
||||
// 前端 OTA 封装
|
||||
// - Tauri 客户端: 直接用 tauri-plugin-hotswap-api (checkUpdate / applyUpdate / ...)
|
||||
// - Web 浏览器: 只走我们自己的 /api/ota/manifest, 提示用户手动刷新 (Vite 资源指纹会失效)
|
||||
//
|
||||
// 设计原则:
|
||||
// - Web 端只能"查 + 提示", 不能真下载应用, 避免误导
|
||||
// - Tauri 端走 hotswap 插件的官方 API, 签名校验/回滚/资源替换全交给插件
|
||||
import { getApiBase, getToken } from './client.js'
|
||||
import { APP_VERSION, BUILD_ID } from '../version.js'
|
||||
|
||||
// 判断是否在 Tauri WebView 内 (与 src/debug/pagespy.js 同样的判定)
|
||||
const inTauri = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window
|
||||
|
||||
// 拼接绝对 URL: 在 Tauri (VITE_API_BASE 有值) 时直连, 否则同源
|
||||
function resolveUrl(p) {
|
||||
const base = getApiBase()
|
||||
return base ? base + p : p
|
||||
}
|
||||
|
||||
// 当前版本信息 (前端展示用)
|
||||
export function getCurrentVersionInfo() {
|
||||
return { version: APP_VERSION, build: BUILD_ID }
|
||||
}
|
||||
|
||||
// 启动时: 确认当前资源可用 (Tauri only)
|
||||
// 失败/异常都不抛, 仅 console.warn
|
||||
export function initOta() {
|
||||
if (!inTauri) return
|
||||
// 动态 import: web 模式根本不打这个 chunk
|
||||
import('tauri-plugin-hotswap-api')
|
||||
.then((mod) => mod.notifyReady())
|
||||
.then(() => console.log('[ota] notifyReady ok'))
|
||||
.catch((e) => console.warn('[ota] notifyReady failed:', e))
|
||||
}
|
||||
|
||||
// 检查更新
|
||||
// 返回统一结构, 调用方不关心是 Tauri 还是 web:
|
||||
// {
|
||||
// available: boolean,
|
||||
// reason: 'no_package' | 'same_version' | 'ota_disabled' | 'unsupported' | 'ok',
|
||||
// info: { version, sequence, notes, mandatory, bundle_size }
|
||||
// }
|
||||
export async function checkOtaUpdate() {
|
||||
if (!inTauri) {
|
||||
return checkOtaUpdateWeb()
|
||||
}
|
||||
return checkOtaUpdateTauri()
|
||||
}
|
||||
|
||||
async function checkOtaUpdateTauri() {
|
||||
// 走 hotswap 插件 (内部会带 sequence / platform / arch 等参数)
|
||||
const { checkUpdate } = await import('tauri-plugin-hotswap-api')
|
||||
const r = await checkUpdate()
|
||||
if (!r.available) {
|
||||
return { available: false, reason: 'no_package', info: r }
|
||||
}
|
||||
return {
|
||||
available: true,
|
||||
reason: 'ok',
|
||||
info: {
|
||||
version: r.version,
|
||||
sequence: r.sequence,
|
||||
notes: r.notes,
|
||||
mandatory: r.mandatory,
|
||||
bundle_size: r.bundle_size,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function checkOtaUpdateWeb() {
|
||||
// Web 端不能下载, 只能拿到版本号/构建号做比对, 提示用户刷新
|
||||
try {
|
||||
const res = await fetch(resolveUrl('/api/ota/manifest-web'), { method: 'GET' })
|
||||
if (!res.ok) {
|
||||
return { available: false, reason: 'unsupported', info: null }
|
||||
}
|
||||
const data = await res.json()
|
||||
if (!data.available) return { available: false, reason: 'no_package', info: data }
|
||||
if (data.build_id && data.build_id === BUILD_ID) {
|
||||
return { available: false, reason: 'same_version', info: data }
|
||||
}
|
||||
return {
|
||||
available: true,
|
||||
reason: 'ok',
|
||||
info: {
|
||||
version: data.version,
|
||||
notes: data.note || '',
|
||||
// web 端没有强制/mandatory 等字段, 仅展示
|
||||
mandatory: false,
|
||||
bundle_size: data.size || 0,
|
||||
},
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[ota] web check failed:', e)
|
||||
return { available: false, reason: 'unsupported', info: null }
|
||||
}
|
||||
}
|
||||
|
||||
// 下载并应用更新 (Tauri only). 调用方负责 UI 提示和错误处理
|
||||
// onProgress(received, total) 进度回调
|
||||
// 成功后必须 reload, 新资源才会生效
|
||||
export async function applyOtaUpdate(onProgress) {
|
||||
if (!inTauri) {
|
||||
throw new Error('OTA 升级仅在 Tauri 客户端可用, Web 端请直接刷新页面')
|
||||
}
|
||||
const mod = await import('tauri-plugin-hotswap-api')
|
||||
|
||||
// 注册一次性进度监听 (applyUpdate 期间触发)
|
||||
let unlisten = null
|
||||
if (onProgress) {
|
||||
try {
|
||||
unlisten = await mod.onDownloadProgress((p) => onProgress(p.downloaded, p.total))
|
||||
} catch (e) {
|
||||
console.warn('[ota] onDownloadProgress 监听失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const newVersion = await mod.applyUpdate()
|
||||
return newVersion
|
||||
} finally {
|
||||
if (unlisten) {
|
||||
try { unlisten() } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 回滚到内置 dist (Tauri only, 一般用于 OTA 出错时手动恢复)
|
||||
export async function rollbackOta() {
|
||||
if (!inTauri) throw new Error('仅 Tauri 客户端可用')
|
||||
const mod = await import('tauri-plugin-hotswap-api')
|
||||
return mod.rollback()
|
||||
}
|
||||
|
||||
// 取当前 OTA 状态 (Tauri only)
|
||||
export async function getOtaVersionInfo() {
|
||||
if (!inTauri) return null
|
||||
try {
|
||||
const mod = await import('tauri-plugin-hotswap-api')
|
||||
return await mod.getVersionInfo()
|
||||
} catch (e) {
|
||||
console.warn('[ota] getVersionInfo failed:', e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// reload 当前页 (新资源生效)
|
||||
export function reloadApp() {
|
||||
window.location.reload()
|
||||
}
|
||||
Reference in New Issue
Block a user