Files
live-stream-app/src/main/Api/api.ts
2025-11-16 18:11:30 +08:00

97 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export async function getSessionId(requestBody: object) {
try {
const response = await fetch("http://ywpt.hx.cn/dmhx/get_sessionid", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
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();
if (response.ok && data.sessionid) {
return { success: true, sessionId: data.sessionid };
} else {
return { success: false, error: data.message || "未知错误" };
}
} catch (error: any) {
console.error("Error in getSessionId:", error);
return { success: false, error: error.message };
}
}
export async function sendMessage(requestBody: object) {
try {
const response = await fetch(`http://ywpt.hx.cn/dmhx/human`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
const data = await response.json();
if (response.ok) {
return { success: true };
} else {
return { success: false, error: data.message || "未知错误" };
}
} catch (error: any) {
console.error("Error in sendMessage:", error);
return { success: false, error: error.message };
}
}
export async function getJob(requestBody: object) {
try {
const response = await fetch(
`https://qd.zhaopinzao8dian.com/api/app/job/metaInfo`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
},
);
const resData = await response.json();
if (resData.code === 200) {
return { success: true, data: resData.data };
} else {
throw new Error(resData.msg);
}
} catch (error: any) {
console.error("Error in sendMessage:", error);
return { success: false, error: error.message };
}
}