mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
QLAPI.systemNotify 支持自定义通知类型和参数
This commit is contained in:
+23
-4
@@ -49,12 +49,21 @@ export default class NotificationService {
|
||||
public async notify(
|
||||
title: string,
|
||||
content: string,
|
||||
notificationInfo?: NotificationInfo,
|
||||
): Promise<boolean | undefined> {
|
||||
const { type, ...rest } = await this.userService.getNotificationMode();
|
||||
let { type, ...rest } = await this.userService.getNotificationMode();
|
||||
if (notificationInfo?.type) {
|
||||
type = notificationInfo?.type;
|
||||
}
|
||||
if (type) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
this.params = rest;
|
||||
let params = rest;
|
||||
if (notificationInfo) {
|
||||
const { type: _, ...others } = notificationInfo;
|
||||
params = { ...rest, ...others };
|
||||
}
|
||||
this.params = params;
|
||||
const notificationModeAction = this.modeMap.get(type);
|
||||
try {
|
||||
return await notificationModeAction?.call(this);
|
||||
@@ -623,7 +632,15 @@ export default class NotificationService {
|
||||
}
|
||||
|
||||
private async ntfy() {
|
||||
const { ntfyUrl, ntfyTopic, ntfyPriority, ntfyToken, ntfyUsername, ntfyPassword, ntfyActions } = this.params;
|
||||
const {
|
||||
ntfyUrl,
|
||||
ntfyTopic,
|
||||
ntfyPriority,
|
||||
ntfyToken,
|
||||
ntfyUsername,
|
||||
ntfyPassword,
|
||||
ntfyActions,
|
||||
} = this.params;
|
||||
// 编码函数
|
||||
const encodeRfc2047 = (text: string, charset: string = 'UTF-8'): string => {
|
||||
const encodedText = Buffer.from(text).toString('base64');
|
||||
@@ -638,7 +655,9 @@ export default class NotificationService {
|
||||
if (ntfyToken) {
|
||||
headers['Authorization'] = `Bearer ${ntfyToken}`;
|
||||
} else if (ntfyUsername && ntfyPassword) {
|
||||
headers['Authorization'] = `Basic ${Buffer.from(`${ntfyUsername}:${ntfyPassword}`).toString('base64')}`;
|
||||
headers['Authorization'] = `Basic ${Buffer.from(
|
||||
`${ntfyUsername}:${ntfyPassword}`,
|
||||
).toString('base64')}`;
|
||||
}
|
||||
if (ntfyActions) {
|
||||
headers['Actions'] = encodeRfc2047(ntfyActions);
|
||||
|
||||
+22
-3
@@ -7,7 +7,7 @@ import path from 'path';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import winston from 'winston';
|
||||
import config from '../config';
|
||||
import { TASK_COMMAND } from '../config/const';
|
||||
import { NotificationModeStringMap, TASK_COMMAND } from '../config/const';
|
||||
import {
|
||||
getPid,
|
||||
killTask,
|
||||
@@ -373,8 +373,27 @@ export default class SystemService {
|
||||
return { code: 200 };
|
||||
}
|
||||
|
||||
public async notify({ title, content }: { title: string; content: string }) {
|
||||
const isSuccess = await this.notificationService.notify(title, content);
|
||||
public async notify({
|
||||
title,
|
||||
content,
|
||||
notificationInfo,
|
||||
}: {
|
||||
title: string;
|
||||
content: string;
|
||||
notificationInfo?: NotificationInfo;
|
||||
}) {
|
||||
const typeString =
|
||||
typeof notificationInfo?.type === 'number'
|
||||
? NotificationModeStringMap[notificationInfo.type]
|
||||
: undefined;
|
||||
if (notificationInfo && typeString) {
|
||||
notificationInfo.type = typeString;
|
||||
}
|
||||
const isSuccess = await this.notificationService.notify(
|
||||
title,
|
||||
content,
|
||||
notificationInfo,
|
||||
);
|
||||
if (isSuccess) {
|
||||
return { code: 200, message: '通知发送成功' };
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user