feat: 默认跟随系统主题和浏览器语言
This commit is contained in:
@@ -7,7 +7,12 @@ const media = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const systemDark = ref(media.matches);
|
||||
media.addEventListener('change', (e) => (systemDark.value = e.matches));
|
||||
|
||||
const mode = ref<ThemeMode>((localStorage.getItem(KEY) as ThemeMode) || 'light');
|
||||
function getInitialMode(): ThemeMode {
|
||||
const saved = localStorage.getItem(KEY) as ThemeMode | null;
|
||||
return saved === 'light' || saved === 'dark' || saved === 'auto' ? saved : 'auto';
|
||||
}
|
||||
|
||||
const mode = ref<ThemeMode>(getInitialMode());
|
||||
|
||||
const isDark = computed(() => mode.value === 'dark' || (mode.value === 'auto' && systemDark.value));
|
||||
|
||||
|
||||
@@ -11,10 +11,20 @@ export const SUPPORTED_LOCALES = [
|
||||
const STORAGE_KEY = 'artveris_locale';
|
||||
const DEFAULT_LOCALE = 'zh-CN';
|
||||
|
||||
function matchBrowserLocale(): string {
|
||||
const languages = navigator.languages?.length ? navigator.languages : [navigator.language];
|
||||
for (const language of languages) {
|
||||
const normalized = language.toLowerCase();
|
||||
const matched = SUPPORTED_LOCALES.find((locale) => normalized.startsWith(locale.value.slice(0, 2).toLowerCase()));
|
||||
if (matched) return matched.value;
|
||||
}
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
export function getInitialLocale(): string {
|
||||
const saved = localStorage.getItem(STORAGE_KEY);
|
||||
if (saved && SUPPORTED_LOCALES.some((l) => l.value === saved)) return saved;
|
||||
return DEFAULT_LOCALE;
|
||||
return matchBrowserLocale();
|
||||
}
|
||||
|
||||
export function setLocale(locale: string): void {
|
||||
@@ -32,3 +42,5 @@ export const i18n = createI18n({
|
||||
'en-US': enUS,
|
||||
},
|
||||
});
|
||||
|
||||
document.querySelector('html')?.setAttribute('lang', i18n.global.locale.value);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<template>
|
||||
<div class="login-wrap">
|
||||
<div class="login-lang">
|
||||
<div class="login-tools">
|
||||
<theme-switch />
|
||||
<lang-switch />
|
||||
</div>
|
||||
<div class="login-left">
|
||||
@@ -54,6 +55,7 @@ import { useAuthStore } from '@/stores/auth';
|
||||
import { message } from '@/utils/feedback';
|
||||
import { resolveAppIcon, setFavicon } from '@/utils/appIcon';
|
||||
import LangSwitch from '@/components/LangSwitch.vue';
|
||||
import ThemeSwitch from '@/components/ThemeSwitch.vue';
|
||||
|
||||
const { t } = useI18n();
|
||||
const router = useRouter();
|
||||
@@ -108,11 +110,14 @@ onMounted(async () => {
|
||||
display: flex;
|
||||
height: 100vh;
|
||||
}
|
||||
.login-lang {
|
||||
.login-tools {
|
||||
position: absolute;
|
||||
top: 20px;
|
||||
right: 24px;
|
||||
z-index: 2;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.login-left {
|
||||
flex: 1 1 55%;
|
||||
|
||||
@@ -29,4 +29,7 @@ export const i18n = createI18n({
|
||||
export function setLocale(l: string): void {
|
||||
localStorage.setItem(KEY, l);
|
||||
i18n.global.locale.value = l as 'zh-CN' | 'en-US';
|
||||
document.querySelector('html')?.setAttribute('lang', l);
|
||||
}
|
||||
|
||||
document.querySelector('html')?.setAttribute('lang', i18n.global.locale.value);
|
||||
|
||||
@@ -2,8 +2,26 @@
|
||||
--brand: #1f3864;
|
||||
--brand-2: #2e5496;
|
||||
--bg: #f3f5f9;
|
||||
--card-bg: #fff;
|
||||
--soft-bg: #f6f8fb;
|
||||
--border: #d0d7e2;
|
||||
--text: #1f2937;
|
||||
--muted: #6b7280;
|
||||
--shell-shadow: 0 0 24px rgba(0, 0, 0, 0.04);
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
--brand: #7dd3fc;
|
||||
--brand-2: #5eead4;
|
||||
--bg: #0f172a;
|
||||
--card-bg: #111827;
|
||||
--soft-bg: #1f2937;
|
||||
--border: #334155;
|
||||
--text: #e5e7eb;
|
||||
--muted: #94a3b8;
|
||||
--shell-shadow: 0 0 24px rgba(0, 0, 0, 0.24);
|
||||
}
|
||||
}
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
@@ -26,10 +44,10 @@ body,
|
||||
min-height: 100vh;
|
||||
background: var(--bg);
|
||||
position: relative;
|
||||
box-shadow: 0 0 24px rgba(0, 0, 0, 0.04);
|
||||
box-shadow: var(--shell-shadow);
|
||||
}
|
||||
.card {
|
||||
background: #fff;
|
||||
background: var(--card-bg);
|
||||
border-radius: 14px;
|
||||
margin: 12px;
|
||||
padding: 16px;
|
||||
|
||||
@@ -58,7 +58,7 @@ onMounted(async () => {
|
||||
height: 72px;
|
||||
border-radius: 18px;
|
||||
object-fit: cover;
|
||||
background: #f6f8fb;
|
||||
background: var(--soft-bg);
|
||||
}
|
||||
h1 {
|
||||
font-size: 22px;
|
||||
@@ -66,8 +66,8 @@ h1 {
|
||||
}
|
||||
.lang {
|
||||
margin-top: 16px;
|
||||
border: 1px solid #d0d7e2;
|
||||
background: #fff;
|
||||
border: 1px solid var(--border);
|
||||
background: var(--card-bg);
|
||||
border-radius: 999px;
|
||||
padding: 6px 18px;
|
||||
color: var(--brand);
|
||||
|
||||
Reference in New Issue
Block a user