mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-07 21:19:51 +00:00
Compare commits
2 Commits
1e67e9333e
...
b546cf3ad0
Author | SHA1 | Date | |
---|---|---|---|
![]() |
b546cf3ad0 | ||
![]() |
6353f0139b |
@ -5,7 +5,7 @@ import { Button, Card, Popover } from "antd";
|
||||
import SharedNode, { type SharedNodeProps } from "./_SharedNode";
|
||||
import AddNode from "./AddNode";
|
||||
import ConditionNodeConfigForm, { ConditionItem, ConditionNodeConfigFormFieldValues, ConditionNodeConfigFormInstance } from "./ConditionNodeConfigForm";
|
||||
import { Expr, WorkflowNodeConfigForCondition } from "@/domain/workflow";
|
||||
import { Expr, WorkflowNodeIoValueType } from "@/domain/workflow";
|
||||
import { produce } from "immer";
|
||||
import { useWorkflowStore } from "@/stores/workflow";
|
||||
import { useZustandShallowSelector } from "@/hooks";
|
||||
@ -29,7 +29,15 @@ const ConditionNode = ({ node, disabled, branchId, branchIndex }: ConditionNodeP
|
||||
const formToExpression = (values: ConditionNodeConfigFormFieldValues): Expr => {
|
||||
// 创建单个条件的表达式
|
||||
const createComparisonExpr = (condition: ConditionItem): Expr => {
|
||||
const left: Expr = { type: "var", selector: condition.leftSelector };
|
||||
const selectors = condition.leftSelector.split("#");
|
||||
const left: Expr = {
|
||||
type: "var",
|
||||
selector: {
|
||||
id: selectors[0],
|
||||
name: selectors[1],
|
||||
type: selectors[2] as WorkflowNodeIoValueType,
|
||||
},
|
||||
};
|
||||
const right: Expr = { type: "const", value: condition.rightValue || "" };
|
||||
|
||||
return {
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { forwardRef, memo, useEffect, useImperativeHandle, useState } from "react";
|
||||
import { Button, Card, Form, Input, Select, Space, Radio } from "antd";
|
||||
import { Button, Card, Form, Input, Select, Radio } from "antd";
|
||||
import { PlusOutlined, DeleteOutlined } from "@ant-design/icons";
|
||||
import i18n from "@/i18n";
|
||||
|
||||
import {
|
||||
WorkflowNodeConfigForCondition,
|
||||
@ -12,14 +13,16 @@ import {
|
||||
isVarExpr,
|
||||
WorkflowNode,
|
||||
workflowNodeIOOptions,
|
||||
WorkflowNodeIoValueType,
|
||||
} from "@/domain/workflow";
|
||||
import { FormInstance } from "antd";
|
||||
import { useZustandShallowSelector } from "@/hooks";
|
||||
import { useWorkflowStore } from "@/stores/workflow";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
// 表单内部使用的扁平结构 - 修改后只保留必要字段
|
||||
export interface ConditionItem {
|
||||
leftSelector: WorkflowNodeIOValueSelector;
|
||||
leftSelector: string;
|
||||
operator: ComparisonOperator;
|
||||
rightValue: string;
|
||||
}
|
||||
@ -50,7 +53,7 @@ const initFormModel = (): ConditionNodeConfigFormFieldValues => {
|
||||
return {
|
||||
conditions: [
|
||||
{
|
||||
leftSelector: undefined as unknown as WorkflowNodeIOValueSelector,
|
||||
leftSelector: "",
|
||||
operator: "==",
|
||||
rightValue: "",
|
||||
},
|
||||
@ -71,7 +74,7 @@ const expressionToForm = (expr?: Expr): ConditionNodeConfigFormFieldValues => {
|
||||
// 确保左侧是变量,右侧是常量
|
||||
if (isVarExpr(expr.left) && isConstExpr(expr.right)) {
|
||||
conditions.push({
|
||||
leftSelector: expr.left.selector,
|
||||
leftSelector: `${expr.left.selector.id}#${expr.left.selector.name}#${expr.left.selector.type}`,
|
||||
operator: expr.op,
|
||||
rightValue: String(expr.right.value),
|
||||
});
|
||||
@ -91,8 +94,43 @@ const expressionToForm = (expr?: Expr): ConditionNodeConfigFormFieldValues => {
|
||||
};
|
||||
};
|
||||
|
||||
// 根据变量类型获取适当的操作符选项
|
||||
const getOperatorsByType = (type: string): { value: ComparisonOperator; label: string }[] => {
|
||||
switch (type) {
|
||||
case "number":
|
||||
case "string":
|
||||
return [
|
||||
{ value: "==", label: i18n.t("workflow_node.condition.form.comparison.equal") },
|
||||
{ value: "!=", label: i18n.t("workflow_node.condition.form.comparison.not_equal") },
|
||||
{ value: ">", label: i18n.t("workflow_node.condition.form.comparison.greater_than") },
|
||||
{ value: ">=", label: i18n.t("workflow_node.condition.form.comparison.greater_than_or_equal") },
|
||||
{ value: "<", label: i18n.t("workflow_node.condition.form.comparison.less_than") },
|
||||
{ value: "<=", label: i18n.t("workflow_node.condition.form.comparison.less_than_or_equal") },
|
||||
];
|
||||
case "boolean":
|
||||
return [{ value: "is", label: i18n.t("workflow_node.condition.form.comparison.is") }];
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// 从选择器字符串中提取变量类型
|
||||
const getVariableTypeFromSelector = (selector: string): string => {
|
||||
if (!selector) return "string";
|
||||
|
||||
// 假设选择器格式为 "id#name#type"
|
||||
const parts = selector.split("#");
|
||||
if (parts.length >= 3) {
|
||||
return parts[2].toLowerCase() || "string";
|
||||
}
|
||||
return "string";
|
||||
};
|
||||
|
||||
const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, ConditionNodeConfigFormProps>(
|
||||
({ className, style, disabled, initialValues, onValuesChange, nodeId }, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const prefix = "workflow_node.condition.form";
|
||||
|
||||
const { getWorkflowOuptutBeforeId } = useWorkflowStore(useZustandShallowSelector(["updateNode", "getWorkflowOuptutBeforeId"]));
|
||||
|
||||
const [form] = Form.useForm<ConditionNodeConfigFormFieldValues>();
|
||||
@ -127,6 +165,12 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
// 表单值变更处理
|
||||
const handleFormChange = (_: undefined, values: ConditionNodeConfigFormFieldValues) => {
|
||||
setFormModel(values);
|
||||
|
||||
if (onValuesChange) {
|
||||
// 将表单值转换为表达式
|
||||
const expression = formToExpression(values);
|
||||
onValuesChange({ expression });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@ -141,33 +185,78 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
className="mb-3"
|
||||
extra={fields.length > 1 ? <Button icon={<DeleteOutlined />} danger type="text" onClick={() => remove(name)} /> : null}
|
||||
>
|
||||
{/* 将三个表单项放在一行 */}
|
||||
<div className="flex items-center gap-2">
|
||||
{/* 左侧变量选择器 */}
|
||||
<Form.Item {...restField} name={[name, "leftSelector"]} className="mb-0 flex-1" rules={[{ required: true, message: "请选择变量" }]}>
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "leftSelector"]}
|
||||
className="mb-0 flex-1"
|
||||
rules={[{ required: true, message: t(`${prefix}.variable.errmsg`) }]}
|
||||
>
|
||||
<Select
|
||||
placeholder="选择变量"
|
||||
placeholder={t(`${prefix}.variable.placeholder`)}
|
||||
options={previousNodes.map((item) => {
|
||||
return workflowNodeIOOptions(item);
|
||||
})}
|
||||
></Select>
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
{/* 操作符 */}
|
||||
<Form.Item {...restField} name={[name, "operator"]} className="mb-0 w-32" rules={[{ required: true, message: "请选择" }]}>
|
||||
<Select>
|
||||
<Select.Option value="==">等于 (==)</Select.Option>
|
||||
<Select.Option value="!=">不等于 (!=)</Select.Option>
|
||||
<Select.Option value=">">大于 (>)</Select.Option>
|
||||
<Select.Option value=">=">大于等于 (>=)</Select.Option>
|
||||
<Select.Option value="<">小于 (<)</Select.Option>
|
||||
<Select.Option value="<=">小于等于 (<=)</Select.Option>
|
||||
</Select>
|
||||
{/* 操作符 - 动态根据变量类型改变选项 */}
|
||||
<Form.Item
|
||||
noStyle
|
||||
shouldUpdate={(prevValues, currentValues) => {
|
||||
return prevValues.conditions?.[name]?.leftSelector !== currentValues.conditions?.[name]?.leftSelector;
|
||||
}}
|
||||
>
|
||||
{({ getFieldValue }) => {
|
||||
const leftSelector = getFieldValue(["conditions", name, "leftSelector"]);
|
||||
const varType = getVariableTypeFromSelector(leftSelector);
|
||||
const operators = getOperatorsByType(varType);
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "operator"]}
|
||||
className="mb-0 w-32"
|
||||
rules={[{ required: true, message: t(`${prefix}.operator.errmsg`) }]}
|
||||
>
|
||||
<Select options={operators} />
|
||||
</Form.Item>
|
||||
);
|
||||
}}
|
||||
</Form.Item>
|
||||
|
||||
{/* 右侧常量输入框 */}
|
||||
<Form.Item {...restField} name={[name, "rightValue"]} className="mb-0 flex-1" rules={[{ required: true, message: "请输入值" }]}>
|
||||
<Input placeholder="输入值" />
|
||||
{/* 右侧输入控件 - 根据变量类型使用不同的控件 */}
|
||||
<Form.Item
|
||||
noStyle
|
||||
shouldUpdate={(prevValues, currentValues) => {
|
||||
return prevValues.conditions?.[name]?.leftSelector !== currentValues.conditions?.[name]?.leftSelector;
|
||||
}}
|
||||
>
|
||||
{({ getFieldValue }) => {
|
||||
const leftSelector = getFieldValue(["conditions", name, "leftSelector"]);
|
||||
const varType = getVariableTypeFromSelector(leftSelector);
|
||||
|
||||
return (
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "rightValue"]}
|
||||
className="mb-0 flex-1"
|
||||
rules={[{ required: true, message: t(`${prefix}.value.errmsg`) }]}
|
||||
>
|
||||
{varType === "boolean" ? (
|
||||
<Select placeholder={t(`${prefix}.value.boolean.placeholder`)}>
|
||||
<Select.Option value="true">{t(`${prefix}.value.boolean.true`)}</Select.Option>
|
||||
<Select.Option value="false">{t(`${prefix}.value.boolean.false`)}</Select.Option>
|
||||
</Select>
|
||||
) : varType === "number" ? (
|
||||
<Input type="number" placeholder={t(`${prefix}.value.number.placeholder`)} />
|
||||
) : (
|
||||
<Input placeholder={t(`${prefix}.value.string.placeholder`)} />
|
||||
)}
|
||||
</Form.Item>
|
||||
);
|
||||
}}
|
||||
</Form.Item>
|
||||
</div>
|
||||
</Card>
|
||||
@ -179,7 +268,7 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
type="dashed"
|
||||
onClick={() =>
|
||||
add({
|
||||
leftSelector: undefined as unknown as WorkflowNodeIOValueSelector,
|
||||
leftSelector: "",
|
||||
operator: "==",
|
||||
rightValue: "",
|
||||
})
|
||||
@ -187,7 +276,7 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
block
|
||||
icon={<PlusOutlined />}
|
||||
>
|
||||
添加条件
|
||||
{t(`${prefix}.add_condition.button`)}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</>
|
||||
@ -195,10 +284,10 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
</Form.List>
|
||||
|
||||
{formModel.conditions && formModel.conditions.length > 1 && (
|
||||
<Form.Item name="logicalOperator" label="条件逻辑">
|
||||
<Form.Item name="logicalOperator" label={t(`${prefix}.logical_operator.label`)}>
|
||||
<Radio.Group buttonStyle="solid">
|
||||
<Radio.Button value="and">满足所有条件 (AND)</Radio.Button>
|
||||
<Radio.Button value="or">满足任一条件 (OR)</Radio.Button>
|
||||
<Radio.Button value="and">{t(`${prefix}.logical_operator.and`)}</Radio.Button>
|
||||
<Radio.Button value="or">{t(`${prefix}.logical_operator.or`)}</Radio.Button>
|
||||
</Radio.Group>
|
||||
</Form.Item>
|
||||
)}
|
||||
@ -207,5 +296,57 @@ const ConditionNodeConfigForm = forwardRef<ConditionNodeConfigFormInstance, Cond
|
||||
}
|
||||
);
|
||||
|
||||
// 表单值转换为表达式结构 (需要添加)
|
||||
const formToExpression = (values: ConditionNodeConfigFormFieldValues): Expr => {
|
||||
const createComparisonExpr = (condition: ConditionItem): Expr => {
|
||||
const [id, name, typeStr] = condition.leftSelector.split("#");
|
||||
|
||||
const type = typeStr as WorkflowNodeIoValueType;
|
||||
|
||||
const left: Expr = {
|
||||
type: "var",
|
||||
selector: { id, name, type },
|
||||
};
|
||||
|
||||
let rightValue: any = condition.rightValue;
|
||||
if (type === "number") {
|
||||
rightValue = Number(condition.rightValue);
|
||||
} else if (type === "boolean") {
|
||||
rightValue = condition.rightValue === "true";
|
||||
}
|
||||
|
||||
const right: Expr = {
|
||||
type: "const",
|
||||
value: rightValue,
|
||||
};
|
||||
|
||||
return {
|
||||
type: "compare",
|
||||
op: condition.operator,
|
||||
left,
|
||||
right,
|
||||
};
|
||||
};
|
||||
|
||||
// 如果只有一个条件,直接返回比较表达式
|
||||
if (values.conditions.length === 1) {
|
||||
return createComparisonExpr(values.conditions[0]);
|
||||
}
|
||||
|
||||
// 多个条件,通过逻辑运算符连接
|
||||
let expr: Expr = createComparisonExpr(values.conditions[0]);
|
||||
|
||||
for (let i = 1; i < values.conditions.length; i++) {
|
||||
expr = {
|
||||
type: "logical",
|
||||
op: values.logicalOperator,
|
||||
left: expr,
|
||||
right: createComparisonExpr(values.conditions[i]),
|
||||
};
|
||||
}
|
||||
|
||||
return expr;
|
||||
};
|
||||
|
||||
export default memo(ConditionNodeConfigForm);
|
||||
|
||||
|
@ -185,8 +185,11 @@ export type WorkflowNodeIO = {
|
||||
export type WorkflowNodeIOValueSelector = {
|
||||
id: string;
|
||||
name: string;
|
||||
type: WorkflowNodeIoValueType;
|
||||
};
|
||||
|
||||
export type WorkflowNodeIoValueType = "string" | "number" | "boolean";
|
||||
|
||||
type WorkflowNodeIOOptions = {
|
||||
label: string;
|
||||
value: string;
|
||||
@ -204,18 +207,18 @@ export const workflowNodeIOOptions = (node: WorkflowNode) => {
|
||||
case "certificate":
|
||||
rs.options.push({
|
||||
label: `${node.name} - ${output.label} - 是否有效`,
|
||||
value: `${node.id}#${output.name}.validated`,
|
||||
value: `${node.id}#${output.name}.validated#boolean`,
|
||||
});
|
||||
|
||||
rs.options.push({
|
||||
label: `${node.name} - ${output.label} - 剩余天数`,
|
||||
value: `${node.id}#${output.name}.daysLeft`,
|
||||
value: `${node.id}#${output.name}.daysLeft#number`,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
rs.options.push({
|
||||
label: `${node.name} - ${output.label}`,
|
||||
value: `${node.id}#${output.name}`,
|
||||
value: `${node.id}#${output.name}#${output.type}`,
|
||||
});
|
||||
break;
|
||||
}
|
||||
@ -231,7 +234,7 @@ export const workflowNodeIOOptions = (node: WorkflowNode) => {
|
||||
|
||||
type Value = string | number | boolean;
|
||||
|
||||
export type ComparisonOperator = ">" | "<" | ">=" | "<=" | "==" | "!=";
|
||||
export type ComparisonOperator = ">" | "<" | ">=" | "<=" | "==" | "!=" | "is";
|
||||
|
||||
export type LogicalOperator = "and" | "or" | "not";
|
||||
|
||||
|
@ -790,6 +790,26 @@
|
||||
"workflow_node.branch.label": "Parallel branch",
|
||||
|
||||
"workflow_node.condition.label": "Branch",
|
||||
"workflow_node.condition.form.variable.placeholder": "Please select variable",
|
||||
"workflow_node.condition.form.variable.errmsg": "Please select variable",
|
||||
"workflow_node.condition.form.operator.errmsg": "Please select operator",
|
||||
"workflow_node.condition.form.value.errmsg": "Please enter value",
|
||||
"workflow_node.condition.form.value.string.placeholder": "Please enter value",
|
||||
"workflow_node.condition.form.value.number.placeholder": "Please enter value",
|
||||
"workflow_node.condition.form.value.boolean.placeholder": "Please select value",
|
||||
"workflow_node.condition.form.value.boolean.true": "True",
|
||||
"workflow_node.condition.form.value.boolean.false": "False",
|
||||
"workflow_node.condition.form.add_condition.button": "Add condition",
|
||||
"workflow_node.condition.form.logical_operator.label": "Logical operator",
|
||||
"workflow_node.condition.form.logical_operator.and": "Meet all conditions (AND)",
|
||||
"workflow_node.condition.form.logical_operator.or": "Meet any condition (OR)",
|
||||
"workflow_node.condition.form.comparison.equal": "Equal",
|
||||
"workflow_node.condition.form.comparison.not_equal": "Not equal",
|
||||
"workflow_node.condition.form.comparison.greater_than": "Greater than",
|
||||
"workflow_node.condition.form.comparison.greater_than_or_equal": "Greater than or equal",
|
||||
"workflow_node.condition.form.comparison.less_than": "Less than",
|
||||
"workflow_node.condition.form.comparison.less_than_or_equal": "Less than or equal",
|
||||
"workflow_node.condition.form.comparison.is": "Is",
|
||||
|
||||
"workflow_node.execute_result_branch.label": "Execution result branch",
|
||||
|
||||
@ -797,3 +817,4 @@
|
||||
|
||||
"workflow_node.execute_failure.label": "If the previous node failed ..."
|
||||
}
|
||||
|
||||
|
@ -789,6 +789,26 @@
|
||||
"workflow_node.branch.label": "并行分支",
|
||||
|
||||
"workflow_node.condition.label": "分支",
|
||||
"workflow_node.condition.form.variable.placeholder": "选择变量",
|
||||
"workflow_node.condition.form.variable.errmsg": "请选择变量",
|
||||
"workflow_node.condition.form.operator.errmsg": "请选择操作符",
|
||||
"workflow_node.condition.form.value.errmsg": "请输入值",
|
||||
"workflow_node.condition.form.value.string.placeholder": "输入值",
|
||||
"workflow_node.condition.form.value.number.placeholder": "输入数值",
|
||||
"workflow_node.condition.form.value.boolean.placeholder": "选择值",
|
||||
"workflow_node.condition.form.value.boolean.true": "是",
|
||||
"workflow_node.condition.form.value.boolean.false": "否",
|
||||
"workflow_node.condition.form.add_condition.button": "添加条件",
|
||||
"workflow_node.condition.form.logical_operator.label": "条件逻辑",
|
||||
"workflow_node.condition.form.logical_operator.and": "满足所有条件 (AND)",
|
||||
"workflow_node.condition.form.logical_operator.or": "满足任一条件 (OR)",
|
||||
"workflow_node.condition.form.comparison.equal": "等于",
|
||||
"workflow_node.condition.form.comparison.not_equal": "不等于",
|
||||
"workflow_node.condition.form.comparison.greater_than": "大于",
|
||||
"workflow_node.condition.form.comparison.greater_than_or_equal": "大于等于",
|
||||
"workflow_node.condition.form.comparison.less_than": "小于",
|
||||
"workflow_node.condition.form.comparison.less_than_or_equal": "小于等于",
|
||||
"workflow_node.condition.form.comparison.is": "为",
|
||||
|
||||
"workflow_node.execute_result_branch.label": "执行结果分支",
|
||||
|
||||
@ -796,3 +816,4 @@
|
||||
|
||||
"workflow_node.execute_failure.label": "若前序节点执行失败…"
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user