CMVR-IOT-UI/src/views/flow/nodes/common/serviceNode.vue

602 lines
16 KiB
Vue
Raw Normal View History

2025-09-19 14:16:28 +08:00
<template>
2025-11-17 14:54:32 +08:00
<div class="node__container" :class="props.model.id">
2025-09-19 14:16:28 +08:00
<NodeState :state="nodeOperatingStatus">
<template #input>
<JsonViewer :value="inputJsonData" copyable boxed sort theme="light" />
</template>
<template #output>
2025-11-17 14:54:32 +08:00
<JsonViewer
v-if="props.properties.outputType === 'json'"
:value="outputJsonData"
copyable
boxed
sort
theme="light"
/>
2025-09-19 14:16:28 +08:00
<el-image
2025-11-17 14:54:32 +08:00
v-if="props.properties.outputType === 'img'"
v-for="item in outputJsonData.imageUrl"
style="width: 60px; height: 60px"
:src="item"
:preview-src-list="outputJsonData.imageUrl"
:preview-teleported="true"
show-progress
fit="fill"
/>
<IPlayer
v-if="props.properties.outputType === 'video'"
v-for="item in outputJsonData.videoUrl"
:videoUrl="item"
2025-09-19 14:16:28 +08:00
/>
</template>
</NodeState>
<NodeTitle
:icon="props.properties.icon"
:nodeId="props.model.id"
:nodeProperties="props.properties"
2025-11-17 17:32:33 +08:00
:nodeType="props.properties.nodeType || 'NONE'"
2025-09-19 14:16:28 +08:00
:nodeName="props.properties.name"
:nodeDesc="props.properties.desc"
2025-11-17 14:54:32 +08:00
:zoom-state="nodeZoom"
@zoom="zoom"
2025-09-19 14:16:28 +08:00
/>
2025-11-17 14:54:32 +08:00
<div class="input__container" v-show="nodeZoom">
2025-09-19 14:16:28 +08:00
<div class="title">
<div class="left">
<div class="tag"></div>
<div class="text">输入</div>
</div>
<div class="right" v-if="properties.properties?.canAddFormItem">
<el-button
:disabled="flowStore.disableForm"
@click="addFormItem"
class="addFormItem"
>
<el-icon :size="20"><Plus /></el-icon>
</el-button>
</div>
</div>
<div class="form__container">
<el-form
:inline="true"
:model="formData"
:rules="rules"
ref="dynamicForm"
label-position="top"
label-width="auto"
:disabled="flowStore.disableForm"
>
<div v-for="(property, index) in formData.nodeParams" :key="index">
<el-row>
<el-form-item
:label="index === 0 ? '参数名' : ''"
:prop="`nodeParams.${index}.name`"
2025-11-17 14:54:32 +08:00
:rules="[
{ required: true, message: '请输入参数名', trigger: 'blur' },
]"
2025-09-19 14:16:28 +08:00
>
<el-input
:disabled="property?.disabled || false"
v-model="property.name"
2025-11-13 21:55:20 +08:00
@keydown="handleInputKeydown"
2025-09-19 14:16:28 +08:00
placeholder="请输入"
clearable
/>
</el-form-item>
<el-form-item
:label="index === 0 ? '参数值' : ''"
:prop="`nodeParams.${index}.type`"
>
2025-11-17 14:54:32 +08:00
<el-select
v-model="property.type"
@change="handleTypeChange(index)"
>
2025-09-19 14:16:28 +08:00
<el-option label="引用" value="quote" />
<el-option label="输入" value="input" />
</el-select>
<el-form-item
v-if="property.type === 'input'"
2025-11-17 14:54:32 +08:00
:rules="[
{
required: true,
message: '请输入参数值',
trigger: 'blur',
},
]"
2025-09-19 14:16:28 +08:00
:prop="`nodeParams.${index}.input`"
>
2025-11-17 14:54:32 +08:00
<el-input-number
v-if="property.componentType === 'number'"
v-model="property.input"
:min="0"
:controls="false"
:step-strictly="true"
placeholder="请输入"
clearable
/>
<el-select
v-model="property.input"
v-else-if="property.componentType === 'select'"
>
<el-option
v-for="item in property.selectOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
<el-input
@keydown="handleInputKeydown"
v-else
v-model="property.input"
placeholder="请输入"
clearable
/>
2025-09-19 14:16:28 +08:00
</el-form-item>
<el-form-item
v-if="property.type === 'quote'"
2025-11-17 14:54:32 +08:00
:rules="[
{
required: true,
message: '请选择参数值',
trigger: 'blur',
},
]"
2025-09-19 14:16:28 +08:00
:prop="`nodeParams.${index}.quote`"
>
<el-cascader
2025-11-17 14:54:32 +08:00
:ref="
(el) => {
if (el) cascaderRefs[index] = el;
}
"
2025-09-19 14:16:28 +08:00
v-model="property.quote"
2025-09-24 08:59:23 +08:00
:checkStrictly="true"
2025-09-19 14:16:28 +08:00
:options="quoteOptions"
placeholder="请选择"
2025-11-17 14:54:32 +08:00
@visible-change="
(visible) => visibleChange(visible, index, property.quote)
"
2025-09-19 14:16:28 +08:00
@change="(value) => cascaderChange(value, index)"
/>
</el-form-item>
</el-form-item>
</el-row>
</div>
</el-form>
</div>
</div>
2025-11-17 14:54:32 +08:00
<div class="output__container" v-show="nodeZoom">
<div v-if="props.properties?.outputParams?.length > 0">
<div class="title">
<div class="left">
<div class="tag"></div>
<div class="text">输出</div>
</div>
<div class="right">
<el-button
:disabled="flowStore.disableForm"
@click="addOutputFormItem"
class="addFormItem"
>
<el-icon :size="20"><Plus /></el-icon>
</el-button>
</div>
2025-09-19 14:16:28 +08:00
</div>
2025-11-17 14:54:32 +08:00
<div class="form__container">
<el-form
:inline="true"
:model="formData"
label-position="top"
label-width="auto"
:rules="outputRules"
ref="outputFormRef"
2025-09-19 14:16:28 +08:00
>
2025-11-17 14:54:32 +08:00
<FormItemRecursive
formType="output"
:current-list="formData.outputParams"
prop-path="outputParams"
:depth="0"
:is-first-level="true"
:endDepth="2"
:parent-path="[]"
@delete-item="deleteTopLevelItem"
/>
</el-form>
2025-09-19 14:16:28 +08:00
</div>
</div>
</div>
</div>
</template>
<script setup>
2025-11-17 15:24:45 +08:00
import { ref, reactive, onMounted, onUnmounted, nextTick } from "vue";
import { getInput, initNodeZoom } from "@/utils/flow";
2025-09-19 14:16:28 +08:00
import { useFlowStore } from "@/store/modules/flow";
import { Plus } from "@element-plus/icons-vue";
import NodeTitle from "../../components/NodeTitle.vue";
import NodeState from "../../components/NodeState.vue";
import "vue3-json-viewer/dist/index.css";
import { emitter } from "@/utils/eventBus";
import IPlayer from "@/views/device/register/components/LogicFlow/IPlayer/index.vue";
import FormItemRecursive from "./FormItemRecursive.vue";
const props = defineProps({
model: Object,
properties: Object,
});
const emits = defineEmits(["contentChange"]);
const flowStore = useFlowStore();
const formData = reactive({
nodeParams: [],
2025-11-17 14:54:32 +08:00
outputParams: [],
2025-09-19 14:16:28 +08:00
});
const rules = reactive({});
const quoteOptions = ref([]);
const handleTypeChange = (index) => {
if (formData.nodeParams[index].type === "input") {
formData.nodeParams[index].quote = "";
} else {
formData.nodeParams[index].input = "";
const option = getInput(props.model.id);
if (option) {
quoteOptions.value = option;
}
}
};
2025-11-17 14:54:32 +08:00
const cascaderRefs = ref([]);
2025-09-19 14:16:28 +08:00
const outputRules = reactive({});
const cascaderChange = (value, index) => {
const selectedOptions = cascaderRefs.value[index].getCheckedNodes(true);
2025-11-17 14:54:32 +08:00
formData.nodeParams[index].quote = value;
formData.nodeParams[index].quoteType = selectedOptions[0].data.type;
};
2025-09-19 14:16:28 +08:00
const addOutputFormItem = () => {
formData.outputParams.push({
2025-11-17 14:54:32 +08:00
name: "",
type: "",
desc: "",
children: [],
});
};
2025-09-19 14:16:28 +08:00
const dynamicForm = ref();
2025-11-17 14:54:32 +08:00
const outputFormRef = ref();
2025-09-19 14:16:28 +08:00
const setNodeProperties = async () => {
try {
const result = await dynamicForm.value.validate();
2025-11-17 14:54:32 +08:00
let flag = true;
2025-09-22 15:42:11 +08:00
if (outputFormRef.value) {
2025-11-17 14:54:32 +08:00
flag = await outputFormRef.value.validate();
2025-09-22 15:42:11 +08:00
}
2025-11-17 14:54:32 +08:00
2025-09-22 15:42:11 +08:00
if (result && flag) {
2025-09-19 14:16:28 +08:00
const data = toRaw(formData);
const properties = lf.getProperties(props.model.id);
lf.setProperties(props.model.id, {
...properties,
...data,
2025-11-17 15:24:45 +08:00
zoom: nodeZoom.value
2025-09-19 14:16:28 +08:00
});
emits("contentChange");
}
} catch {
// dynamicForm.value.clearValidate();
}
};
const nodeOperatingStatus = ref("NORMAL");
const inputJsonData = ref({});
const outputJsonData = ref({});
2025-09-24 08:59:23 +08:00
const visibleChange = (value, index, quote) => {
2025-09-19 14:16:28 +08:00
if (value) {
const option = getInput(props.model.id);
if (option) {
quoteOptions.value = option;
2025-09-24 08:59:23 +08:00
const currentValue = [...quote];
// 强制级联选择器重新处理选中值与选项的匹配
if (cascaderRefs.value[index]) {
// 等待DOM更新后再设置值确保新options已生效
setTimeout(() => {
// 如果当前有选中值,重新设置一次以触发重新匹配
if (currentValue.length) {
formData.nodeParams[index].quote = [];
// 确保响应式更新
setTimeout(() => {
formData.nodeParams[index].quote = currentValue;
}, 0);
}
// 手动触发级联选择器的重新渲染
// cascaderRefs.value[index].updatePopper();
}, 0);
}
2025-09-19 14:16:28 +08:00
}
}
};
const validateForm = async () => {
try {
const result = await dynamicForm.value.validate();
if (result) {
return { valid: true, message: "验证通过" };
} else {
return { valid: false, message: "验证失败" };
}
} catch {
return { valid: false, message: "验证失败" };
}
};
const addFormItem = () => {
formData.nodeParams.push({
name: "",
type: "",
desc: "",
required: false,
children: [],
});
emits("contentChange");
};
// 删除顶层表单项
const deleteTopLevelItem = (fullPath) => {
2025-11-17 14:54:32 +08:00
// 从顶层数据开始查找
2025-09-19 14:16:28 +08:00
let currentLevel = formData.outputParams;
2025-11-17 14:54:32 +08:00
2025-09-19 14:16:28 +08:00
// 遍历路径(除最后一个索引,因为最后一个是要删除的项)
for (let i = 0; i < fullPath.length - 1; i++) {
const index = fullPath[i];
// 进入下一层级
currentLevel = currentLevel[index].children;
}
2025-11-17 14:54:32 +08:00
2025-09-19 14:16:28 +08:00
// 最后一个索引是当前层级要删除的项
const lastIndex = fullPath[fullPath.length - 1];
currentLevel.splice(lastIndex, 1);
emits("contentChange");
2025-11-17 14:54:32 +08:00
};
2025-11-17 15:24:45 +08:00
const nodeZoom = ref(props?.properties?.zoom ?? true)
2025-11-17 14:54:32 +08:00
const zoom = (flag) => {
nodeZoom.value = flag
2025-11-17 15:24:45 +08:00
initNodeZoom(props.model.id, nodeZoom.value, '.node__box')
2025-09-19 14:16:28 +08:00
}
watch(
() => props.properties,
() => {
if (props.properties.nodeParams && props.properties.nodeParams.length > 0) {
formData.nodeParams = props.properties.nodeParams;
const option = getInput(props.model.id);
if (option) {
quoteOptions.value = option;
}
2025-11-17 15:24:45 +08:00
nextTick(() => {
initNodeZoom(props.model.id, props?.properties?.zoom ?? true, '.node__box', true)
})
2025-09-19 14:16:28 +08:00
}
2025-11-17 14:54:32 +08:00
if (
props.properties.outputParams &&
props.properties.outputParams.length > 0
) {
2025-09-19 14:16:28 +08:00
formData.outputParams = props.properties.outputParams;
}
},
{
immediate: true,
deep: true,
}
);
2025-11-13 21:55:20 +08:00
const handleInputKeydown = (e) => {
// 阻止事件冒泡到 Logic Flow 节点,避免被其事件拦截
e.stopPropagation();
// 可选:明确放行 Ctrl+V增强兼容性
2025-11-17 14:54:32 +08:00
if ((e.ctrlKey || e.metaKey) && e.key === "v") {
2025-11-13 21:55:20 +08:00
e.returnValue = true; // 允许默认粘贴行为
}
2025-11-17 14:54:32 +08:00
};
2025-11-13 21:55:20 +08:00
2025-09-19 14:16:28 +08:00
onMounted(() => {
emitter.on("changeNodeState", (data) => {
if (data.nodeId === props.model.id) {
nodeOperatingStatus.value = data.status;
let inputData = {};
let output = {};
try {
inputData = JSON.parse(data.paramsIn) || {};
output = JSON.parse(data.paramsOut) || {};
} catch (error) {
inputData = data.paramsIn || {};
output = data.paramsOut || {};
}
inputJsonData.value = inputData;
outputJsonData.value = output;
emits("contentChange");
}
});
emitter.on("contentChange", (data) => {
if (data.id === props.model.id) {
nodeOperatingStatus.value = "NORMAL";
inputJsonData.value = {};
outputJsonData.value = {};
emits("contentChange");
}
});
});
onUnmounted(() => {
emitter.off("changeNodeState");
emitter.off("contentChange");
});
defineExpose({
validateForm,
setNodeProperties,
});
</script>
<style lang="scss" scoped>
.node__container {
width: 100%;
height: auto;
.input__container {
width: 100%;
background-color: #fafbfc;
padding: 0 16px;
border-radius: 8px;
box-sizing: border-box;
.title {
height: 32px;
display: flex;
align-items: center;
justify-content: space-between;
.left {
display: flex;
align-items: center;
.tag {
width: 3px;
height: 16px;
background: #1664ff;
border-radius: 0 4px 4px 0;
margin-right: 12px;
}
.text {
font-size: 16px;
font-weight: 700;
color: #0c0d0e;
}
}
.right {
.addFormItem {
border: none;
background: transparent;
cursor: pointer;
}
}
}
.form__container {
margin: 12px 0;
.sub-properties {
margin-left: 10px;
.zw {
margin-left: 15px;
&::before {
content: "";
position: absolute;
left: 0;
top: -4px;
width: 10px;
border-left: 1px solid gray;
border-bottom: 1px solid gray;
border-bottom-left-radius: 4px;
background-color: transparent;
height: 24px;
}
}
}
.gSon-properties {
margin-left: 10px;
.zw {
margin-left: 15px;
&::before {
content: "";
position: absolute;
left: 0;
top: -4px;
width: 10px;
border-left: 1px solid gray;
border-bottom: 1px solid gray;
border-bottom-left-radius: 4px;
background-color: transparent;
height: 24px;
}
}
}
}
}
.output__container {
width: 100%;
background-color: #fafbfc;
padding: 0 16px;
border-radius: 8px;
box-sizing: border-box;
.title {
height: 32px;
display: flex;
align-items: center;
justify-content: space-between;
.left {
display: flex;
align-items: center;
.tag {
width: 3px;
height: 16px;
background: #1664ff;
border-radius: 0 4px 4px 0;
margin-right: 12px;
}
.text {
font-size: 16px;
font-weight: 700;
color: #0c0d0e;
}
}
.right {
.addFormItem {
border: none;
background: transparent;
cursor: pointer;
}
}
}
.form__container {
margin: 12px 0;
}
}
:deep(.el-row) {
align-items: end;
2025-11-17 14:54:32 +08:00
2025-09-19 14:16:28 +08:00
.el-input {
2025-11-17 14:54:32 +08:00
--el-input-width: 148px;
2025-09-19 14:16:28 +08:00
}
.el-select {
2025-11-17 14:54:32 +08:00
--el-select-width: 148px;
2025-09-19 14:16:28 +08:00
}
.el-cascader {
2025-09-22 15:42:11 +08:00
--el-form-inline-content-width: 148px;
2025-09-19 14:16:28 +08:00
}
}
}
</style>