refactor: 重构设置对话框的API调用逻辑,优化错误处理和默认设置初始化
This commit is contained in:
parent
039e3fc0bb
commit
e3dfffad7d
|
|
@ -349,27 +349,25 @@ app.registerExtension({
|
|||
}
|
||||
async function showSettingsDialog() {
|
||||
try {
|
||||
const resp = await api.fetchApi("/publisher/settings", {
|
||||
cache: "no-store",
|
||||
});
|
||||
if (!resp.ok) {
|
||||
throw new Error(`服务器错误: ${resp.status} ${resp.statusText}`);
|
||||
}
|
||||
const text = await resp.text();
|
||||
let settings;
|
||||
try {
|
||||
settings = text ? JSON.parse(text) : {};
|
||||
} catch (parseError) {
|
||||
console.error("JSON解析失败:", parseError, "原始响应:", text);
|
||||
settings = { api_url: "", host: "" };
|
||||
}
|
||||
let settings = { api_url: "", host: "" };
|
||||
|
||||
// 确保设置对象有必需的字段
|
||||
settings = {
|
||||
api_url: settings.api_url || "",
|
||||
host: settings.host || "",
|
||||
...settings,
|
||||
};
|
||||
await api
|
||||
.fetchApi("/publisher/settings", {
|
||||
cache: "no-store",
|
||||
})
|
||||
.then(async (resp) => {
|
||||
if (!resp.ok) {
|
||||
console.error(`服务器错误: ${resp.status} ${resp.statusText}`);
|
||||
return;
|
||||
}
|
||||
const text = await resp.text();
|
||||
try {
|
||||
settings = text ? JSON.parse(text) : {};
|
||||
} catch (parseError) {
|
||||
console.error("JSON解析失败:", parseError, "原始响应:", text);
|
||||
settings = { api_url: "", host: "" };
|
||||
}
|
||||
});
|
||||
|
||||
settingsDialog.setContent(`
|
||||
<label for="publisher-host" style="display: block; margin-bottom: 5px;">Host:</label>
|
||||
|
|
|
|||
Loading…
Reference in New Issue