mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-25 00:16:06 +08:00
fix:body json多参数时无法被正确分割的问题
This commit is contained in:
parent
81898f9dd7
commit
52d1b9bf5e
|
@ -1273,19 +1273,24 @@ function chronocatNotify(title, desp) {
|
||||||
|
|
||||||
function webhookNotify(text, desp) {
|
function webhookNotify(text, desp) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const { formatBody, formatUrl } = formatNotifyContentFun(
|
const formatUrl = formatNotifyContentFun(
|
||||||
WEBHOOK_URL,
|
WEBHOOK_URL,
|
||||||
WEBHOOK_BODY,
|
encodeURIComponent(text),
|
||||||
text,
|
encodeURIComponent(desp),
|
||||||
desp,
|
|
||||||
);
|
);
|
||||||
if (!formatUrl && !formatBody) {
|
|
||||||
|
if (!formatUrl) {
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const headers = parseHeaders(WEBHOOK_HEADERS);
|
const headers = parseHeaders(WEBHOOK_HEADERS);
|
||||||
const body = parseBody(formatBody, WEBHOOK_CONTENT_TYPE);
|
const body = parseBody(WEBHOOK_BODY, WEBHOOK_CONTENT_TYPE,text,desp);
|
||||||
|
if (!body) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const bodyParam = formatBodyFun(WEBHOOK_CONTENT_TYPE, body);
|
const bodyParam = formatBodyFun(WEBHOOK_CONTENT_TYPE, body);
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
method: WEBHOOK_METHOD,
|
method: WEBHOOK_METHOD,
|
||||||
headers,
|
headers,
|
||||||
|
@ -1294,14 +1299,15 @@ function webhookNotify(text, desp) {
|
||||||
timeout,
|
timeout,
|
||||||
retry: 1,
|
retry: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (WEBHOOK_METHOD) {
|
if (WEBHOOK_METHOD) {
|
||||||
got(formatUrl, options).then((resp) => {
|
got(formatUrl, options).then((resp) => {
|
||||||
try {
|
try {
|
||||||
if (resp.statusCode !== 200) {
|
if (resp.statusCode !== 200) {
|
||||||
console.log(`自定义发送通知消息失败!!\n${resp.body}`);
|
console.log('自定义发送通知消息失败!!\n');
|
||||||
|
console.log(resp.body);
|
||||||
} else {
|
} else {
|
||||||
console.log(`自定义发送通知消息成功🎉。\n${resp.body}`);
|
console.log('自定义发送通知消息成功🎉。\n');
|
||||||
|
console.log(resp.body);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
$.logErr(e, resp);
|
$.logErr(e, resp);
|
||||||
|
@ -1339,20 +1345,19 @@ function parseHeaders(headers) {
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseBody(body, contentType) {
|
function parseBody(body, contentType,notifyTitle,notifyDesp) {
|
||||||
if (contentType === 'text/plain' || !body) {
|
if (!body) return '';
|
||||||
return body;
|
|
||||||
}
|
const formatBodyChar = body.replace(/\\n/g, '\n');
|
||||||
|
|
||||||
const parsed = {};
|
const parsed = {};
|
||||||
let key;
|
let key;
|
||||||
let val;
|
let val;
|
||||||
let i;
|
let i;
|
||||||
|
formatBodyChar &&
|
||||||
body &&
|
formatBodyChar.split('\n').forEach(function parser(line) {
|
||||||
body.split('\n').forEach(function parser(line) {
|
|
||||||
i = line.indexOf(':');
|
i = line.indexOf(':');
|
||||||
key = line.substring(0, i).trim();
|
key = line.substring(0, i).trim().toLowerCase();
|
||||||
val = line.substring(i + 1).trim();
|
val = line.substring(i + 1).trim();
|
||||||
|
|
||||||
if (!key || parsed[key]) {
|
if (!key || parsed[key]) {
|
||||||
|
@ -1361,9 +1366,9 @@ function parseBody(body, contentType) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const jsonValue = JSON.parse(val);
|
const jsonValue = JSON.parse(val);
|
||||||
parsed[key] = jsonValue;
|
parsed[key] = formatNotifyContentFun(jsonValue,notifyTitle,notifyDesp);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
parsed[key] = val;
|
parsed[key] = formatNotifyContentFun(val,notifyTitle,notifyDesp);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1395,19 +1400,18 @@ function formatBodyFun(contentType, body) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatNotifyContentFun(url, body, title, content) {
|
|
||||||
if (!url.includes('$title') && !body.includes('$title')) {
|
function formatNotifyContentFun(originalContent, title, content) {
|
||||||
return {};
|
if (!originalContent) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!originalContent.includes('$content') && !originalContent.includes('$title')) {
|
||||||
|
return originalContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return originalContent
|
||||||
formatUrl: url
|
|
||||||
.replaceAll('$title', encodeURIComponent(title))
|
|
||||||
.replaceAll('$content', encodeURIComponent(content)),
|
|
||||||
formatBody: body
|
|
||||||
.replaceAll('$title', title)
|
.replaceAll('$title', title)
|
||||||
.replaceAll('$content', content),
|
.replaceAll('$content', content);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user