From 3db87218ee84731c08e856bf94640b708718b865 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 3 Jun 2026 00:29:21 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E8=AF=81=E4=B9=A6?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A8=A1=E7=89=88=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib-server/src/basic/base-controller.ts | 2 +- .../views/certd/cert/apply-template/crud.tsx | 25 +++++-- .../views/certd/cert/apply-template/fields.ts | 14 +--- .../views/certd/pipeline/certd-form/use.tsx | 57 +++++++-------- packages/ui/certd-server/src/configuration.ts | 1 - .../cert/cert-apply-template-controller.ts | 19 ++++- .../cert-apply-template-service.test.ts | 72 +++++++++++++++---- .../service/cert-apply-template-service.ts | 2 +- 8 files changed, 122 insertions(+), 70 deletions(-) diff --git a/packages/libs/lib-server/src/basic/base-controller.ts b/packages/libs/lib-server/src/basic/base-controller.ts index e1fa09220..0de3dff1f 100644 --- a/packages/libs/lib-server/src/basic/base-controller.ts +++ b/packages/libs/lib-server/src/basic/base-controller.ts @@ -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){ diff --git a/packages/ui/certd-client/src/views/certd/cert/apply-template/crud.tsx b/packages/ui/certd-client/src/views/certd/cert/apply-template/crud.tsx index 02296e17a..827a15f45 100644 --- a/packages/ui/certd-client/src/views/certd/cert/apply-template/crud.tsx +++ b/packages/ui/certd-client/src/views/certd/cert/apply-template/crud.tsx @@ -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 (
{!row.isDefault && ( - - setDefault(row)}> - + { + event.stopPropagation(); + setDefault(row); + }} + > + 设为默认 + )}
); @@ -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: "创建时间", diff --git a/packages/ui/certd-client/src/views/certd/cert/apply-template/fields.ts b/packages/ui/certd-client/src/views/certd/cert/apply-template/fields.ts index 0ebdec44c..3df634e97 100644 --- a/packages/ui/certd-client/src/views/certd/cert/apply-template/fields.ts +++ b/packages/ui/certd-client/src/views/certd/cert/apply-template/fields.ts @@ -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, }; } diff --git a/packages/ui/certd-client/src/views/certd/pipeline/certd-form/use.tsx b/packages/ui/certd-client/src/views/certd/pipeline/certd-form/use.tsx index a16bb14e6..f532c1534 100644 --- a/packages/ui/certd-client/src/views/certd/pipeline/certd-form/use.tsx +++ b/packages/ui/certd-client/src/views/certd/pipeline/certd-form/use.tsx @@ -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 { 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([]); + const selectedTemplateId = ref(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 > - {getSelectedApplyTemplateName(form)} + {getSelectedApplyTemplateName()} @@ -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, diff --git a/packages/ui/certd-server/src/configuration.ts b/packages/ui/certd-server/src/configuration.ts index 55bb01d0e..5ce3fba47 100644 --- a/packages/ui/certd-server/src/configuration.ts +++ b/packages/ui/certd-server/src/configuration.ts @@ -135,6 +135,5 @@ export class MainConfiguration { logger.info("当前环境:", this.app.getEnv()); // prod - } } diff --git a/packages/ui/certd-server/src/controller/user/cert/cert-apply-template-controller.ts b/packages/ui/certd-server/src/controller/user/cert/cert-apply-template-controller.ts index 8d17a3b0a..0b3fd53e8 100644 --- a/packages/ui/certd-server/src/controller/user/cert/cert-apply-template-controller.ts +++ b/packages/ui/certd-server/src/controller/user/cert/cert-apply-template-controller.ts @@ -14,13 +14,26 @@ export class CertApplyTemplateController extends CrudController { - 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); + }); }); diff --git a/packages/ui/certd-server/src/modules/cert/service/cert-apply-template-service.ts b/packages/ui/certd-server/src/modules/cert/service/cert-apply-template-service.ts index 9e3ffe7c6..b533ff2dd 100644 --- a/packages/ui/certd-server/src/modules/cert/service/cert-apply-template-service.ts +++ b/packages/ui/certd-server/src/modules/cert/service/cert-apply-template-service.ts @@ -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";