Merge pull request 'v1.0.3' (#4) from dev into master

Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
2026-11-07 10:01:58 +00:00
4 changed files with 284 additions and 213 deletions
+91 -51
View File
@@ -1,30 +1,25 @@
import { Capacitor } from "@capacitor/core";
import { CapacitorUpdater, type BundleInfo, type LatestVersion } from "@capgo/capacitor-updater";
import { DialogPlugin } from "tdesign-mobile-vue";
import { ref } from "vue";
/** 自动检查更新的存储键 */
/** 自动更新的存储键 */
export const AUTO_UPDATE_KEY = "app.autoUpdate";
/**
* 应用热更新
*
* 只在原生环境可用,Web 端调用会得到 supported = false
*
* 下载完成后调用 next 排队,下次切后台或重启自动生效,不打断当前操作
*/
export function useAppUpdate() {
const supported = Capacitor.isNativePlatform();
const currentVersion = ref("");
const nativeVersion = ref("");
const latest = ref<LatestVersion | null>(null);
const checking = ref(false);
const downloading = ref(false);
const percent = ref(0);
const ready = ref<BundleInfo | null>(null);
const error = ref("");
// 模块级状态,页面与启动逻辑共享同一份
const supported = Capacitor.isNativePlatform();
const currentVersion = ref("");
const nativeVersion = ref("");
const latest = ref<LatestVersion | null>(null);
const checking = ref(false);
const downloading = ref(false);
const percent = ref(0);
const ready = ref<BundleInfo | null>(null);
const error = ref("");
const autoUpdate = ref(localStorage.getItem(AUTO_UPDATE_KEY) === "1");
/** 读取当前运行的 Web 包版本与原生壳版本 */
async function loadCurrent(): Promise<void> {
/** 读取当前运行的 Web 包版本与原生壳版本 */
async function loadCurrent(): Promise<void> {
if (!supported) {
return;
}
@@ -36,10 +31,10 @@ export function useAppUpdate() {
currentVersion.value = "";
nativeVersion.value = "";
}
}
}
/** 查询服务端最新版本 */
async function check(): Promise<LatestVersion | null> {
/** 查询服务端最新版本 */
async function check(): Promise<LatestVersion | null> {
if (!supported) {
error.value = "仅在 App 内可用";
return null;
@@ -56,16 +51,16 @@ export function useAppUpdate() {
} finally {
checking.value = false;
}
}
}
/** 是否发现新版本 */
function hasUpdate(): boolean {
/** 是否发现新版本 */
function hasUpdate(): boolean {
const target = latest.value;
return Boolean(target?.url) && target?.version !== currentVersion.value;
}
}
/** 下载最新版本,只落盘不激活,等用户确认后再应用 */
async function download(): Promise<BundleInfo | null> {
/** 下载最新版本,只落盘不激活 */
async function download(): Promise<BundleInfo | null> {
const target = latest.value;
if (!target?.url) {
error.value = "没有可下载的版本";
@@ -93,15 +88,20 @@ export function useAppUpdate() {
downloading.value = false;
await listener.remove();
}
}
}
/** 应用已下载的版本并重启,只有用户明确点了才执行 */
async function apply(bundle: BundleInfo): Promise<void> {
/** 立即应用并重启 */
async function apply(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.set({ id: bundle.id });
}
}
/** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */
async function restorePending(): Promise<void> {
/** 排队到下次启动应用 */
async function defer(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.next({ id: bundle.id });
}
/** 查找已下载但尚未应用的版本 */
async function restorePending(): Promise<void> {
if (!supported) {
return;
}
@@ -111,14 +111,18 @@ export function useAppUpdate() {
const pending = list.bundles
.filter(bundle => bundle.id !== currentId)
.sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0];
if (pending) {
ready.value = pending;
}
ready.value = pending ?? null;
} catch {
ready.value = null;
}
}
}
/**
* 应用热更新
*
* 只在原生环境可用,Web 端 supported 恒为 false
*/
export function useAppUpdate() {
return {
supported,
currentVersion,
@@ -129,38 +133,74 @@ export function useAppUpdate() {
percent,
ready,
error,
autoUpdate,
loadCurrent,
check,
hasUpdate,
download,
apply,
defer,
restorePending
};
}
/** 是否开启自动检查更新 */
/** 是否开启自动更新 */
export function isAutoUpdateEnabled(): boolean {
return localStorage.getItem(AUTO_UPDATE_KEY) === "1";
return autoUpdate.value;
}
/** 切换自动更新,同时写入本地存储 */
export function setAutoUpdate(enabled: boolean): void {
autoUpdate.value = enabled;
localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0");
}
/** 弹窗让用户决定立即更新还是下次启动 */
function promptUpdate(bundle: BundleInfo): void {
const dialog = DialogPlugin.confirm({
title: "发现新版本",
content: `v${bundle.version} 已下载完成,重启后生效`,
cancelBtn: "稍后更新",
confirmBtn: "现在更新",
onCancel() {
dialog.destroy();
// 稍后更新即排队,下次启动自动应用
void defer(bundle);
},
async onConfirm() {
dialog.destroy();
await apply(bundle);
}
});
}
/**
* 启动时静默检查更新
* 启动时检查更新
*
* 只下载不应用,是否切换版本始终由用户决定
* 检查每次启动都做;是否静默下载取决于自动更新开关
* 下载完成后弹窗让用户选择立即重启还是稍后
*/
export async function runAutoUpdate(): Promise<void> {
if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) {
if (!supported) {
return;
}
const update = useAppUpdate();
await update.loadCurrent();
await update.restorePending();
if (update.ready.value) {
await loadCurrent();
await restorePending();
if (ready.value) {
if (isAutoUpdateEnabled()) {
promptUpdate(ready.value);
}
return;
}
const result = await update.check();
if (!result?.url || result.version === update.currentVersion.value) {
const result = await check();
if (!result?.url || result.version === currentVersion.value) {
return;
}
await update.download();
if (!isAutoUpdateEnabled()) {
return;
}
await download();
if (ready.value) {
promptUpdate(ready.value);
}
}
+8 -12
View File
@@ -1,5 +1,5 @@
<template>
<div class="tui-page-layout tui-navbar">
<div class="tui-page-layout settings-page tui-navbar">
<t-navbar
fixed
:z-index="20"
@@ -8,7 +8,6 @@
@left-click="$router.back()"
/>
<main class="tui-page-content scroll column">
<div class="settings-page">
<t-cell-group title="显示" theme="card" v-entrance-transition>
<t-cell class="theme" title="显示模式">
<template #right-icon>
@@ -50,7 +49,8 @@
<t-cell-group v-if="updateSupported" title="版本更新" theme="card" v-entrance-transition>
<t-cell title="外壳版本" :note="nativeVersion || '—'" />
<t-cell title="系统版本" :note="currentVersion || '—'" />
<t-cell title="自动检查更新">
<t-cell v-if="latestVersionText" title="最新版本" :note="latestVersionText" />
<t-cell title="自动更新">
<template #right-icon>
<t-switch :model-value="autoUpdate" @change="handleAutoUpdateChange" />
</template>
@@ -92,7 +92,6 @@
现在更新
</t-button>
</div>
</div>
</main>
</div>
</template>
@@ -102,7 +101,7 @@ import type { ThemeMode } from "@/store/globalUI";
import { useGlobalUIStore } from "@/store/globalUI";
import SegmentedSlider from "@/components/SegmentedSlider.vue";
import { useSettingStore } from "@/store/settingStore";
import { AUTO_UPDATE_KEY, isAutoUpdateEnabled, useAppUpdate } from "@/composables/useAppUpdate";
import { setAutoUpdate, useAppUpdate } from "@/composables/useAppUpdate";
import { Message } from "@/utils/message";
defineOptions({
@@ -172,6 +171,7 @@ const {
percent,
ready,
error: updateError,
autoUpdate,
loadCurrent,
check,
hasUpdate,
@@ -180,7 +180,8 @@ const {
restorePending
} = useAppUpdate();
const autoUpdate = ref(isAutoUpdateEnabled());
/** 有更新时展示目标版本号 */
const latestVersionText = computed<string>(() => (hasUpdate() ? `v${latest.value?.version ?? ""}` : ""));
/** 当前所处的更新阶段 */
const updateStage = computed<"check" | "download" | "downloading" | "apply">(() => {
@@ -219,9 +220,7 @@ onMounted(async () => {
});
function handleAutoUpdateChange(value: boolean | string | number): void {
const enabled = value === true;
autoUpdate.value = enabled;
localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0");
setAutoUpdate(value === true);
}
async function handleCheck(): Promise<void> {
@@ -255,9 +254,6 @@ async function handleApply(): Promise<void> {
<style scoped lang="less">
.settings-page {
padding: 0 0 1rem 0;
display: flex;
flex-direction: column;
.theme {
+3
View File
@@ -7,6 +7,7 @@
title="客户"
>
<template #right>
<app-update-button />
<navbar-notify-button />
</template>
</t-navbar>
@@ -31,6 +32,7 @@
<store-overview ref="storeOverviewRef" compact />
</template>
<template #right-actions>
<app-update-button />
<navbar-notify-button />
</template>
<template #right>
@@ -53,6 +55,7 @@ import { useLayout } from "timi-tdesign-mobile";
import PendingNoticeBar from "./component/PendingNoticeBar.vue";
import CustomerEntryPanel from "./component/CustomerEntryPanel.vue";
import NavbarNotifyButton from "./component/NavbarNotifyButton.vue";
import AppUpdateButton from "./component/AppUpdateButton.vue";
import StoreOverview from "./component/StoreOverview.vue";
import TabSplitView from "./component/TabSplitView.vue";
import { permissionCodes } from "@/utils/permission";
@@ -0,0 +1,32 @@
<template>
<t-button
v-if="visible"
aria-label="有新版本"
@click="router.push('/management/settings')"
theme="default"
shape="square"
variant="text"
size="small"
>
<t-badge dot :offset="[-4, 4]">
<t-icon name="download" size="1.25rem" />
</t-badge>
</t-button>
</template>
<script setup lang="ts">
import { useAppUpdate } from "@/composables/useAppUpdate";
defineOptions({
name: "AppUpdateButton"
});
const router = useRouter();
const { supported, latest, autoUpdate, hasUpdate } = useAppUpdate();
// 只在有更新且未开启自动更新时出现,开启自动更新后由启动弹窗负责提醒
const visible = computed(() => {
void latest.value;
return supported && hasUpdate() && !autoUpdate.value;
});
</script>