Files
certd/packages/ui/certd-server/src/plugins/plugin-rainyun/access.ts
T

149 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-06-10 18:41:25 +08:00
import {AccessInput, BaseAccess, IsAccess} from "@certd/pipeline";
import {HttpRequestConfig} from "@certd/basic";
import { CertInfo } from "@certd/plugin-cert";
2025-06-10 18:41:25 +08:00
/**
*/
@IsAccess({
name: "rainyun",
title: "雨云授权",
desc: "https://app.rainyun.com/",
icon: "svg:icon-lucky"
})
export class RainyunAccess extends BaseAccess {
@AccessInput({
title: "ApiKey",
component: {
placeholder: "api-key",
component: {
name: "a-input",
vModel: "value"
}
},
2025-06-11 22:40:21 +08:00
helper:"https://app.rainyun.com/account/settings/api-key",
2025-06-10 18:41:25 +08:00
encrypt: true,
required: true
})
apiKey!: string;
@AccessInput({
title: "测试",
component: {
name: "api-test",
action: "TestRequest"
},
helper: "点击测试接口是否正常"
})
testRequest = true;
async onTestRequest() {
await this.getDomainList({limit:1});
2025-06-10 18:41:25 +08:00
return "ok"
}
// {"columnFilters":{"domains.Domain":"domain"},"sort":[],"page":1,"perPage":20}
async getDomainList(req:{offset?:number,limit?:number,query?:string}){
const size = req.limit ?? 20;
2025-06-10 18:41:25 +08:00
const offset = req.offset ?? 0;
2025-06-11 22:40:21 +08:00
let page = Math.floor(offset / size);
if(offset % size === 0 ){
page++
}
2025-06-10 18:41:25 +08:00
const options ={
page: page,
perPage: size,
columnFilters: {
"domains.Domain": req.query??""
},
}
2025-06-12 22:41:08 +08:00
const res = await this.doRequest({
2025-06-11 22:40:21 +08:00
url: `/product/domain/?options=${encodeURIComponent(JSON.stringify(options))}`,
2025-06-10 18:41:25 +08:00
method: "GET",
});
2025-06-12 22:41:08 +08:00
return {
total: res.TotalRecords,
list: res.Records || [],
limit: size,
offset: offset
2025-06-12 22:41:08 +08:00
}
}
2025-06-29 19:59:13 +08:00
async getCertList(req:{pageNo?:number,pageSize?:number,query?:string}){
const pageNo = req.pageNo ?? 1;
const pageSize = req.pageSize ?? 20;
const options ={
columnFilters: {
Domain: req.query??""
},
sort:[],
2025-06-29 19:59:13 +08:00
page: pageNo,
perPage: pageSize,
}
const res = await this.doRequest({
url: `product/sslcenter/?options=${encodeURIComponent(JSON.stringify(options))}`,
method: "GET",
});
return {
total: res.TotalRecords,
list: res.Records || [],
2025-06-29 19:59:13 +08:00
pageNo: pageNo,
pageSize: pageSize
}
}
async doCertReplace(req:{certId:number,cert:CertInfo}){
// /product/sslcenter/{id}
return await this.doRequest({
url: `product/sslcenter/${req.certId}`,
method: "PUT",
data: {
cert: req.cert.crt,
key: req.cert.key,
}
});
}
2025-06-12 22:41:08 +08:00
async getDomainId(domain:string){
const res = await this.getDomainList({query: domain,limit:1});
2025-06-12 22:41:08 +08:00
if (res.list.length === 0) {
throw new Error(`域名${domain}不存在` );
2025-06-12 22:41:08 +08:00
}
return res.list[0].id;
2025-06-10 18:41:25 +08:00
}
async doRequest(req:HttpRequestConfig){
const res = await this.ctx.http.request({
url: req.url,
baseURL:"https://api.v2.rainyun.com",
method: req.method|| "POST",
data: req.data,
params: req.params,
headers:{
2025-06-11 22:40:21 +08:00
"X-Api-Key": this.apiKey
2025-06-10 18:41:25 +08:00
},
// httpProxy: this.httpProxy||undefined,
});
if (res.code === 200) {
return res.data;
}
throw new Error(res.message || res);
}
}
new RainyunAccess();