447 lines
13 KiB
Vue
Executable File
447 lines
13 KiB
Vue
Executable File
<template>
|
||
<div class="general-settings">
|
||
<div class="section-header">
|
||
<h2>{{ $t('general.title') }}</h2>
|
||
<p class="section-description">{{ $t('general.description') }}</p>
|
||
</div>
|
||
|
||
<div class="settings-group">
|
||
<!-- 语言选择 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('language.language') }}</label>
|
||
<p class="desc">{{ $t('language.languageDescription') }}</p>
|
||
</div>
|
||
<div class="setting-control">
|
||
<t-select
|
||
v-model="localLanguage"
|
||
:placeholder="$t('language.selectLanguage')"
|
||
@change="handleLanguageChange"
|
||
style="width: 280px;"
|
||
>
|
||
<t-option value="zh-CN" :label="$t('language.zhCN')">{{ $t('language.zhCN') }}</t-option>
|
||
<t-option value="en-US" :label="$t('language.enUS')">{{ $t('language.enUS') }}</t-option>
|
||
<t-option value="ru-RU" :label="$t('language.ruRU')">{{ $t('language.ruRU') }}</t-option>
|
||
<t-option value="ko-KR" :label="$t('language.koKR')">{{ $t('language.koKR') }}</t-option>
|
||
</t-select>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 主题设置 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('theme.theme') }}</label>
|
||
<p class="desc">{{ $t('theme.themeDescription') }}</p>
|
||
</div>
|
||
<div class="setting-control">
|
||
<t-select
|
||
v-model="localTheme"
|
||
style="width: 280px;"
|
||
:placeholder="$t('theme.selectTheme')"
|
||
@change="handleThemeChange"
|
||
>
|
||
<t-option value="light" :label="$t('theme.light')">{{ $t('theme.light') }}</t-option>
|
||
<t-option value="dark" :label="$t('theme.dark')">{{ $t('theme.dark') }}</t-option>
|
||
<t-option value="system" :label="$t('theme.system')">{{ $t('theme.system') }}</t-option>
|
||
</t-select>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 界面字体 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('font.uiFont') }}</label>
|
||
<p class="desc">{{ $t('font.uiFontDescription') }}</p>
|
||
</div>
|
||
<div class="setting-control setting-control--stacked">
|
||
<t-select
|
||
v-model="localSansFont"
|
||
style="width: 280px;"
|
||
:placeholder="$t('font.selectFont')"
|
||
@change="handleSansFontChange"
|
||
>
|
||
<t-option
|
||
v-for="opt in sansFontOptions"
|
||
:key="opt.value"
|
||
:value="opt.value"
|
||
:label="opt.label"
|
||
>
|
||
<span :style="{ fontFamily: opt.preview }">{{ opt.label }}</span>
|
||
</t-option>
|
||
</t-select>
|
||
<div class="font-preview" :style="{ fontFamily: currentSansStack }">
|
||
{{ $t('font.sansPreview') }}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 代码字体 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('font.monoFont') }}</label>
|
||
<p class="desc">{{ $t('font.monoFontDescription') }}</p>
|
||
</div>
|
||
<div class="setting-control setting-control--stacked">
|
||
<t-select
|
||
v-model="localMonoFont"
|
||
style="width: 280px;"
|
||
:placeholder="$t('font.selectFont')"
|
||
@change="handleMonoFontChange"
|
||
>
|
||
<t-option
|
||
v-for="opt in monoFontOptions"
|
||
:key="opt.value"
|
||
:value="opt.value"
|
||
:label="opt.label"
|
||
>
|
||
<span :style="{ fontFamily: opt.preview }">{{ opt.label }}</span>
|
||
</t-option>
|
||
</t-select>
|
||
<div class="font-preview font-preview--mono" :style="{ fontFamily: currentMonoStack }">
|
||
{{ $t('font.monoPreview') }}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 字体大小 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('font.fontSize') }}</label>
|
||
<p class="desc">{{ $t('font.fontSizeDescription') }}</p>
|
||
</div>
|
||
<div class="setting-control">
|
||
<t-radio-group
|
||
v-model="localFontSize"
|
||
@change="handleFontSizeChange"
|
||
>
|
||
<t-radio-button value="small">{{ $t('font.size.small') }}</t-radio-button>
|
||
<t-radio-button value="normal">{{ $t('font.size.normal') }}</t-radio-button>
|
||
<t-radio-button value="large">{{ $t('font.size.large') }}</t-radio-button>
|
||
</t-radio-group>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 记忆功能开关 -->
|
||
<div class="setting-row">
|
||
<div class="setting-info">
|
||
<label>{{ $t('settings.enableMemory') }}</label>
|
||
<p class="desc">{{ $t('settings.enableMemoryDesc') }}</p>
|
||
</div>
|
||
<div class="setting-control">
|
||
<t-switch
|
||
:value="isMemoryEnabled"
|
||
:disabled="!isNeo4jAvailable || memorySaving"
|
||
:loading="memorySaving"
|
||
@change="handleMemoryChange"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<t-alert
|
||
v-if="!isNeo4jAvailable"
|
||
theme="warning"
|
||
style="margin-top: -8px; margin-bottom: 16px;"
|
||
>
|
||
<template #message>
|
||
<div>{{ $t('settings.memoryRequiresNeo4j') }}</div>
|
||
<t-link theme="primary" href="https://github.com/Tencent/WeKnora/blob/main/docs/KnowledgeGraph.md" target="_blank">
|
||
{{ $t('settings.memoryHowToEnable') }}
|
||
</t-link>
|
||
</template>
|
||
</t-alert>
|
||
|
||
<!-- 自动下载更新开关 (Lite edition only) -->
|
||
<div class="setting-row" v-if="authStore.isLiteMode">
|
||
<div class="setting-info">
|
||
<label>{{ $t('settings.autoCheckUpdate') }}</label>
|
||
<p class="desc">{{ $t('settings.autoCheckUpdateDesc') }}</p>
|
||
</div>
|
||
<div class="setting-control">
|
||
<t-switch
|
||
v-model="isAutoCheckUpdateEnabled"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, onMounted, computed, watch } from 'vue'
|
||
import { MessagePlugin } from 'tdesign-vue-next'
|
||
import { useI18n } from 'vue-i18n'
|
||
import { useSettingsStore } from '@/stores/settings'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { getSystemInfo } from '@/api/system'
|
||
import { useTheme, type ThemeMode } from '@/composables/useTheme'
|
||
import {
|
||
useFont,
|
||
SANS_STACKS,
|
||
MONO_STACKS,
|
||
visibleSansKeys,
|
||
visibleMonoKeys,
|
||
type FontKey,
|
||
type MonoFontKey,
|
||
type FontSizeKey,
|
||
} from '@/composables/useFont'
|
||
|
||
const { t, locale } = useI18n()
|
||
const settingsStore = useSettingsStore()
|
||
const authStore = useAuthStore()
|
||
const { currentTheme, setTheme } = useTheme()
|
||
const {
|
||
currentSans,
|
||
currentMono,
|
||
currentSize,
|
||
setSansFont,
|
||
setMonoFont,
|
||
setFontSize,
|
||
} = useFont()
|
||
|
||
// 本地状态
|
||
const localLanguage = ref('zh-CN')
|
||
const localTheme = ref<ThemeMode>(currentTheme.value)
|
||
const localSansFont = ref<FontKey>(currentSans.value)
|
||
const localMonoFont = ref<MonoFontKey>(currentMono.value)
|
||
const localFontSize = ref<FontSizeKey>(currentSize.value)
|
||
|
||
// Keep the form in sync if preferences change externally (e.g. on user switch).
|
||
watch(currentTheme, (val) => { localTheme.value = val })
|
||
watch(currentSans, (val) => { localSansFont.value = val })
|
||
watch(currentMono, (val) => { localMonoFont.value = val })
|
||
watch(currentSize, (val) => { localFontSize.value = val })
|
||
|
||
const sansFontOptions = computed<{ value: FontKey; label: string; preview: string }[]>(() =>
|
||
visibleSansKeys().map((key) => ({
|
||
value: key,
|
||
label: t(`font.sans.${key}`),
|
||
preview: SANS_STACKS[key],
|
||
})),
|
||
)
|
||
|
||
const monoFontOptions = computed<{ value: MonoFontKey; label: string; preview: string }[]>(() =>
|
||
visibleMonoKeys().map((key) => ({
|
||
value: key,
|
||
label: t(`font.mono.${key}`),
|
||
preview: MONO_STACKS[key],
|
||
})),
|
||
)
|
||
|
||
// Live preview stacks, driven by the local form refs so the preview row
|
||
// updates immediately on selection — even before handleSansFontChange
|
||
// commits the choice to the global store and writes the CSS variable.
|
||
const currentSansStack = computed(() => SANS_STACKS[localSansFont.value] ?? SANS_STACKS.system)
|
||
const currentMonoStack = computed(() => MONO_STACKS[localMonoFont.value] ?? MONO_STACKS.system)
|
||
|
||
// 系统信息
|
||
const systemInfo = ref<any>(null)
|
||
|
||
const isNeo4jAvailable = computed(() => {
|
||
return systemInfo.value?.graph_database_engine && systemInfo.value.graph_database_engine !== '未启用'
|
||
})
|
||
|
||
// 记忆功能状态:只读 computed(toggleMemory 现在是 async + 后端持久化,
|
||
// 触发路径统一走 @change → handleMemoryChange,避免 v-model setter 二次调用)。
|
||
const isMemoryEnabled = computed(() => settingsStore.isMemoryEnabled)
|
||
const memorySaving = ref(false)
|
||
|
||
// 自动检查更新状态
|
||
const isAutoCheckUpdateEnabled = computed({
|
||
get: () => settingsStore.isAutoCheckUpdateEnabled,
|
||
set: (val) => {
|
||
settingsStore.toggleAutoCheckUpdate(val)
|
||
if (val) {
|
||
// @ts-ignore
|
||
if (window.go && window.go.main && window.go.main.App && window.go.main.App.AutoCheckForUpdates) {
|
||
// @ts-ignore
|
||
window.go.main.App.AutoCheckForUpdates()
|
||
}
|
||
}
|
||
}
|
||
})
|
||
|
||
// 初始化加载
|
||
onMounted(async () => {
|
||
// 从 localStorage 加载语言设置
|
||
const savedLocale = localStorage.getItem('locale')
|
||
if (savedLocale) {
|
||
localLanguage.value = savedLocale
|
||
locale.value = savedLocale
|
||
} else {
|
||
localLanguage.value = locale.value
|
||
}
|
||
|
||
// 加载系统信息以检查 Neo4j 可用性
|
||
try {
|
||
const response = await getSystemInfo()
|
||
systemInfo.value = response.data
|
||
if (!isNeo4jAvailable.value && settingsStore.isMemoryEnabled) {
|
||
// Neo4j 不可用 → 兜底关掉。后端写入失败不打断主流程(页面级 best-effort)。
|
||
void settingsStore.toggleMemory(false).catch(() => {})
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to load system info:', error)
|
||
}
|
||
})
|
||
|
||
// 处理语言变化
|
||
const handleLanguageChange = () => {
|
||
locale.value = localLanguage.value
|
||
localStorage.setItem('locale', localLanguage.value)
|
||
MessagePlugin.success(t('language.languageSaved'))
|
||
}
|
||
|
||
// 处理记忆功能变化。
|
||
// toggleMemory 是 async:先乐观写本地、再 PUT 后端;失败会回滚并 throw。
|
||
// UI 在 saving 期间禁用开关 + 显示 loading,避免用户在请求未完成时反复点。
|
||
const handleMemoryChange = async (val: boolean) => {
|
||
if (val && !isNeo4jAvailable.value) {
|
||
MessagePlugin.warning(t('settings.memoryRequiresNeo4j'))
|
||
return
|
||
}
|
||
memorySaving.value = true
|
||
try {
|
||
await settingsStore.toggleMemory(val)
|
||
MessagePlugin.success(t('common.success'))
|
||
} catch (err: any) {
|
||
MessagePlugin.error(err?.message || t('error.auth.updatePreferencesFailed'))
|
||
} finally {
|
||
memorySaving.value = false
|
||
}
|
||
}
|
||
|
||
// 处理主题变化
|
||
const handleThemeChange = (val: ThemeMode) => {
|
||
if (!setTheme(val)) {
|
||
// Setter rejected the value (validation guard); roll the form back to
|
||
// the canonical state so the UI doesn't drift.
|
||
localTheme.value = currentTheme.value
|
||
return
|
||
}
|
||
MessagePlugin.success(t('common.success'))
|
||
}
|
||
|
||
// 处理字体变化
|
||
const handleSansFontChange = (val: FontKey) => {
|
||
if (!setSansFont(val)) {
|
||
localSansFont.value = currentSans.value
|
||
return
|
||
}
|
||
MessagePlugin.success(t('common.success'))
|
||
}
|
||
|
||
const handleMonoFontChange = (val: MonoFontKey) => {
|
||
if (!setMonoFont(val)) {
|
||
localMonoFont.value = currentMono.value
|
||
return
|
||
}
|
||
MessagePlugin.success(t('common.success'))
|
||
}
|
||
|
||
const handleFontSizeChange = (val: FontSizeKey) => {
|
||
if (!setFontSize(val)) {
|
||
localFontSize.value = currentSize.value
|
||
return
|
||
}
|
||
MessagePlugin.success(t('common.success'))
|
||
}
|
||
</script>
|
||
|
||
<style lang="less" scoped>
|
||
.general-settings {
|
||
width: 100%;
|
||
}
|
||
|
||
.section-header {
|
||
margin-bottom: 32px;
|
||
|
||
h2 {
|
||
font-size: 20px;
|
||
font-weight: 600;
|
||
color: var(--td-text-color-primary);
|
||
margin: 0 0 8px 0;
|
||
}
|
||
|
||
.section-description {
|
||
font-size: 14px;
|
||
color: var(--td-text-color-secondary);
|
||
margin: 0;
|
||
line-height: 1.5;
|
||
}
|
||
}
|
||
|
||
.settings-group {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 0;
|
||
}
|
||
|
||
.setting-row {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
justify-content: space-between;
|
||
padding: 20px 0;
|
||
border-bottom: 1px solid var(--td-component-stroke);
|
||
|
||
&:last-child {
|
||
border-bottom: none;
|
||
}
|
||
}
|
||
|
||
.setting-info {
|
||
flex: 1;
|
||
max-width: 65%;
|
||
padding-right: 24px;
|
||
|
||
label {
|
||
font-size: 15px;
|
||
font-weight: 500;
|
||
color: var(--td-text-color-primary);
|
||
display: block;
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.desc {
|
||
font-size: 13px;
|
||
color: var(--td-text-color-secondary);
|
||
margin: 0;
|
||
line-height: 1.5;
|
||
}
|
||
}
|
||
|
||
.setting-control {
|
||
flex-shrink: 0;
|
||
min-width: 280px;
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
align-items: center;
|
||
}
|
||
|
||
// When a font picker is rendered, stack the select on top of a live
|
||
// preview line so the user can verify their choice without hunting for
|
||
// an API Info page or a code block.
|
||
.setting-control--stacked {
|
||
flex-direction: column;
|
||
align-items: flex-end;
|
||
gap: 8px;
|
||
}
|
||
|
||
.font-preview {
|
||
width: 280px;
|
||
padding: 8px 12px;
|
||
border: 1px solid var(--td-component-stroke);
|
||
border-radius: var(--td-radius-medium);
|
||
background: var(--td-bg-color-container);
|
||
color: var(--td-text-color-primary);
|
||
font-size: 14px;
|
||
line-height: 1.4;
|
||
text-align: left;
|
||
box-sizing: border-box;
|
||
|
||
&--mono {
|
||
// Harden the preview against wrap-around for long monospace samples.
|
||
white-space: nowrap;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
}
|
||
}
|
||
</style> |