V2.2.8
This commit is contained in:
+34
-8
@@ -1,6 +1,10 @@
|
||||
|
||||
const { net } = require('electron');
|
||||
|
||||
function isString(contentType) {
|
||||
return contentType.startsWith('text/plain') || contentType.startsWith('application/json');
|
||||
}
|
||||
|
||||
let basicApi = {
|
||||
/**
|
||||
* 使用electron的net模块实现的get方法
|
||||
@@ -12,9 +16,27 @@ let basicApi = {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = net.request({ url, method: 'GET', headers });
|
||||
request.on('response', (response) => {
|
||||
let data = '';
|
||||
let data;
|
||||
if (isString(response.headers["content-type"])) {
|
||||
data = '';
|
||||
} else if (response.headers["content-type"].startsWith("image/")) {
|
||||
// 处理图片数据
|
||||
data = []
|
||||
}
|
||||
else {
|
||||
throw new Error("未知的请求返回数据类型");
|
||||
}
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
if (isString(response.headers["content-type"])) {
|
||||
// 处理 JSON 数据
|
||||
data += chunk;
|
||||
} else if (response.headers['content-type'].startsWith('image/')) {
|
||||
// 处理图片数据
|
||||
data.push(chunk);
|
||||
} else {
|
||||
throw new Error("未知的请求返回数据类型");
|
||||
}
|
||||
});
|
||||
response.on('end', () => {
|
||||
// 结束的时候检查请求的状态码,是不是成功的请求,不是返回错误,有些其他的状态码也是成功的请求,并且返回错误提示
|
||||
@@ -24,10 +46,13 @@ let basicApi = {
|
||||
}
|
||||
|
||||
let parsedData;
|
||||
if (response.headers['content-type'].includes('application/json')) {
|
||||
if (isString(response.headers["content-type"])) {
|
||||
parsedData = JSON.parse(data);
|
||||
} else if (response.headers['content-type'].startsWith('image/')) {
|
||||
// parsedData = responseData;
|
||||
parsedData = Buffer.concat(data);
|
||||
} else {
|
||||
parsedData = data;
|
||||
throw new Error("未知的请求返回数据类型");
|
||||
}
|
||||
|
||||
resolve({
|
||||
@@ -71,8 +96,7 @@ let basicApi = {
|
||||
|
||||
request.on('response', (response) => {
|
||||
let responseData;
|
||||
if (response.headers["content-type"] === "application/json") {
|
||||
|
||||
if (isString(response.headers["content-type"])) {
|
||||
responseData = '';
|
||||
} else if (response.headers["content-type"].startsWith("image/")) {
|
||||
// 处理图片数据
|
||||
@@ -83,12 +107,14 @@ let basicApi = {
|
||||
}
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
if (response.headers['content-type'] === 'application/json') {
|
||||
if (isString(response.headers["content-type"])) {
|
||||
// 处理 JSON 数据
|
||||
responseData += chunk;
|
||||
} else if (response.headers['content-type'].startsWith('image/')) {
|
||||
// 处理图片数据
|
||||
responseData.push(chunk);
|
||||
} else {
|
||||
throw new Error("未知的请求返回数据类型");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -99,7 +125,7 @@ let basicApi = {
|
||||
}
|
||||
|
||||
let parsedData;
|
||||
if (response.headers['content-type'].includes('application/json')) {
|
||||
if (isString(response.headers["content-type"])) {
|
||||
parsedData = JSON.parse(responseData);
|
||||
} else if (response.headers['content-type'].startsWith('image/')) {
|
||||
// parsedData = responseData;
|
||||
|
||||
@@ -88,7 +88,13 @@ export class DiscordAPI {
|
||||
res_data = JSON.parse(res_data);
|
||||
}
|
||||
if (res_data && res_data.code != 1) {
|
||||
throw new Error(res_data.message);
|
||||
if (res_data.message) {
|
||||
throw new Error(res_data.message);
|
||||
} else if (res_data.description) {
|
||||
throw new Error(res_data.description)
|
||||
} else {
|
||||
throw new Error("未知错误,可联系管理员排查");
|
||||
}
|
||||
}
|
||||
return res_data;
|
||||
} catch (error) {
|
||||
|
||||
@@ -277,14 +277,14 @@ export class MJOriginalImageGenerate {
|
||||
let imagine_url = apiUrl.mj_url.imagine;
|
||||
let once_get_task = apiUrl.mj_url.once_get_task;
|
||||
let task_count = mjSetting.task_count ? mjSetting.task_count : 3;
|
||||
let request_model = mjSetting.request_model ? mjSetting.request_model : "relaxed";
|
||||
let mj_speed = mjSetting.mj_speed ? mjSetting.mj_speed : "relaxed";
|
||||
let res;
|
||||
// 判断当前的API是哪个
|
||||
if (imagine_url.includes("mjapi.deepwl.net")) {
|
||||
// DrawAPI(MJ)
|
||||
let data = {
|
||||
prompt: prompt,
|
||||
mode: request_model == "fast" ? "FAST" : "RELAX",
|
||||
mode: mj_speed == "fast" ? "FAST" : "RELAX",
|
||||
}
|
||||
let headers = {
|
||||
"Authorization": mjSetting.api_key
|
||||
@@ -300,7 +300,7 @@ export class MJOriginalImageGenerate {
|
||||
botType: "MID_JOURNEY",
|
||||
accountFilter: {
|
||||
modes: [
|
||||
request_model == "fast" ? "FAST" : "RELAX"
|
||||
mj_speed == "fast" ? "FAST" : "RELAX"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ export class SD {
|
||||
return successMessage(data);
|
||||
|
||||
} catch (error) {
|
||||
return errorMessage(error.toString());
|
||||
return errorMessage("加载数据失败,错误信息如下:" +error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user