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()) {
+132
View File
@@ -0,0 +1,132 @@
import { Capacitor, registerPlugin, type PluginListenerHandle } from "@capacitor/core";
import { DialogPlugin } from "tdesign-mobile-vue";
import type { Ref } from "vue";
import type { UpdateInfo } from "@/composables/useAppUpdate";
interface ShellUpdaterPlugin {
canInstall(): Promise<{ allowed: boolean }>;
openInstallSettings(): Promise<void>;
downloadAndInstall(options: { url: string; sha256: string; fileName: string }): Promise<void>;
addListener(eventName: "downloadProgress", listener: (progress: { percent: number; received: number; total: number }) => void): Promise<PluginListenerHandle>;
addListener(eventName: "resume", listener: () => void): Promise<PluginListenerHandle>;
}
const ShellUpdater = registerPlugin<ShellUpdaterPlugin>("ShellUpdater");
/** 管理更新页的外壳安装和授权流程 */
export function useShellUpdate(latest: Ref<UpdateInfo | null>) {
const downloading = ref(false);
const percent = ref(0);
const error = ref("");
let waitingForSettings = false;
let resumeListener: PluginListenerHandle | undefined;
let disposed = false;
/** 下载并交给系统安装器 */
async function install(): Promise<void> {
const target = latest.value;
if (!target?.url || !target.checksum) {
error.value = "缺少安装包地址或校验值";
return;
}
downloading.value = true;
percent.value = 0;
error.value = "";
let listener: PluginListenerHandle | undefined;
try {
listener = await ShellUpdater.addListener("downloadProgress", progress => {
percent.value = progress.percent;
});
await ShellUpdater.downloadAndInstall({
url: target.url,
sha256: target.checksum,
fileName: `app-${target.version}.apk`
});
} catch (e) {
error.value = e instanceof Error ? e.message : "安装包下载失败";
} finally {
if (listener) {
await listener.remove();
}
downloading.value = false;
}
}
/** 从授权页返回后只检查一次 */
async function handleResume(): Promise<void> {
if (!waitingForSettings) {
return;
}
waitingForSettings = false;
try {
const result = await ShellUpdater.canInstall();
if (result.allowed) {
await install();
} else {
error.value = "未获得安装权限,可重试或使用浏览器下载";
}
} catch (e) {
error.value = e instanceof Error ? e.message : "检查安装权限失败";
}
}
/** 检查权限,旧壳交给浏览器下载 */
async function start(): Promise<void> {
error.value = "";
if (!Capacitor.isPluginAvailable("ShellUpdater")) {
if (latest.value?.url) {
window.location.assign(latest.value.url);
}
return;
}
try {
const result = await ShellUpdater.canInstall();
if (result.allowed) {
await install();
return;
}
const dialog = DialogPlugin.confirm({
title: "需要安装权限",
content: "请允许此应用安装未知应用,返回后将继续下载并安装。",
cancelBtn: "稍后",
confirmBtn: "去设置",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
waitingForSettings = true;
try {
await ShellUpdater.openInstallSettings();
} catch (e) {
waitingForSettings = false;
error.value = e instanceof Error ? e.message : "无法打开安装权限设置";
}
}
});
} catch (e) {
error.value = e instanceof Error ? e.message : "检查安装权限失败";
}
}
onMounted(async () => {
if (!Capacitor.isPluginAvailable("ShellUpdater")) {
return;
}
const listener = await ShellUpdater.addListener("resume", () => { void handleResume(); });
if (disposed) {
await listener.remove();
} else {
resumeListener = listener;
}
});
onUnmounted(async () => {
disposed = true;
waitingForSettings = false;
if (resumeListener) {
await resumeListener.remove();
}
});
return { downloading, percent, error, start };
}