UPDATE: VERSION-20260316

This commit is contained in:
libertyspy
2026-03-16 22:43:12 +08:00
parent 82bbfb83ca
commit 974f403784
9 changed files with 381 additions and 41 deletions

View File

@@ -8,7 +8,8 @@
*/
import { HttpRequestClient } from '@/utils/request';
import type { PlatformWithComponentsResponse, ScenarioPageableResponse, ScenarioRequest, Scenario } from './types';
import type { Scenario, ScenarioPageableResponse, ScenarioRequest } from './types';
import type { PlatformWithComponentsResponse } from '../types';
import type { BasicResponse } from '@/types';
const req = HttpRequestClient.create<BasicResponse>({

View File

@@ -27,10 +27,11 @@
</template>
<script lang="ts">
import { defineComponent, onMounted, type PropType, ref, watch } from 'vue';
import { defineComponent, type PropType, ref, watch } from 'vue';
import { safePreventDefault, safeStopPropagation } from '@/utils/event';
import {findPlatformWithComponents} from './api'
import { type PlatformWithComponents, type Scenario } from './types';
import { findPlatformWithComponents } from './api';
import { type Scenario } from './types';
import { type PlatformWithComponents } from '../types';
export default defineComponent({
emits: ['drag-item-start', 'drag-item-end'],

View File

@@ -10,6 +10,7 @@
import type { ApiDataResponse, NullableString, PageableResponse } from '@/types';
import type { GraphContainer } from '../graph';
import type { Platform, PlatformComponent } from '../types';
export interface PlatformRelation {
sourceId: NullableString,
@@ -43,27 +44,3 @@ export interface ScenarioPageableResponse extends PageableResponse<Scenario> {
}
export interface Platform {
id: number,
name: NullableString,
description: NullableString,
scenarioId: number,
[key: string]: unknown;
}
export interface PlatformComponent {
id: number,
name: NullableString,
type: NullableString,
description: NullableString,
platformId: number,
}
export interface PlatformWithComponents extends Platform {
components: PlatformComponent[],
}
export interface PlatformWithComponentsResponse extends ApiDataResponse<PlatformWithComponents[]> {
}

View File

@@ -0,0 +1,168 @@
<template>
<a-space>
<!-- 平台选择框 -->
<a-select
style="width:280px"
v-model:value="selectedPlatformId"
:placeholder="'请选择平台'"
>
<a-select-option
v-for="item in platforms"
:key="item.id"
:value="item.id"
>
{{ item.description || item.name || '未命名平台' }}
</a-select-option>
</a-select>
<!-- 组件选择框 -->
<a-select
style="width:280px;"
v-model:value="selectedComponentId"
:placeholder="'请选择组件'"
:disabled="!selectedPlatformId"
>
<a-select-option
v-for="item in filteredComponents"
:key="item.id"
:value="item.id"
>
{{ item.description || item.name || '未命名组件' }}
</a-select-option>
</a-select>
</a-space>
</template>
<script lang="ts">
import { computed, defineComponent, onMounted, type PropType, ref, watch } from 'vue';
import type { Platform, PlatformComponent, PlatformComponentPayload, PlatformWithComponents } from '../types';
import { findAllPlatformWithComponents } from './api';
export default defineComponent({
props: {
placeholder: {
type: String,
default: '',
},
platform: {
type: [Object,null] as PropType<Platform | null>,
required: false,
default: () => ({}), // 设置默认空对象避免undefined
},
component: {
type: [Object, null] as PropType<PlatformComponent | null>,
required: false,
default: () => ({}), // 设置默认空对象避免undefined
},
},
emits: [
// 只保留统一的update事件
'update',
],
setup(props, { emit }) {
// 平台列表
const platforms = ref<PlatformWithComponents[]>([]);
// 选中的平台ID兼容props初始值
const selectedPlatformId = ref<number | undefined>(props.platform?.id);
// 选中的组件ID兼容props初始值
const selectedComponentId = ref<number | undefined>(props.component?.id);
// 平台映射表
const platformMapping = ref<Map<number, PlatformWithComponents>>(new Map());
// 计算属性:根据选中的平台过滤对应的组件列表
const filteredComponents = computed<PlatformComponent[]>(() => {
if (!selectedPlatformId.value) return [];
const currentPlatform = platformMapping.value.get(selectedPlatformId.value);
return currentPlatform?.components ?? [];
});
// 封装统一的update事件触发函数
const triggerUpdateEvent = () => {
// 获取当前选中的平台对象
const selectedPlatform = selectedPlatformId.value
? platformMapping.value.get(selectedPlatformId.value) || ({} as Platform)
: ({} as Platform);
// 获取当前选中的组件对象
const selectedComponent = selectedComponentId.value
? filteredComponents.value.find(item => item.id === selectedComponentId.value) || ({} as PlatformComponent)
: ({} as PlatformComponent);
// 触发统一的update事件传递平台和组件信息
emit('update', {
platform: {
id: selectedPlatform.id,
name: selectedPlatform.name,
description: selectedPlatform.description,
},
component: selectedComponent,
} as PlatformComponentPayload);
};
// 加载平台和组件数据
const load = () => {
platforms.value = [];
platformMapping.value.clear();
findAllPlatformWithComponents().then(re => {
platforms.value = re.data ?? [];
// 构建平台映射表
platforms.value.forEach(platform => {
platformMapping.value.set(platform.id, platform);
});
// 数据加载完成后,若有初始平台值,自动匹配组件列表
if (props.platform?.id) {
selectedPlatformId.value = props.platform.id;
// 若有初始组件值,校验是否属于当前平台
if (props.component?.id) {
const currentComponents = filteredComponents.value;
const hasComponent = currentComponents.some(item => item.id === Number(props.component?.id ?? 0));
// 若初始组件不属于当前平台,清空组件选中值
if (!hasComponent) {
selectedComponentId.value = undefined;
}
}
}
// 数据加载完成后触发一次update事件
triggerUpdateEvent();
}).catch(err => {
console.error('加载平台组件数据失败:', err);
});
};
// 监听平台选择变化
watch(selectedPlatformId, () => {
// 平台变更时,清空组件选中值
selectedComponentId.value = undefined;
// 触发统一的update事件
triggerUpdateEvent();
});
// 监听组件选择变化
watch(selectedComponentId, () => {
// 触发统一的update事件
triggerUpdateEvent();
});
watch(() => props.platform?.id, (newVal) => {
selectedPlatformId.value = newVal;
}, { immediate: true }); // 立即执行,确保初始值同步
watch(() => props.component?.id, (newVal) => {
selectedComponentId.value = newVal;
}, { immediate: true }); // 立即执行,确保初始值同步
onMounted(() => load());
return {
platforms,
selectedPlatformId,
selectedComponentId,
filteredComponents,
};
},
});
</script>

View File

@@ -9,6 +9,7 @@
import { HttpRequestClient } from '@/utils/request';
import type { FireRule, FireRulePageableResponse, FireRuleRequest } from './types';
import type { PlatformWithComponentsResponse } from '../types';
import type { BasicResponse } from '@/types';
const req = HttpRequestClient.create<BasicResponse>({
@@ -31,5 +32,8 @@ export const deleteFireRule = (id: number): Promise<BasicResponse> => {
return req.delete(`/system/rule/${id}`);
};
export const findAllPlatformWithComponents = (): Promise<PlatformWithComponentsResponse> => {
return req.get(`/system/firerule/platforms`);
};

View File

@@ -75,18 +75,52 @@
<a-form-item
label="触发条件"
:rules="[{ required: true, message: '请输入触发条件!', trigger: ['input', 'change'] }]"
:name="['conditions']"
:rules="[{ required: true, message: '请输入触发条件!', trigger: [ 'change'] }]"
:name="['conditionsArray']"
>
<a-input v-model:value="selectedFireRule.conditions" placeholder="请输入触发条件" />
<a-form-item-rest>
<div class="ks-sidebar-list-param-list">
<div class="ks-sidebar-list-param-item" v-for="(item,index) in selectedFireRule.conditionsArray">
<a-row :gutter="15">
<a-col :span="21">
<PlatformSelect @update="(payload: PlatformComponentPayload)=> handleUpdateCondition(payload, index)"
:platform="item.platform" :component="item.component"/>
</a-col>
<a-col :span="3">
<a-space class="ks-sidebar-list-param-actions">
<MinusCircleOutlined @click="()=> handleMinusCondition(index)" />
<PlusCircleOutlined @click="()=> handleAddCondition(index)" v-if="index === 0" />
</a-space>
</a-col>
</a-row>
</div>
</div>
</a-form-item-rest>
</a-form-item>
<a-form-item
label="响应动作"
:rules="[{ required: true, message: '请输入响应动作!', trigger: ['input', 'change'] }]"
:name="['actions']"
:rules="[{ required: true, message: '请输入响应动作!', trigger: ['change'] }]"
:name="['actionsArray']"
>
<a-input v-model:value="selectedFireRule.actions" placeholder="请输入响应动作" />
<a-form-item-rest>
<div class="ks-sidebar-list-param-list">
<div class="ks-sidebar-list-param-item" v-for="(item,index) in selectedFireRule.actionsArray">
<a-row :gutter="15">
<a-col :span="21">
<PlatformSelect @update="(payload: PlatformComponentPayload)=> handleUpdateAction(payload, index)"
:platform="item.platform" :component="item.component"/>
</a-col>
<a-col :span="3">
<a-space class="ks-sidebar-list-param-actions">
<MinusCircleOutlined @click="()=> handleMinusAction(index)" />
<PlusCircleOutlined @click="()=> handleAddAction(index)" v-if="index === 0" />
</a-space>
</a-col>
</a-row>
</div>
</div>
</a-form-item-rest>
</a-form-item>
<a-form-item
@@ -136,11 +170,13 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { type FormInstance, message } from 'ant-design-vue';
import { PlusOutlined } from '@ant-design/icons-vue';
import { MinusCircleOutlined, PlusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue';
import Layout from '../layout.vue';
import { createFireRule, deleteFireRule, findFireRuleByQuery, updateFireRule } from './api';
import type { FireRule, FireRuleRequest } from './types';
import { substring } from '@/utils/strings';
import PlatformSelect from './PlatformSelect.vue';
import type { PlatformComponentPayload } from '../types';
const query = ref<Partial<FireRuleRequest>>({
pageNum: 1,
@@ -155,8 +191,20 @@ const defaultFireRule: FireRule = {
sceneType: null,
// 触发条件JSON格式
conditions: null,
conditionsArray: [
{
platform: null,
component: null,
}
],
// 响应动作JSON格式
actions: null,
actionsArray: [
{
platform: null,
component: null,
}
],
// 优先级(数值越小优先级越高)
priority: 0,
// 是否启用0禁用1启用
@@ -174,20 +222,47 @@ const getSceneTypeName = (item: FireRule): string => {
};
const resolveItem = (item: FireRule) => {
let newItem = JSON.parse(JSON.stringify(item));
let newItem = JSON.parse(JSON.stringify(item)) as FireRule;
try{
newItem.conditionsArray = JSON.parse(newItem.conditions as string);
} catch(e: any){
}
if(!newItem.conditionsArray) {
newItem.conditionsArray = []
}
if(newItem.conditionsArray.length===0){
newItem.conditionsArray.push({
platform: null,
component: null,
})
}
try{
newItem.actionsArray = JSON.parse(newItem.actions as string);
} catch(e: any){
}
if(!newItem.actionsArray){
newItem.actionsArray = []
}
if(newItem.actionsArray.length===0){
newItem.actionsArray.push({
platform: null,
component: null,
})
}
return newItem;
};
const datasource = ref<FireRule[]>([]);
const datasourceTotal = ref<number>(0);
const selectedFireRule = ref<FireRule>(resolveItem(defaultFireRule));
const selectedFireRule = ref<FireRule>({...defaultFireRule});
const formRef = ref<FormInstance | null>(null);
const load = () => {
datasource.value = [];
datasourceTotal.value = 0;
formRef.value?.resetFields();
selectedFireRule.value = resolveItem(defaultFireRule);
selectedFireRule.value = {...defaultFireRule};
findFireRuleByQuery(query.value).then(r => {
datasource.value = r.rows ?? [];
@@ -197,7 +272,7 @@ const load = () => {
const handleCreate = () => {
selectedFireRule.value = resolveItem(defaultFireRule);
selectedFireRule.value = {...defaultFireRule};
};
const handleSelect = (item: FireRule) => {
@@ -223,6 +298,8 @@ const handleSave = () => {
formRef.value.validate().then(() => {
let res = null;
let savedValue: FireRule = JSON.parse(JSON.stringify(selectedFireRule.value)) as any as FireRule;
savedValue.conditions = JSON.stringify(savedValue.conditionsArray ?? null) as string;
savedValue.actions = JSON.stringify(savedValue.actionsArray ?? null) as string;
if (savedValue.id > 0) {
res = updateFireRule(savedValue);
} else {
@@ -242,6 +319,65 @@ const handleSave = () => {
}
};
const handleMinusCondition = (index: number) => {
if(selectedFireRule.value){
const paramList = selectedFireRule.value.conditionsArray;
if (index === 0 && selectedFireRule.value.conditionsArray.length === 1) {
selectedFireRule.value.conditionsArray = [{
platform: null,
component: null,
}];
} else if (index >= 0 && index < paramList.length && paramList.length > 1) {
paramList.splice(index, 1);
selectedFireRule.value.conditionsArray = [...paramList];
}
}
}
const handleAddCondition = (index: number)=> {
if(selectedFireRule.value){
selectedFireRule.value.conditionsArray.push({
platform: null,
component: null,
})
}
}
const handleMinusAction = (index: number) => {
if(selectedFireRule.value){
const paramList = selectedFireRule.value.actionsArray;
if (index === 0 && selectedFireRule.value.actionsArray.length === 1) {
selectedFireRule.value.actionsArray = [{
platform: null,
component: null,
}];
} else if (index >= 0 && index < paramList.length && paramList.length > 1) {
paramList.splice(index, 1);
selectedFireRule.value.actionsArray = [...paramList];
}
}
}
const handleAddAction = (_index: number)=> {
if(selectedFireRule.value){
selectedFireRule.value.actionsArray.push({
platform: null,
component: null,
})
}
}
const handleUpdateCondition = (payload: PlatformComponentPayload, index: number)=> {
if(selectedFireRule.value && selectedFireRule.value.conditionsArray[index]){
selectedFireRule.value.conditionsArray[index] = payload;
}
}
const handleUpdateAction = (payload: PlatformComponentPayload, index: number)=> {
if(selectedFireRule.value && selectedFireRule.value.actionsArray[index]){
selectedFireRule.value.actionsArray[index] = payload;
}
}
const handleChange = (page: number, pageSize: number) => {
query.value.pageNum = page;

View File

@@ -8,6 +8,7 @@
*/
import type { NullableString, PageableResponse } from '@/types';
import type { PlatformComponentPayload } from '../types';
export interface FireRule {
id: number,
@@ -17,8 +18,10 @@ export interface FireRule {
sceneType: Number | null,
// 触发条件JSON格式
conditions: NullableString,
// 响应动作JSON格式
actions: NullableString,
conditionsArray: PlatformComponentPayload[],
// 响应动作JSON格式
actions: NullableString,
actionsArray: PlatformComponentPayload[],
// 优先级(数值越小优先级越高)
priority: number,
// 是否启用0禁用1启用

View File

@@ -0,0 +1,10 @@
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
export * from './platform'

View File

@@ -0,0 +1,40 @@
/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
import type { ApiDataResponse, NullableString } from '@/types';
export interface Platform {
id: number,
name: NullableString,
description: NullableString,
scenarioId: number,
[key: string]: unknown;
}
export interface PlatformComponent {
id: number,
name: NullableString,
type: NullableString,
description: NullableString,
platformId: number,
}
export interface PlatformWithComponents extends Platform {
components: PlatformComponent[],
}
export interface PlatformComponentPayload {
platform: Platform | null;
component: PlatformComponent | null;
}
export interface PlatformWithComponentsResponse extends ApiDataResponse<PlatformWithComponents[]> {
}