Files
gao-customer-h5/src/pages/setting/AppUpdate.vue
T

304 lines
7.1 KiB
Vue

<template>
<div class="tui-page-layout tui-navbar app-update">
<t-navbar
fixed
:z-index="20"
title="版本更新"
left-arrow
@left-click="goBack"
/>
<main class="tui-page-content scroll column">
<t-cell-group v-if="updateSupported" theme="card" v-entrance-transition>
<t-cell title="外壳版本" :note="nativeVersion || '—'" />
<t-cell title="系统版本" :note="displayVersion" />
<t-cell v-if="latestVersionText" title="最新版本" :note="latestVersionText" />
<t-cell title="自动更新">
<template #right-icon>
<t-switch :model-value="autoUpdate" @change="handleAutoUpdateChange" />
</template>
</t-cell>
</t-cell-group>
<t-cell-group v-else theme="card" v-entrance-transition>
<t-cell title="系统版本" :note="displayVersion" />
</t-cell-group>
<div v-if="updateSupported" class="update-panel" v-entrance-transition>
<center-tips v-if="updateHint"><span v-text="updateHint" /></center-tips>
<t-button
v-if="updateStage === 'check'"
class="cell-button"
theme="primary"
size="large"
:loading="checking"
@click="handleCheck"
>
检查更新
</t-button>
<t-button
v-else-if="updateStage === 'download'"
class="cell-button"
theme="primary"
size="large"
@click="handleDownload"
>
下载更新
</t-button>
<template v-else-if="updateStage === 'downloading'">
<t-progress class="progress" stroke-width=".5rem" :percentage="percent" />
<t-button
class="cell-button"
theme="primary"
size="large"
disabled
content="正在下载.."
/>
</template>
<t-button
v-else-if="updateStage === 'apply'"
class="cell-button"
theme="primary"
size="large"
@click="handleApply"
>
现在更新
</t-button>
</div>
<section v-if="noteList.length" class="versions">
<h2 class="title" v-text="noteTitle" />
<timeline>
<timeline-item
class="version"
v-for="item in noteList"
:key="item.version"
v-entrance-transition
>
<template #label>
<div class="label">
<t-tag
size="small"
theme="primary"
variant="light-outline"
:content="item.version"
/>
<time-label class="time" :timestamp="item.publishedAt" />
</div>
</template>
<markdown-view class="content bg-white" :content="item.note" />
</timeline-item>
<timeline-item v-entrance-transition>
<template #label>
<div class="label">
<t-tag
size="small"
theme="primary"
variant="light-outline"
content="..."
/>
</div>
</template>
</timeline-item>
</timeline>
</section>
</main>
</div>
</template>
<script setup lang="ts">
import { version } from "../../../package.json";
import { Capacitor } from "@capacitor/core";
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 type { AppUpdateNote } from "@/types/AppUpdate";
import { Message } from "@/utils/message";
defineOptions({
name: "AppUpdate"
});
const router = useRouter();
const pane = usePane();
function goBack(): void {
if (pane) {
pane.close();
return;
}
router.back();
}
// ---------- 版本更新 ----------
const {
supported: updateSupported,
currentVersion,
nativeVersion,
latest,
checking,
downloading,
percent,
ready,
error: updateError,
autoUpdate,
loadCurrent,
check,
hasUpdate,
download,
apply,
restorePending
} = useAppUpdate();
/** 系统版本:原生端优先取热更包版本,未热更过或 web 端回落到构建版本 */
const displayVersion = computed<string>(
() => (currentVersion.value && currentVersion.value !== "builtin" ? currentVersion.value : version)
);
/** 有更新时展示目标版本号 */
const latestVersionText = computed<string>(() => (hasUpdate() ? `v${latest.value?.version ?? ""}` : ""));
/** 当前所处的更新阶段 */
const updateStage = computed<"check" | "download" | "downloading" | "apply">(() => {
if (downloading.value) {
return "downloading";
}
if (ready.value) {
return "apply";
}
if (latest.value && hasUpdate()) {
return "download";
}
return "check";
});
/** 阶段说明文案 */
const updateHint = computed<string>(() => {
if (!updateSupported) {
return "仅在 App 内可用";
}
if (ready.value) {
return `v${ready.value.version} 已下载,点击下方按钮重启生效`;
}
if (latest.value && hasUpdate()) {
return `发现新版本 v${latest.value.version}`;
}
if (latest.value && !checking.value) {
return "当前已是最新版本";
}
return "";
});
// ---------- 更新日志 ----------
/** web 端只展示最近几个版本 */
const WEB_NOTE_LIMIT = 3;
const noteList = ref<AppUpdateNote[]>([]);
const noteTitle = computed<string>(() => (updateSupported ? "更新内容" : "最近更新"));
/** 查询当前版本到通道最新版本之间的变更说明 */
async function loadNotes(): Promise<void> {
try {
const result = await AppUpdateAPI.notes({
appId: APP_ID,
// web 端没有热更包版本,从内置包开始算,后端会返回通道的完整历史
versionName: updateSupported ? currentVersion.value || "builtin" : "builtin",
platform: Capacitor.getPlatform(),
channel: "stable"
});
const list = result?.notes ?? [];
noteList.value = updateSupported ? list : list.slice(0, WEB_NOTE_LIMIT);
} catch {
// 日志不是关键路径,失败时安静降级,不弹错误
noteList.value = [];
}
}
// ---------- 生命周期 ----------
onMounted(async () => {
await loadCurrent();
await restorePending();
await loadNotes();
});
// ---------- 交互 ----------
function handleAutoUpdateChange(value: boolean | string | number): void {
setAutoUpdate(value === true);
}
async function handleCheck(): Promise<void> {
const result = await check();
if (!result) {
Message.error(updateError.value || "检查更新失败");
return;
}
if (!hasUpdate()) {
Message.info("已是最新版本");
}
// 检查后目标版本可能变化,重查一次日志
await loadNotes();
}
async function handleDownload(): Promise<void> {
const bundle = await download();
if (!bundle) {
Message.error(updateError.value || "下载失败");
return;
}
Message.success(`v${bundle.version} 已下载`);
}
async function handleApply(): Promise<void> {
const bundle = ready.value;
if (!bundle) {
return;
}
await apply(bundle);
}
</script>
<style scoped lang="less">
.app-update {
.progress {
margin: 0 1rem;
}
.versions {
gap: .5rem;
display: flex;
padding: .25rem 1rem 1rem;
flex-direction: column;
.title {
color: var(--td-text-color-secondary);
margin: .5rem .25rem 0;
font-size: .8125rem;
font-weight: 500;
}
.version {
.label {
gap: .5rem;
display: flex;
align-items: center;
.time {
font-size: .875rem;
}
}
.content {
padding: .5rem;
font-size: .875rem;
margin-top: .5rem;
border-radius: 1rem;
}
}
}
}
</style>