add: app update page
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
<template>
|
||||
<div class="tui-page-layout tui-navbar">
|
||||
<t-navbar fixed :z-index="20" title="版本更新" left-arrow @left-click="$router.back()" />
|
||||
<main class="tui-page-content scroll column">
|
||||
<center-tips v-if="!updateSupported" content="仅在 App 内可用" />
|
||||
<t-cell-group v-if="updateSupported" title="版本更新" theme="card" v-entrance-transition>
|
||||
<t-cell title="外壳版本" :note="nativeVersion || '—'" />
|
||||
<t-cell title="系统版本" :note="currentVersion || '—'" />
|
||||
<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>
|
||||
<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 theme="primary" size="large" block disabled>
|
||||
<span v-text="`下载中 ${percent}%`" />
|
||||
</t-button>
|
||||
</template>
|
||||
<t-button
|
||||
v-else-if="updateStage === 'apply'"
|
||||
class="cell-button"
|
||||
theme="primary"
|
||||
size="large"
|
||||
@click="handleApply"
|
||||
>
|
||||
现在更新
|
||||
</t-button>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { setAutoUpdate, useAppUpdate } from "@/composables/useAppUpdate";
|
||||
import { Message } from "@/utils/message";
|
||||
|
||||
defineOptions({
|
||||
name: "AppUpdate"
|
||||
});
|
||||
|
||||
// ---------- 版本更新 ----------
|
||||
|
||||
const {
|
||||
supported: updateSupported,
|
||||
currentVersion,
|
||||
nativeVersion,
|
||||
latest,
|
||||
checking,
|
||||
downloading,
|
||||
percent,
|
||||
ready,
|
||||
error: updateError,
|
||||
autoUpdate,
|
||||
loadCurrent,
|
||||
check,
|
||||
hasUpdate,
|
||||
download,
|
||||
apply,
|
||||
restorePending
|
||||
} = useAppUpdate();
|
||||
|
||||
/** 有更新时展示目标版本号 */
|
||||
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 "";
|
||||
});
|
||||
|
||||
onMounted(async () => {
|
||||
await loadCurrent();
|
||||
await restorePending();
|
||||
});
|
||||
|
||||
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("已是最新版本");
|
||||
}
|
||||
}
|
||||
|
||||
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">
|
||||
.update-panel {
|
||||
gap: .75rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
.progress {
|
||||
margin: 0 1rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user