feat(system): support app internal update apk
This commit is contained in:
@@ -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()) {
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -33,6 +33,11 @@
|
||||
>
|
||||
检查更新
|
||||
</t-button>
|
||||
<template v-else-if="updateStage === 'shell-error'">
|
||||
<center-tips><span v-text="shellError" /></center-tips>
|
||||
<t-button class="cell-button" theme="primary" size="large" @click="handleShellDownload">重试</t-button>
|
||||
<t-button class="cell-button" size="large" @click="openShellDownload">用浏览器下载</t-button>
|
||||
</template>
|
||||
<t-button
|
||||
v-else-if="updateStage === 'shell'"
|
||||
class="cell-button"
|
||||
@@ -52,7 +57,7 @@
|
||||
下载更新
|
||||
</t-button>
|
||||
<template v-else-if="updateStage === 'downloading'">
|
||||
<t-progress class="progress" stroke-width=".5rem" :percentage="percent" />
|
||||
<t-progress class="progress" stroke-width=".5rem" :percentage="shellDownloading ? shellPercent : percent" />
|
||||
<t-button
|
||||
class="cell-button"
|
||||
theme="primary"
|
||||
@@ -118,6 +123,7 @@ import { Timeline, TimelineItem, usePane } from "timi-tdesign-mobile";
|
||||
import { MarkdownView, TimeLabel } from "timi-web";
|
||||
import AppUpdateAPI from "@/api/AppUpdateAPI";
|
||||
import { APP_ID, setAutoUpdate, useAppUpdate } from "@/composables/useAppUpdate";
|
||||
import { useShellUpdate } from "@/composables/useShellUpdate";
|
||||
import type { AppUpdateNote } from "@/types/AppUpdate";
|
||||
import { Message } from "@/utils/message";
|
||||
|
||||
@@ -159,6 +165,8 @@ const {
|
||||
restorePending
|
||||
} = useAppUpdate();
|
||||
|
||||
const { downloading: shellDownloading, percent: shellPercent, error: shellError, start: startShellUpdate } = useShellUpdate(latest);
|
||||
|
||||
/** 系统版本:原生端优先取热更包版本,未热更过或 web 端回落到构建版本 */
|
||||
const displayVersion = computed<string>(
|
||||
() => (currentVersion.value && currentVersion.value !== "builtin" ? currentVersion.value : version)
|
||||
@@ -168,13 +176,13 @@ const displayVersion = computed<string>(
|
||||
const latestVersionText = computed<string>(() => (hasUpdate() ? `v${latest.value?.version ?? ""}` : ""));
|
||||
|
||||
/** 当前所处的更新阶段 */
|
||||
const updateStage = computed<"check" | "shell" | "download" | "downloading" | "apply">(() => {
|
||||
if (isShellUpdate()) {
|
||||
return "shell";
|
||||
}
|
||||
if (downloading.value) {
|
||||
const updateStage = computed<"check" | "shell" | "shell-error" | "download" | "downloading" | "apply">(() => {
|
||||
if (downloading.value || shellDownloading.value) {
|
||||
return "downloading";
|
||||
}
|
||||
if (isShellUpdate()) {
|
||||
return shellError.value ? "shell-error" : "shell";
|
||||
}
|
||||
if (ready.value) {
|
||||
return "apply";
|
||||
}
|
||||
@@ -259,9 +267,9 @@ async function handleCheck(): Promise<void> {
|
||||
await loadNotes();
|
||||
}
|
||||
|
||||
/** 外壳更新:交给系统浏览器下载安装包 */
|
||||
function handleShellDownload(): void {
|
||||
openShellDownload();
|
||||
/** 外壳更新:优先使用原生安装能力 */
|
||||
async function handleShellDownload(): Promise<void> {
|
||||
await startShellUpdate();
|
||||
}
|
||||
|
||||
async function handleDownload(): Promise<void> {
|
||||
|
||||
@@ -23,11 +23,21 @@ import { usePendingNoticeStore } from "@/store/pendingNoticeStore";
|
||||
|
||||
const pendingNoticeStore = usePendingNoticeStore();
|
||||
const router = useRouter();
|
||||
const { supported, latest, autoUpdate, hasUpdate } = useAppUpdate();
|
||||
const { supported, latest, autoUpdate, hasUpdate, isShellUpdate } = useAppUpdate();
|
||||
const noticeList = computed(() => {
|
||||
const notices = [...pendingNoticeStore.pendingNoticeList];
|
||||
if (supported && !autoUpdate.value && hasUpdate() && latest.value) {
|
||||
notices.push({ content: `有可用新版本 v${latest.value.version} 更新`, name: "AppUpdate", query: {} });
|
||||
if (supported && hasUpdate() && latest.value) {
|
||||
// 外壳更新不会自动下载,自动更新开着也必须提示;热更开着开关就不提示了
|
||||
const shell = isShellUpdate();
|
||||
if (shell || !autoUpdate.value) {
|
||||
notices.push({
|
||||
content: shell
|
||||
? `需要安装新版本 v${latest.value.version},点击前往更新`
|
||||
: `有可用新版本 v${latest.value.version} 更新`,
|
||||
name: "AppUpdate",
|
||||
query: {}
|
||||
});
|
||||
}
|
||||
}
|
||||
return notices;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user