feat(system): support app internal update apk

This commit is contained in:
Timi
2026-11-17 23:56:43 +08:00
parent 9f7f260f3e
commit cbfcd69408
9 changed files with 350 additions and 43 deletions
+25 -29
View File
@@ -12,13 +12,17 @@ export const APP_ID = "com.imyeyu.gao.customer";
/**
* 服务端下发的更新信息
*
* 在插件协议基础上多三个字段,插件不识别但会原样透传:
* 在插件协议基础上增加更新说明与外壳字段,插件会原样透传:
* note 是更新说明,shell 标记产物为安装包(外壳),mandatory 标记强制更新
*/
export type UpdateInfo = LatestVersion & {
note?: string;
shell?: boolean;
mandatory?: boolean;
shellUrl?: string;
shellVersion?: string;
shellChecksum?: string;
shellMandatory?: boolean;
};
// 模块级状态,页面与启动逻辑共享同一份
@@ -57,9 +61,18 @@ async function check(): Promise<UpdateInfo | null> {
checking.value = true;
error.value = "";
try {
const result = await CapacitorUpdater.getLatest();
latest.value = result;
return result;
const result: UpdateInfo = await CapacitorUpdater.getLatest();
// 新协议的独立外壳地址优先,统一转换为页面已有的外壳更新格式
const target: UpdateInfo = result.shellUrl ? {
...result,
url: result.shellUrl,
version: result.shellVersion ?? result.version,
checksum: result.shellChecksum,
mandatory: result.shellMandatory,
shell: true
} : result;
latest.value = target;
return target;
} catch (e) {
error.value = e instanceof Error ? e.message : "检查更新失败";
return null;
@@ -141,7 +154,11 @@ async function apply(bundle: BundleInfo): Promise<void> {
} catch (e) {
// 包目录已被清理或本身就是坏的(例如把安装包当热更包下下来),
// set 会报 id 不存在。不清理的话每次启动都卡在「已下载,重启生效」
await CapacitorUpdater.delete({ id: bundle.id }).catch(() => undefined);
try {
await CapacitorUpdater.delete({ id: bundle.id });
} catch {
// 包可能已经被插件清理
}
ready.value = null;
error.value = e instanceof Error ? e.message : "更新失败";
}
@@ -231,27 +248,6 @@ function promptUpdate(bundle: BundleInfo): void {
});
}
/**
* 弹窗引导下载安装包
*
* 外壳更新装不进热更,只能走浏览器下载后覆盖安装;强制更新不给「稍后」按钮
*/
function promptShellUpdate(info: UpdateInfo): void {
const dialog = DialogPlugin.confirm({
title: "需要安装新版本",
content: `v${info.version} 是应用外壳更新,需要下载安装包覆盖安装`,
cancelBtn: info.mandatory ? null : "稍后",
confirmBtn: "下载安装包",
onCancel() {
dialog.destroy();
},
onConfirm() {
dialog.destroy();
openShellDownload();
}
});
}
/**
* 启动时检查更新
*
@@ -272,11 +268,11 @@ export async function runAutoUpdate(): Promise<void> {
return;
}
const result = await check();
if (!result?.url) {
// 外壳更新不弹窗,入口统一在「版本更新」页
if (result?.shell) {
return;
}
if (result.shell) {
promptShellUpdate(result);
if (!result?.url) {
return;
}
if (result.version === currentVersion.value || !isAutoUpdateEnabled()) {