webhook 通知 body 增加 text/plain 类型,修复多定时任务服务重启未初始化

This commit is contained in:
whyour
2023-11-22 22:43:48 +08:00
parent f056b611a4
commit de9e9ce627
7 changed files with 29 additions and 19 deletions
+8 -3
View File
@@ -61,7 +61,8 @@ export async function getNetIp(req: any) {
...req.ips,
req.socket.remoteAddress,
]),
];
].filter(Boolean);
let ip = ipArray[0];
if (ipArray.length > 1) {
@@ -82,6 +83,7 @@ export async function getNetIp(req: any) {
break;
}
}
ip = ip.substr(ip.lastIndexOf(':') + 1, ip.length);
if (ip.includes('127.0') || ip.includes('192.168') || ip.includes('10.7')) {
ip = '';
@@ -347,9 +349,12 @@ export function parseBody(
contentType:
| 'application/json'
| 'multipart/form-data'
| 'application/x-www-form-urlencoded',
| 'application/x-www-form-urlencoded'
| 'text/plain',
) {
if (!body) return '';
if (contentType === 'text/plain' || !body) {
return body;
}
const parsed: any = {};
let key;
+8 -8
View File
@@ -26,9 +26,9 @@ import omit from 'lodash/omit';
export default class CronService {
constructor(@Inject('logger') private logger: winston.Logger) {}
private isSixCron(cron: Crontab) {
const { schedule } = cron;
if (Number(schedule?.split(/ +/).length) > 5) {
private isNodeCron(cron: Crontab) {
const { schedule, extra_schedules } = cron;
if (Number(schedule?.split(/ +/).length) > 5 || extra_schedules?.length) {
return true;
}
return false;
@@ -38,7 +38,7 @@ export default class CronService {
const tab = new Crontab(payload);
tab.saved = false;
const doc = await this.insert(tab);
if (this.isSixCron(doc) || doc.extra_schedules?.length) {
if (this.isNodeCron(doc)) {
await cronClient.addCron([
{
name: doc.name || '',
@@ -65,10 +65,10 @@ export default class CronService {
if (doc.isDisabled === 1) {
return newDoc;
}
if (this.isSixCron(doc) || doc.extra_schedules?.length) {
if (this.isNodeCron(doc)) {
await cronClient.delCron([String(doc.id)]);
}
if (this.isSixCron(newDoc) || newDoc.extra_schedules?.length) {
if (this.isNodeCron(newDoc)) {
await cronClient.addCron([
{
name: doc.name || '',
@@ -472,7 +472,7 @@ export default class CronService {
await CrontabModel.update({ isDisabled: 0 }, { where: { id: ids } });
const docs = await CrontabModel.findAll({ where: { id: ids } });
const sixCron = docs
.filter((x) => this.isSixCron(x))
.filter((x) => this.isNodeCron(x))
.map((doc) => ({
name: doc.name || '',
id: String(doc.id),
@@ -613,7 +613,7 @@ export default class CronService {
this.set_crontab(tabs);
const sixCron = tabs.data
.filter((x) => this.isSixCron(x) && x.isDisabled !== 1)
.filter((x) => this.isNodeCron(x) && x.isDisabled !== 1)
.map((doc) => ({
name: doc.name || '',
id: String(doc.id),
+4 -1
View File
@@ -659,9 +659,11 @@ export default class NotificationService {
webhookUrl,
webhookBody,
);
if (!formatUrl && !formatBody) {
return false;
throw new Error('Url 或者 Body 中必须包含 $title')
}
const headers = parseHeaders(webhookHeaders);
const body = parseBody(formatBody, webhookContentType);
const bodyParam = this.formatBody(webhookContentType, body);
@@ -692,6 +694,7 @@ export default class NotificationService {
case 'multipart/form-data':
return { form: body };
case 'application/x-www-form-urlencoded':
case 'text/plain':
return { body };
}
return {};