fix(frontend/settings): fix json marshal fault, now the settings page could be loaded correctly

This commit is contained in:
2026-09-10 20:12:21 +08:00
parent 93d5ac2707
commit 8797f8eda6
2 changed files with 26 additions and 1 deletions
+3
View File
@@ -16,6 +16,9 @@ export async function loadDebugMode() {
const config = (res && res.data && res.data.config) || {};
const system = config.system || {};
debugMode.value = !!system.debugMode;
if (debugMode.value) {
console.log('Debug mode enabled');
}
} catch {
// Non-fatal: the flag simply keeps its current value.
}
+23 -1
View File
@@ -1,6 +1,7 @@
<script setup>
import { onMounted, reactive, ref } from 'vue';
import { settingsApi } from '../api/index.js';
import { debugMode } from '../utils/runtime.js';
import { toast } from '../utils/toast.js';
import Toggle from '../components/Toggle.vue';
import TagsEditor from '../components/TagsEditor.vue';
@@ -155,6 +156,18 @@ function getVal(obj, path, fb) {
return cur === undefined || cur === null ? fb : cur;
}
// hasVal reports whether a path actually resolves in the loaded config. A
// missing key and a key whose value equals the fallback are indistinguishable
// from getVal's return value alone, so misses are detected separately.
function hasVal(obj, path) {
let cur = obj;
for (const k of path) {
if (cur === null || cur === undefined || typeof cur !== 'object') return false;
cur = cur[k];
}
return cur !== undefined && cur !== null;
}
function defaults(f) {
switch (f.type) {
case 'bool': return false;
@@ -172,7 +185,16 @@ async function load() {
for (const s of sections) {
const obj = {};
for (const f of s.fields) {
obj[f.key] = getVal(cfg, fieldPath(f), defaults(f));
// Mirror wrapRoot() on save: prepend the section's root path so
// the value is read from the same place it is written to.
const path = [...s.root, ...fieldPath(f)];
obj[f.key] = getVal(cfg, path, defaults(f));
if (debugMode.value) {
console.log(`[Settings] Loaded ${s.id}.${f.key}:`, obj[f.key]);
if (!hasVal(cfg, path)) {
console.warn(`[Settings] ${s.id}.${f.key} missing at "${path.join('.')}" — using default`);
}
}
}
vals[s.id] = obj;
}