chore: 优化证书参数模版功能

This commit is contained in:
xiaojunnuo
2026-06-03 00:29:21 +08:00
parent 91d5c90eb0
commit 3db87218ee
8 changed files with 122 additions and 70 deletions
@@ -63,7 +63,7 @@ export abstract class BaseController {
async getProjectId(permission:string) {
if (!isEnterprise()) {
return null
return undefined
}
let projectIdStr = this.ctx.headers["project-id"] as string;
if (!projectIdStr){
@@ -29,6 +29,9 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
const delRequest = async ({ row }: DelReq) => {
return await api.DelObj(row.id);
};
const infoRequest = async ({ row }: DelReq) => {
return await api.GetObj(row.id);
};
async function setDefault(row: any) {
await api.SetDefault(row.id);
@@ -36,9 +39,10 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
await crudExpose.doRefresh();
}
async function openForm(row?: any) {
async function openForm(entity?: any) {
const certPlugin: any = await pluginStore.getPluginDefine("CertApply");
const columns = buildCertApplyTemplateColumns(certPlugin);
const row = await infoRequest({ row: entity });
const content = row?.content ? (typeof row.content === "string" ? JSON.parse(row.content || "{}") : row.content) : {};
const initialForm = row
? {
@@ -85,6 +89,7 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
addRequest,
editRequest,
delRequest,
infoRequest,
},
search: {
initialForm: {
@@ -127,15 +132,22 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
type: "dict-switch",
dict: isDefaultDict,
column: {
width: 150,
width: 170,
cellRender({ value, row }) {
return (
<div class="flex items-center gap-2">
<fs-values-format modelValue={value} dict={isDefaultDict}></fs-values-format>
{!row.isDefault && (
<a-tooltip title="设为默认">
<fs-icon class="pointer color-primary" icon="ion:star-outline" onClick={() => setDefault(row)}></fs-icon>
</a-tooltip>
<a-button
size="small"
type="link"
onClick={(event: MouseEvent) => {
event.stopPropagation();
setDefault(row);
}}
>
</a-button>
)}
</div>
);
@@ -145,7 +157,8 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
disabled: {
title: "禁用",
type: "dict-switch",
column: { width: 100 },
column: { show: false },
form: { show: false },
},
createTime: {
title: "创建时间",
@@ -66,18 +66,6 @@ export function buildCertApplyTemplateColumns(certPlugin: any) {
order: 900,
},
},
disabled: {
title: "禁用",
type: "switch",
form: {
value: false,
component: {
name: "a-switch",
vModel: "checked",
},
order: 901,
},
},
});
return columns;
@@ -89,6 +77,6 @@ export function buildTemplateSubmitData(form: any) {
name: form.name,
content: pickCertApplyTemplateParams(omit(form, ["id", "name", "content", "isDefault", "disabled"])),
isDefault: form.isDefault,
disabled: form.disabled,
disabled: false,
};
}
@@ -111,7 +111,7 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
const router = useRouter();
const { openCrudFormDialog: openInnerCrudFormDialog } = useFormWrapper();
function createCrudOptions(req: { certPlugin: any; doSubmit: any; title?: string; initialForm?: any }): CreateCrudOptionsRet {
async function createCrudOptions(req: { certPlugin: any; doSubmit: any; title?: string; initialForm?: any }): Promise<CreateCrudOptionsRet> {
const inputs: any = {};
const moreParams = [];
const doSubmit = req.doSubmit;
@@ -153,19 +153,8 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
const initialForm = req.initialForm || {};
initialForm.type = certPlugin.name;
const templateDict = dict({
value: "id",
label: "name",
async getData() {
return await certApplyTemplateApi.ListAll();
},
async getNodesByValues(ids: any[]) {
const list = await certApplyTemplateApi.ListAll();
return list.filter((item: any) => ids.includes(item.id));
},
immediate: false,
});
const applyTemplates = reactive<any[]>([]);
const selectedTemplateId = ref<number | null>(null);
async function reloadApplyTemplates() {
const list = await certApplyTemplateApi.ListAll();
@@ -177,6 +166,7 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
if (!templateId) {
return;
}
selectedTemplateId.value = templateId;
const template = await certApplyTemplateApi.GetObj(templateId);
const params = pickCertApplyTemplateParams(typeof template.content === "string" ? JSON.parse(template.content || "{}") : template.content);
form.input = {
@@ -185,11 +175,25 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
};
}
function getSelectedApplyTemplateName(form: any) {
if (!form?.applyTemplateId) {
async function applyDefaultTemplateToInitialForm() {
if (certPlugin.name !== "CertApply") {
return;
}
const list = await reloadApplyTemplates();
const defaultTemplate = list.find((item: any) => item.isDefault);
if (!defaultTemplate) {
return;
}
await applyTemplateToForm(defaultTemplate.id, initialForm);
}
await applyDefaultTemplateToInitialForm();
function getSelectedApplyTemplateName() {
if (!selectedTemplateId.value) {
return "选择模版";
}
const template = applyTemplates.find(item => item.id === form.applyTemplateId);
const template = applyTemplates.find(item => item.id === selectedTemplateId.value);
return template?.name || "选择模版";
}
@@ -233,7 +237,6 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
content: pickCertApplyTemplateParams(form.input),
});
await reloadApplyTemplates();
await templateDict.reloadDict();
message.success("保存成功");
},
},
@@ -268,7 +271,6 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
async doSubmit({ form: templateForm }: any) {
await certApplyTemplateApi.UpdateObj(buildTemplateSubmitData(templateForm));
await reloadApplyTemplates();
await templateDict.reloadDict();
message.success("保存成功");
},
},
@@ -283,7 +285,9 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
async onOk() {
await certApplyTemplateApi.DelObj(templateId);
await reloadApplyTemplates();
await templateDict.reloadDict();
if (selectedTemplateId.value === templateId) {
selectedTemplateId.value = null;
}
message.success("删除成功");
},
});
@@ -329,7 +333,6 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
return;
}
const templateId = Number(key);
form.applyTemplateId = templateId;
applyTemplateToForm(templateId, form);
}}
>
@@ -374,7 +377,7 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
>
<a-tooltip title="选择参数模版,自动填充证书申请参数">
<a-button>
<span class="inline-block max-w-48 truncate align-bottom">{getSelectedApplyTemplateName(form)}</span>
<span class="inline-block max-w-48 truncate align-bottom">{getSelectedApplyTemplateName()}</span>
<fs-icon icon="ion:chevron-down" class="ml-1" />
</a-button>
</a-tooltip>
@@ -559,16 +562,6 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
initialForm.input[key] = pluginSysConfig.sysSetting?.input[key];
}
}
const defaultTemplate = req.pluginName === "CertApply" ? await certApplyTemplateApi.GetDefault() : null;
if (defaultTemplate) {
initialForm.applyTemplateId = defaultTemplate.id;
const templateParams = pickCertApplyTemplateParams(typeof defaultTemplate.content === "string" ? JSON.parse(defaultTemplate.content || "{}") : defaultTemplate.content);
initialForm.input = {
...initialForm.input,
...templateParams,
};
}
async function doSubmit({ form }: any) {
// const certDetail = readCertDetail(form.cert.crt);
// 添加certd pipeline
@@ -638,7 +631,7 @@ export function useCertPipelineCreator({ formWrapperRef }: { formWrapperRef: Ref
}
req.currentPluginRef.value = certPlugin;
const { crudOptions } = createCrudOptions({
const { crudOptions } = await createCrudOptions({
certPlugin,
doSubmit,
title: req.title,
@@ -135,6 +135,5 @@ export class MainConfiguration {
logger.info("当前环境:", this.app.getEnv()); // prod
}
}
@@ -14,13 +14,26 @@ export class CertApplyTemplateController extends CrudController<CertApplyTemplat
return this.service;
}
private removeContent(data: any) {
const records = Array.isArray(data) ? data : data?.records;
if (!records) {
return data;
}
for (const item of records) {
delete item.content;
}
return data;
}
@Post("/page", { description: Constants.per.authOnly, summary: "查询证书申请参数模版分页列表" })
async page(@Body(ALL) body: any) {
const { projectId, userId } = await this.getProjectUserIdRead();
body.query = body.query ?? {};
body.query.projectId = projectId;
body.query.userId = userId;
return super.page(body);
const res = await super.page(body);
this.removeContent(res.data);
return res;
}
@Post("/list", { description: Constants.per.authOnly, summary: "查询证书申请参数模版列表" })
@@ -30,7 +43,9 @@ export class CertApplyTemplateController extends CrudController<CertApplyTemplat
body.query.projectId = projectId;
body.query.userId = userId;
body.query.disabled = false;
return super.list(body);
const res = await super.list(body);
this.removeContent(res.data);
return res;
}
@Post("/add", { description: Constants.per.authOnly, summary: "添加证书申请参数模版" })
@@ -3,23 +3,31 @@ import { CertApplyTemplateService } from "./cert-apply-template-service.js";
function createService(list: any[]) {
const service = new CertApplyTemplateService();
function matchesWhere(item: any, where: any) {
for (const key of Object.keys(where)) {
const expected = where[key];
if (expected?._type === "isNull") {
if (item[key] != null) {
return false;
}
continue;
}
if (expected != null && item[key] !== expected) {
return false;
}
}
return true;
}
(service as any).repository = {
async findOne({ where }: any) {
return list.find(item => {
if (where.id != null && item.id !== where.id) {
return false;
return list.find(item => matchesWhere(item, where));
},
async update(where: any, patch: any) {
for (const item of list) {
if (matchesWhere(item, where)) {
Object.assign(item, patch);
}
if (where.userId != null && item.userId !== where.userId) {
return false;
}
if (where.projectId != null && item.projectId !== where.projectId) {
return false;
}
if (where.isDefault != null && item.isDefault !== where.isDefault) {
return false;
}
return true;
});
}
},
};
return service;
@@ -149,4 +157,40 @@ describe("CertApplyTemplateService", () => {
privateKeyType: "rsa_4096",
});
});
it("sets default for templates with null project id", async () => {
const list = [
{
id: 1,
userId: 10,
projectId: null,
isDefault: true,
disabled: false,
content: "{}",
},
{
id: 2,
userId: 10,
projectId: null,
isDefault: false,
disabled: false,
content: "{}",
},
{
id: 3,
userId: 10,
projectId: 20,
isDefault: true,
disabled: false,
content: "{}",
},
];
const service = createService(list);
await service.setDefault(2, 10, null);
assert.equal(list[0].isDefault, false);
assert.equal(list[1].isDefault, true);
assert.equal(list[2].isDefault, true);
});
});
@@ -1,7 +1,7 @@
import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { BaseService, ValidateException } from "@certd/lib-server";
import { Repository } from "typeorm";
import { IsNull, Repository } from "typeorm";
import { CertApplyTemplateEntity } from "../entity/cert-apply-template.js";
import { CertApplyTemplateParams, pickCertApplyCustomParams, pickCertApplyTemplateParams } from "./cert-apply-template-fields.js";