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) {
|
||||
|
||||
Reference in New Issue
Block a user