feat: add manual and optional auto update in settings
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
import { CapacitorUpdater, type BundleInfo, type LatestVersion } from "@capgo/capacitor-updater";
|
||||
import { ref } from "vue";
|
||||
|
||||
/** 自动检查更新的存储键 */
|
||||
export const AUTO_UPDATE_KEY = "app.autoUpdate";
|
||||
|
||||
/**
|
||||
* 应用热更新
|
||||
*
|
||||
* 只在原生环境可用,Web 端调用会得到 supported = false
|
||||
*
|
||||
* 下载完成后调用 next 排队,下次切后台或重启自动生效,不打断当前操作
|
||||
*/
|
||||
export function useAppUpdate() {
|
||||
const supported = Capacitor.isNativePlatform();
|
||||
const currentVersion = ref("");
|
||||
const latest = ref<LatestVersion | null>(null);
|
||||
const checking = ref(false);
|
||||
const downloading = ref(false);
|
||||
const percent = ref(0);
|
||||
const ready = ref<BundleInfo | null>(null);
|
||||
const error = ref("");
|
||||
|
||||
/** 读取当前运行的 Web 包版本,内置包为 builtin */
|
||||
async function loadCurrent(): Promise<void> {
|
||||
if (!supported) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await CapacitorUpdater.current();
|
||||
currentVersion.value = result.bundle?.version ?? "builtin";
|
||||
} catch {
|
||||
currentVersion.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
/** 查询服务端最新版本 */
|
||||
async function check(): Promise<LatestVersion | null> {
|
||||
if (!supported) {
|
||||
error.value = "仅在 App 内可用";
|
||||
return null;
|
||||
}
|
||||
checking.value = true;
|
||||
error.value = "";
|
||||
try {
|
||||
const result = await CapacitorUpdater.getLatest();
|
||||
latest.value = result;
|
||||
return result;
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : "检查更新失败";
|
||||
return null;
|
||||
} finally {
|
||||
checking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 是否发现新版本 */
|
||||
function hasUpdate(): boolean {
|
||||
const target = latest.value;
|
||||
return Boolean(target?.url) && target?.version !== currentVersion.value;
|
||||
}
|
||||
|
||||
/** 下载最新版本并排队等待生效 */
|
||||
async function download(): Promise<BundleInfo | null> {
|
||||
const target = latest.value;
|
||||
if (!target?.url) {
|
||||
error.value = "没有可下载的版本";
|
||||
return null;
|
||||
}
|
||||
downloading.value = true;
|
||||
percent.value = 0;
|
||||
error.value = "";
|
||||
const listener = await CapacitorUpdater.addListener("download", info => {
|
||||
percent.value = info.percent;
|
||||
});
|
||||
try {
|
||||
const bundle = await CapacitorUpdater.download({
|
||||
url: target.url,
|
||||
version: target.version,
|
||||
checksum: target.checksum,
|
||||
sessionKey: target.sessionKey
|
||||
});
|
||||
await CapacitorUpdater.next({ id: bundle.id });
|
||||
ready.value = bundle;
|
||||
return bundle;
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : "下载失败";
|
||||
return null;
|
||||
} finally {
|
||||
downloading.value = false;
|
||||
await listener.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/** 立即重启应用使更新生效 */
|
||||
async function reload(): Promise<void> {
|
||||
await CapacitorUpdater.reload();
|
||||
}
|
||||
|
||||
/** 检查并下载,供启动时自动更新调用 */
|
||||
async function checkAndDownload(): Promise<boolean> {
|
||||
await loadCurrent();
|
||||
const result = await check();
|
||||
if (!result?.url || result.version === currentVersion.value) {
|
||||
return false;
|
||||
}
|
||||
return (await download()) !== null;
|
||||
}
|
||||
|
||||
return {
|
||||
supported,
|
||||
currentVersion,
|
||||
latest,
|
||||
checking,
|
||||
downloading,
|
||||
percent,
|
||||
ready,
|
||||
error,
|
||||
loadCurrent,
|
||||
check,
|
||||
hasUpdate,
|
||||
download,
|
||||
reload,
|
||||
checkAndDownload
|
||||
};
|
||||
}
|
||||
|
||||
/** 是否开启自动检查更新 */
|
||||
export function isAutoUpdateEnabled(): boolean {
|
||||
return localStorage.getItem(AUTO_UPDATE_KEY) === "1";
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动时静默检查更新
|
||||
*
|
||||
* 只下载排队,不重启,下次打开 App 自动生效
|
||||
*/
|
||||
export async function runAutoUpdate(): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) {
|
||||
return;
|
||||
}
|
||||
await useAppUpdate().checkAndDownload();
|
||||
}
|
||||
Reference in New Issue
Block a user