v2
xiaojunnuo 2025-08-07 11:26:14 +08:00
parent cbe0b1c5a6
commit 5f4a89cecc
2 changed files with 40 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import { Controller, Get, Inject, Provide } from '@midwayjs/core';
import {Body, Controller, Get, Inject, Post, Provide} from '@midwayjs/core';
import { BaseController, Constants, FileService, SysSettingsService, SysSiteInfo } from '@certd/lib-server';
import { http, logger } from '@certd/basic';
import { isComm } from '@certd/plus-core';
@ -46,4 +46,10 @@ export class AppController extends BaseController {
this.ctx.response.redirect(redirect);
this.ctx.response.set('Cache-Control', 'public,max-age=25920');
}
@Post('/webhook', { summary: Constants.per.guest })
public async webhook( @Body() body: any) {
logger.info('webhook', JSON.stringify(body))
return this.ok("success")
}
}

View File

@ -2,7 +2,6 @@ import qs from 'qs';
import {
AbstractTaskPlugin,
IsTaskPlugin,
NotificationBody,
pluginGroups,
RunStrategy,
TaskInput
@ -15,7 +14,7 @@ import {CertApplyPluginNames, CertInfo, CertReader} from "@certd/plugin-cert";
icon: 'ion:send-sharp',
desc: '调用webhook部署证书',
group: pluginGroups.other.key,
showRunStrategy:true,
showRunStrategy: false,
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
@ -97,8 +96,8 @@ export class WebhookDeployCert extends AbstractTaskPlugin {
title: '消息body模版',
value: `{
"id":"123",
"crt":"{crt}",
"key":"{key}"
"crt":"\${crt}",
"key":"\${key}"
}`,
component: {
name: 'a-textarea',
@ -126,6 +125,17 @@ export class WebhookDeployCert extends AbstractTaskPlugin {
})
skipSslVerify: boolean;
@TaskInput({
title: '成功判定',
helper: "返回结果中包含此字符串则表示部署成功不填则仅通过statusCode判定",
component: {
name: 'a-input',
placeholder: '例如: status:"success"',
},
})
successStr = '';
replaceTemplate(target: string, body: any, urlEncode = false) {
let bodyStr = target;
const keys = Object.keys(body);
@ -184,8 +194,9 @@ export class WebhookDeployCert extends AbstractTaskPlugin {
});
}
let res = null
try {
const res = await this.http.request({
res = await this.http.request({
url: url,
method: this.method,
headers: {
@ -194,21 +205,30 @@ export class WebhookDeployCert extends AbstractTaskPlugin {
},
data: data,
skipSslVerify: this.skipSslVerify,
responseType: "text",
returnOriginRes: true
});
return res
} catch (e) {
if (e.response?.data) {
throw new Error(e.message + ',' + JSON.stringify(e.response.data));
}
throw e;
}
if (this.successStr && !res?.data?.includes(this.successStr)) {
throw new Error(`请求失败,期望包含:${this.successStr},实际返回:${res.data}`);
}
return res
}
async onInstance() {
}
async onInstance() {}
async execute(): Promise<void> {
this.logger.info(`通过webhook部署开始`);
await this.send();
this.logger.info('部署成功');
}
}
new WebhookDeployCert();