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:
@@ -1,18 +1,12 @@
|
||||
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("");
|
||||
@@ -22,6 +16,7 @@ export function useAppUpdate() {
|
||||
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> {
|
||||
@@ -64,7 +59,7 @@ export function useAppUpdate() {
|
||||
return Boolean(target?.url) && target?.version !== currentVersion.value;
|
||||
}
|
||||
|
||||
/** 下载最新版本,只落盘不激活,等用户确认后再应用 */
|
||||
/** 下载最新版本,只落盘不激活 */
|
||||
async function download(): Promise<BundleInfo | null> {
|
||||
const target = latest.value;
|
||||
if (!target?.url) {
|
||||
@@ -95,12 +90,17 @@ export function useAppUpdate() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 应用已下载的版本并重启,只有用户明确点了才执行 */
|
||||
/** 立即应用并重启 */
|
||||
async function apply(bundle: BundleInfo): Promise<void> {
|
||||
await CapacitorUpdater.set({ id: bundle.id });
|
||||
}
|
||||
|
||||
/** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */
|
||||
/** 排队到下次启动应用 */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user