Files
gao-customer-h5/src/pages/setting/AppSettings.vue
T
Timi 390833840a
Build / build (pull_request) Successful in 46s
Deploy / deploy (pull_request) Successful in 46s
Release / release (pull_request) Failing after 1s
style: drop v prefix from latest version
2026-11-07 18:01:10 +08:00

312 lines
7.1 KiB
Vue

<template>
<div class="tui-page-layout settings-page tui-navbar">
<t-navbar
fixed
:z-index="20"
title="应用设置"
left-arrow
@left-click="$router.back()"
/>
<main class="tui-page-content scroll column">
<t-cell-group title="显示" theme="card" v-entrance-transition>
<t-cell class="theme" title="显示模式">
<template #right-icon>
<segmented-slider
class="theme-slider"
:model-value="globalUIStore.themeMode"
:options="themeOptions"
@change="handleThemeChange"
/>
</template>
</t-cell>
</t-cell-group>
<t-cell-group title="声音" theme="card" v-entrance-transition>
<t-cell title="扫码成功">
<template #right-icon>
<t-switch :model-value="settingStore.sound.scanSuccess" @change="value => handleScanSuccessChange(value === true)" />
</template>
</t-cell>
<t-cell title="登记成功">
<template #right-icon>
<t-switch :model-value="settingStore.sound.registerSuccess" @change="value => handleRegisterSuccessChange(value === true)" />
</template>
</t-cell>
<t-cell title="音量">
<template #right-icon>
<div class="volume-field">
<t-slider
:model-value="settingStore.sound.volume"
:min="0"
:max="100"
:step="1"
@change="handleVolumeChange"
/>
<span v-text="`${settingStore.sound.volume}%`" />
</div>
</template>
</t-cell>
</t-cell-group>
<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" v-text="updateHint" />
<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>
下载中 {{ 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 type { ThemeMode } from "@/store/globalUI";
import { useGlobalUIStore } from "@/store/globalUI";
import SegmentedSlider from "@/components/SegmentedSlider.vue";
import { useSettingStore } from "@/store/settingStore";
import { setAutoUpdate, useAppUpdate } from "@/composables/useAppUpdate";
import { Message } from "@/utils/message";
defineOptions({
name: "AppSettings"
});
const globalUIStore = useGlobalUIStore();
const settingStore = useSettingStore();
// ---------- 主题 ----------
const themeOptions: Array<{ label: string; value: ThemeMode }> = [
{ label: "浅色", value: "light" },
{ label: "深色", value: "dark" },
{ label: "跟随系统", value: "system" }
];
function handleThemeChange(value: string): void {
globalUIStore.setThemeMode(value as ThemeMode);
}
// ---------- 声音 ----------
async function handleScanSuccessChange(value: boolean): Promise<void> {
settingStore.setSound({
scanSuccess: value
});
if (value) {
await playSettingSoundPreview("scanSuccess");
}
}
async function handleRegisterSuccessChange(value: boolean): Promise<void> {
settingStore.setSound({
registerSuccess: value
});
if (value) {
await playSettingSoundPreview("registerSuccess");
}
}
function handleVolumeChange(value: number | number[]): void {
settingStore.setSound({
volume: Array.isArray(value) ? value[0] : value
});
}
async function playSettingSoundPreview(type: "scanSuccess" | "registerSuccess"): Promise<void> {
await settingStore.unlockSoundPlayers();
if (type === "scanSuccess") {
await settingStore.playScanSuccessSound();
return;
}
await settingStore.playRegisterSuccessSound();
}
// ---------- 版本更新 ----------
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">
.settings-page {
.theme {
:deep(.t-cell__title) {
display: flex;
align-items: center;
}
.theme-slider {
width: 12rem;
flex: 0 0 12rem;
}
}
.volume-field {
gap: .5rem;
width: 16rem;
display: flex;
align-items: center;
:deep(.t-slider) {
flex: 1;
min-width: 0;
}
span {
color: #52607a;
font-size: .8125rem;
text-align: right;
}
}
.update-panel {
gap: .75rem;
display: flex;
flex-direction: column;
.progress {
margin: 0 1rem;
}
}
}
:global(html.theme-dark) {
.settings-page {
.volume-field span {
color: #94a3b8;
}
.update-hint {
color: #94a3b8;
}
}
}
</style>