feat(update): support download apk package
Build / build (pull_request) Successful in 48s
Deploy / deploy (pull_request) Successful in 48s
Release / release (pull_request) Successful in 49s

This commit is contained in:
Timi
2026-11-17 11:54:00 +08:00
parent f2b36013fe
commit 8c346a81c3
5 changed files with 124 additions and 10 deletions
+64 -5
View File
@@ -12,10 +12,13 @@ export const APP_ID = "com.imyeyu.gao.customer";
/**
* 服务端下发的更新信息
*
* 在插件协议基础上多一个 note:更新说明由后端附加,插件不识别但会原样透传
* 在插件协议基础上多三个字段,插件不识别但会原样透传:
* note 是更新说明,shell 标记产物为安装包(外壳),mandatory 标记强制更新
*/
export type UpdateInfo = LatestVersion & {
note?: string;
shell?: boolean;
mandatory?: boolean;
};
// 模块级状态,页面与启动逻辑共享同一份
@@ -65,10 +68,34 @@ async function check(): Promise<UpdateInfo | null> {
}
}
/** 是否发现新版本 */
/** 是否发现新版本(外壳更新不看版本号,只认服务端下发的地址) */
function hasUpdate(): boolean {
const target = latest.value;
return Boolean(target?.url) && target?.version !== currentVersion.value;
if (!target?.url) {
return false;
}
return Boolean(target.shell) || target.version !== currentVersion.value;
}
/** 是否为外壳更新,只能下载安装包覆盖安装,热更装不进去 */
function isShellUpdate(): boolean {
const target = latest.value;
return Boolean(target?.shell && target?.url);
}
/**
* 把安装包交给系统浏览器下载
*
* Capacitor 对非同源地址走 Bridge.launchIntent,用 ACTION_VIEW 外抛给浏览器,
* 安装动作由浏览器和系统安装器发起,应用自身不需要安装权限,所以老壳也能用
*/
function openShellDownload(): void {
const target = latest.value;
if (!target?.url) {
error.value = "没有可下载的版本";
return;
}
window.location.assign(target.url);
}
/** 下载最新版本,只落盘不激活 */
@@ -78,6 +105,10 @@ async function download(): Promise<BundleInfo | null> {
error.value = "没有可下载的版本";
return null;
}
if (target.shell) {
error.value = "外壳更新需要下载安装包";
return null;
}
downloading.value = true;
percent.value = 0;
error.value = "";
@@ -149,6 +180,8 @@ export function useAppUpdate() {
loadCurrent,
check,
hasUpdate,
isShellUpdate,
openShellDownload,
download,
apply,
defer,
@@ -186,11 +219,33 @@ 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();
}
});
}
/**
* 启动时检查更新
*
* 检查每次启动都做;是否静默下载取决于自动更新开关
* 下载完成后弹窗让用户选择立即重启还是稍后
* 外壳更新优先处理:热更包是 JS,装不进旧壳
*/
export async function runAutoUpdate(): Promise<void> {
if (!supported) {
@@ -205,10 +260,14 @@ export async function runAutoUpdate(): Promise<void> {
return;
}
const result = await check();
if (!result?.url || result.version === currentVersion.value) {
if (!result?.url) {
return;
}
if (!isAutoUpdateEnabled()) {
if (result.shell) {
promptShellUpdate(result);
return;
}
if (result.version === currentVersion.value || !isAutoUpdateEnabled()) {
return;
}
await download();