flat: ts + 接口错误处理

This commit is contained in:
Apcallover
2025-11-11 21:31:32 +08:00
parent 59e04e53b1
commit 120bad4abe
5 changed files with 58 additions and 3 deletions

View File

@@ -98,7 +98,7 @@ export function setupLiveHandlers() {
contextIsolation: false, contextIsolation: false,
}, },
}); });
liveWindow.webContents.openDevTools(); // liveWindow.webContents.openDevTools();
liveWindow.on("closed", () => { liveWindow.on("closed", () => {
liveWindow = null; liveWindow = null;
}); });
@@ -134,6 +134,33 @@ async function getSessionId(requestBody: object) {
body: JSON.stringify(requestBody), body: JSON.stringify(requestBody),
}); });
// 首先检查响应内容类型
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
// 如果不是JSON读取原始文本进行调试
const rawText = await response.text();
console.warn("服务器返回非JSON响应:", rawText);
// 尝试解析可能的JSON响应即使Content-Type不正确
try {
const data = JSON.parse(rawText);
if (response.ok && data.sessionid) {
return { success: true, sessionId: data.sessionid };
} else {
return {
success: false,
error: data.message || "服务器返回非JSON格式",
};
}
} catch (parseError) {
return {
success: false,
error: `服务器响应格式错误: ${rawText.substring(0, 100)}...`,
};
}
}
// 如果是JSON正常解析
const data = await response.json(); const data = await response.json();
if (response.ok && data.sessionid) { if (response.ok && data.sessionid) {

View File

@@ -141,6 +141,7 @@ export function setupWorkflowHandlers() {
// 将整个工作流封装在 IPC Handler 中 // 将整个工作流封装在 IPC Handler 中
ipcMain.handle("run-job-workflow", async (_, userQuery) => { ipcMain.handle("run-job-workflow", async (_, userQuery) => {
console.log("工作流: 正在准备工作...");
let currentJobData = userQuery || {}; let currentJobData = userQuery || {};
let answerText = ""; let answerText = "";

View File

@@ -8,6 +8,28 @@ export async function getSessionId(requestBody: object) {
body: JSON.stringify(requestBody), body: JSON.stringify(requestBody),
}); });
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
const rawText = await response.text();
try {
const data = JSON.parse(rawText);
if (response.ok && data.sessionid) {
return { success: true, sessionId: data.sessionid };
} else {
return {
success: false,
error: data.message || "服务器返回非JSON格式",
};
}
} catch (parseError) {
return {
success: false,
error: `服务器响应格式错误: ${rawText.substring(0, 100)}...`,
};
}
}
// 如果是JSON正常解析
const data = await response.json(); const data = await response.json();
if (response.ok && data.sessionid) { if (response.ok && data.sessionid) {

View File

@@ -29,8 +29,11 @@ export const useLiveStore = defineStore("live", () => {
if (positions.value.length < 10) { if (positions.value.length < 10) {
fetchPositions(); fetchPositions();
} }
} else {
throw new Error(resData.msg);
} }
} catch (error) { } catch (error) {
alert("获取岗位失败");
console.error("获取岗位列表失败:", error); console.error("获取岗位列表失败:", error);
} }
} }
@@ -117,7 +120,7 @@ export const useLiveStore = defineStore("live", () => {
try { try {
const result = await window.electron.ipcRenderer.invoke( const result = await window.electron.ipcRenderer.invoke(
"open-live-window", "open-live-window",
{ path: "live", width: 375, height: 682 }, { path: "live", width: 375, height: 682, userId: "rs876543" },
); );
if (result.success) { if (result.success) {
isLiveWindowOpen.value = true; isLiveWindowOpen.value = true;

View File

@@ -20,7 +20,7 @@ const isCameraActive = ref(false)
const isfullScreen = ref(false); const isfullScreen = ref(false);
const userId = ref(null); const userId = ref(null);
const liveUrl = ref(""); const liveUrl = ref("");
const soundUrl = ref("https://dmdemo.hx.cn/sound/welcome.mp3"); const soundUrl = ref("");
const welcome = ref() const welcome = ref()
const live = ref() const live = ref()
let cameraStream = null let cameraStream = null
@@ -73,8 +73,10 @@ onBeforeUnmount(() => {
function startLive() { function startLive() {
const paramsUserId = route.query.userId; const paramsUserId = route.query.userId;
console.log(paramsUserId)
if (paramsUserId) { if (paramsUserId) {
userId.value = paramsUserId userId.value = paramsUserId
liveUrl.value = `https://dmdemo.hx.cn/dashboard.html?userId=${userId.value}`; liveUrl.value = `https://dmdemo.hx.cn/dashboard.html?userId=${userId.value}`;
} }
} }