perf: 阿里云证书订单支持获取2.0的订单

This commit is contained in:
xiaojunnuo
2026-04-28 11:51:54 +08:00
parent 2f1ad7201f
commit 64b3184b28
2 changed files with 235 additions and 94 deletions
@@ -9,7 +9,7 @@ import dayjs from "dayjs";
icon: "ph:certificate",
title: "获取阿里云订阅证书",
group: pluginGroups.cert.key,
desc: "从阿里云拉取订阅模式的商用证书",
desc: "从阿里云拉取订阅模式的商用证书(支持 API 1.0 和 2.0",
default: {
strategy: {
runStrategy: RunStrategy.AlwaysRun,
@@ -18,8 +18,8 @@ import dayjs from "dayjs";
})
export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
@TaskInput({
title: "Access授权",
helper: "阿里云授权AccessKeyId、AccessKeySecret",
title: "Access 授权",
helper: "阿里云授权 AccessKeyId、AccessKeySecret",
component: {
name: "access-selector",
type: "aliyun",
@@ -28,34 +28,34 @@ export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
})
accessId!: string;
@TaskInput(
@TaskInput(
{
title:"订单类型",
value:"CPACK",
component:{
name:"a-select",
vModel:"value",
options:[
title: "证书API 版本",
value: "v1",
component: {
name: "a-select",
vModel: "value",
options: [
{
label:"资源虚拟订单(一般选这个)",
value:"CPACK",
label: "API 1.0 (旧版)",
value: "v1",
},
{
label:"售卖订单",
value:"BUY",
}
]
}
label: "API 2.0 (新版)",
value: "v2",
},
],
},
helper: "选择阿里云证书 API 版本",
}
)
orderType!: string;
apiVersion!: string;
@TaskInput(
createRemoteSelectInputDefine({
title: "证书订单ID",
helper: "订阅模式的证书订单Id",
title: "证书订单 ID",
helper: "订阅模式的证书订单 Id",
typeName: "CertApplyGetFormAliyun",
pageSize: 50,
component: {
@@ -68,40 +68,53 @@ export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
)
orderId!: string;
async onInit(): Promise<void> {}
async onInit(): Promise<void> { }
async doCertApply(): Promise<CertReader> {
const access = await this.getAccess<AliyunAccess>(this.accessId);
const client = await access.getClient("cas.aliyuncs.com");
this.logger.info(`开始获取证书,orderId:${this.orderId}`);
if (this.apiVersion === "v2") {
return this.doCertApplyV2(client);
} else {
return this.doCertApplyV1(client);
}
}
async doCertApplyV1(client: any): Promise<CertReader> {
this.logger.info(`开始获取证书 (API 1.0),orderId:${this.orderId}`);
let orderId: any = this.orderId;
if (!orderId) {
throw new Error("请先输入证书订单ID");
throw new Error("请先输入证书订单 ID");
}
if (typeof orderId !== "string") {
orderId = parseInt(orderId);
}
const certState = await this.getCertificateState(client, orderId);
this.logger.info(`获取到证书Id:${JSON.stringify(certState.CertId)}`);
this.logger.info(`获取到证书 Id:${JSON.stringify(certState.CertId)}`);
const certDetail = await this.getCertDetail(client, certState.CertId);
this.logger.info(`获取到证书:${certDetail.getAllDomains()}, 过期时间:${dayjs(certDetail.expires).format("YYYY-MM-DD HH:mm:ss")}`);
return certDetail;
}
async doCertApplyV2(client: any): Promise<CertReader> {
this.logger.info(`开始获取证书 (API 2.0),instanceId:${this.orderId}`);
if (!this.orderId) {
throw new Error("请先输入证书实例 ID");
}
const certDetail = await this.getCertDetailV2(client, this.orderId);
this.logger.info(`获取到证书:${certDetail.getAllDomains()}, 过期时间:${dayjs(certDetail.expires).format("YYYY-MM-DD HH:mm:ss")}`);
return certDetail;
}
async getCertDetail(client: any, certId: any) {
const res = await client.doRequest({
// 接口名称
// 接口名称
action: "GetUserCertificateDetail",
// 接口版本
version: "2020-04-07",
// 接口协议
protocol: "HTTPS",
// 接口 HTTP 方法
method: "POST",
authType: "AK",
style: "RPC",
// 接口 PATH
pathname: `/`,
data: {
query: {
@@ -120,19 +133,40 @@ export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
});
}
async getCertificateState(client: any, orderId: any): Promise<{ CertId: string; Type: string; Domain: string }> {
async getCertDetailV2(client: any, instanceId: string) {
const res = await client.doRequest({
// 接口名称
action: "DescribeCertificateState",
// 接口版本
action: "GetUserCertificateDetail",
version: "2020-04-07",
protocol: "HTTPS",
method: "POST",
authType: "AK",
style: "RPC",
pathname: `/`,
data: {
query: {
CertId: instanceId,
},
},
});
const crt = res.Cert;
const key = res.Key;
return new CertReader({
crt,
key,
csr: "",
});
}
async getCertificateState(client: any, orderId: any): Promise<{ CertId: string; Type: string; Domain: string }> {
const res = await client.doRequest({
action: "DescribeCertificateState",
version: "2020-04-07",
// 接口协议
protocol: "HTTPS",
// 接口 HTTP 方法
method: "POST",
authType: "AK",
style: "RPC",
// 接口 PATH
pathname: `/`,
data: {
query: {
@@ -146,35 +180,41 @@ export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
async onGetOrderList(req: PageSearch) {
if (!this.accessId) {
throw new Error("请先选择Access授权");
throw new Error("请先选择 Access 授权");
}
const access = await this.getAccess<AliyunAccess>(this.accessId);
const client = await access.getClient("cas.aliyuncs.com");
const pager = new Pager(req)
const pager = new Pager(req);
if (this.apiVersion === "v2") {
return this.onGetOrderListV2(client, pager);
} else {
return this.onGetOrderListV1(client, pager);
}
}
async onGetOrderListV1(client: any, pager: Pager) {
const res = await client.doRequest({
// 接口名称
action: "ListUserCertificateOrder",
// 接口版本
version: "2020-04-07",
method: "POST",
authType: "AK",
style: "RPC",
// 接口 PATH
pathname: `/`,
data: {
query: {
OrderType: this.orderType,
OrderType: "CPACK",
Status: "ISSUED",
CurrentPage: pager.pageNo,
ShowSize : pager.pageSize,
ShowSize: pager.pageSize,
},
},
});
const list = res?.CertificateOrderList || [];
if (!list || list.length === 0) {
return []
return [];
}
const total = res.TotalCount || 0;
@@ -195,9 +235,49 @@ export class CertApplyGetFormAliyunPlugin extends CertApplyBasePlugin {
};
});
return {
list:records,
list: records,
total,
};
}
async onGetOrderListV2(client: any, pager: Pager) {
const res = await client.doRequest({
action: "ListInstances",
version: "2020-04-07",
method: "POST",
authType: "AK",
style: "RPC",
pathname: `/`,
data: {
query: {
Status: "normal",
CurrentPage: pager.pageNo,
ShowSize: pager.pageSize,
},
},
});
const list = res?.InstanceList || [];
if (!list || list.length === 0) {
return [];
}
const total = res.TotalCount || 0;
const records = list.map((item: any) => {
const value = item.InstanceId;
const domain = item.Domain;
const label = `${item.Domain}<${item.CertificateName}>`;
return {
label: label,
value: value,
Domain: domain,
};
});
return {
list: records,
total,
};
}
}