fix: 修复站点监控通知通过webhook发送失败的bug

This commit is contained in:
xiaojunnuo
2025-03-17 18:20:15 +08:00
parent 729b19c8da
commit 9be1ecc8aa
4 changed files with 12 additions and 13 deletions
@@ -122,7 +122,9 @@ export abstract class BaseNotification implements INotification {
return await this.doSend({ return await this.doSend({
userId: 0, userId: 0,
title: "【Certd】测试通知【*.foo.com】,标题长度测试、测试、测试", title: "【Certd】测试通知【*.foo.com】,标题长度测试、测试、测试",
content: "测试通知,*.foo.com", content: `测试通知,*.foo.com
换行测试
`,
pipeline: { pipeline: {
id: 1, id: 1,
title: "证书申请成功【测试流水线】", title: "证书申请成功【测试流水线】",
@@ -235,9 +235,10 @@ export default function ({ crudExpose, context }: CreateCrudOptionsProps): Creat
show: false show: false
}, },
column: { column: {
width: 100, width: 110,
sorter: true, sorter: true,
show: false show: true,
align: "center"
} }
}, },
certExpiresTime: { certExpiresTime: {
@@ -173,9 +173,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
body: { body: {
url, url,
title: `站点证书检查出错<${site.name}>`, title: `站点证书检查出错<${site.name}>`,
content: `站点名称: ${site.name} \n content: `站点名称: ${site.name} \n站点域名: ${site.domain} \n错误信息:${site.error}`,
站点域名: ${site.domain} \n
错误信息:${site.error}`,
}, },
}, },
site.userId site.userId
@@ -188,11 +186,7 @@ export class SiteInfoService extends BaseService<SiteInfoEntity> {
const expires = site.certExpiresTime; const expires = site.certExpiresTime;
const validDays = dayjs(expires).diff(dayjs(), 'day'); const validDays = dayjs(expires).diff(dayjs(), 'day');
const url = await this.notificationService.getBindUrl('#/monitor/site'); const url = await this.notificationService.getBindUrl('#/monitor/site');
const content = `站点名称: ${site.name} \n const content = `站点名称: ${site.name} \n站点域名: ${site.domain} \n证书域名: ${site.certDomains} \n证书颁发者: ${site.certProvider} \n过期时间: ${dayjs(site.certExpiresTime).format('YYYY-MM-DD')} \n`;
站点域名: ${site.domain} \n
证书域名: ${site.certDomains} \n
证书颁发者: ${site.certProvider} \n
过期时间: ${dayjs(site.certExpiresTime).format('YYYY-MM-DD')} \n`;
if (validDays >= 0 && validDays < tipDays) { if (validDays >= 0 && validDays < tipDays) {
// 发通知 // 发通知
await this.notificationService.send( await this.notificationService.send(
@@ -78,7 +78,7 @@ export class WebhookNotification extends BaseNotification {
col: { col: {
span: 24, span: 24,
}, },
helper: `根据实际的webhook接口,构建一个json对象作为参数支持变量:{title}、{content}、{url},变量用{}包裹字符串需要双引号\n如果是get方式,将作为query参数拼接到url上`, helper: `根据对应的webhook接口文档,构建一个json对象作为参数(默认值只是一个示例,一般不是正确的参数)\n支持变量:{title}、{content}、{url},变量用{}包裹\n字符串需要双引号,使用\\n换行\n如果是get方式,将作为query参数拼接到url上`,
required: true, required: true,
}) })
template = ''; template = '';
@@ -98,8 +98,10 @@ export class WebhookNotification extends BaseNotification {
let bodyStr = target; let bodyStr = target;
const keys = Object.keys(body); const keys = Object.keys(body);
for (const key of keys) { for (const key of keys) {
const value = urlEncode ? encodeURIComponent(body[key]) : body[key]; let value = urlEncode ? encodeURIComponent(body[key]) : body[key];
value = value.replaceAll(`\n`, "\\n");
bodyStr = bodyStr.replaceAll(`{${key}}`, value); bodyStr = bodyStr.replaceAll(`{${key}}`, value);
} }
return bodyStr; return bodyStr;
} }