Files
admin-govern/src/views/govern/device/planData/components/schemeTree.vue

400 lines
13 KiB
Vue
Raw Normal View History

2024-06-18 16:35:53 +08:00
<template>
<div>
2024-12-25 10:53:07 +08:00
<div :style="{ width: menuCollapse ? '40px' : '280px' }" style=" overflow: hidden; height: 100%">
2024-11-19 10:39:46 +08:00
<Icon v-show="menuCollapse" @click="onMenuCollapse" :name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
:class="menuCollapse ? 'unfold' : ''" size="18" class="fold ml10 mt20 menu-collapse"
style="cursor: pointer" />
<div class="cn-tree" :style="{ opacity: menuCollapse ? 0 : 1, display: menuCollapse ? 'none' : '' }">
2024-06-18 16:35:53 +08:00
<div style="display: flex; align-items: center" class="mb10">
2024-12-25 10:53:07 +08:00
<el-input maxlength="32" show-word-limit v-model.trim="filterText" placeholder="请输入内容" clearable>
2024-06-18 16:35:53 +08:00
<template #prefix>
<Icon name="el-icon-Search" style="font-size: 16px" />
</template>
</el-input>
2024-12-13 14:36:23 +08:00
<el-tooltip placement="bottom" :hide-after="0">
2024-12-09 16:30:40 +08:00
<template #content>
<span>新增方案</span>
</template>
<Icon name="el-icon-Plus" size="18" class="fold ml10 menu-collapse" style="cursor: pointer"
@click="onAdd" />
</el-tooltip>
2024-11-19 10:39:46 +08:00
<Icon @click="onMenuCollapse" :name="menuCollapse ? 'el-icon-Expand' : 'el-icon-Fold'"
:class="menuCollapse ? 'unfold' : ''" size="18" class="fold ml10 menu-collapse"
style="cursor: pointer" />
2024-06-18 16:35:53 +08:00
</div>
2024-11-19 10:39:46 +08:00
<el-tree style="flex: 1; overflow: auto" :props="defaultProps" highlight-current
:filter-node-method="filterNode" node-key="id" v-bind="$attrs" default-expand-all :data="tree"
ref="treRef" @node-click="clickNode" :expand-on-click-node="false">
2024-06-18 16:35:53 +08:00
<template #default="{ node, data }">
<span class="custom-tree-node">
<div class="left">
2024-11-19 10:39:46 +08:00
<Icon :name="data.icon" style="font-size: 16px" :style="{ color: data.color }"
v-if="data.icon" />
2024-06-24 14:38:42 +08:00
<span>{{ node.label }}</span>
2024-06-18 16:35:53 +08:00
</div>
<div class="right">
2024-12-09 16:30:40 +08:00
2024-10-21 09:09:56 +08:00
<a :style="{ marginRight: '0.5rem' }" v-if="data?.children">
2024-06-18 16:35:53 +08:00
<el-icon :style="{ color: '#0000FF' }">
2024-12-09 16:30:40 +08:00
<el-tooltip placement="bottom" :hide-after="0">
<template #content>
<span>新增测试项</span>
</template>
<Plus @click.stop="add(node, data)" />
</el-tooltip>
2024-06-18 16:35:53 +08:00
</el-icon>
</a>
2024-11-19 10:39:46 +08:00
<a :style="{ marginRight: '0.5rem' }" v-else>
<el-icon :style="{ color: '#0000FF' }">
2024-12-13 14:36:23 +08:00
<el-tooltip placement="bottom" :hide-after="0">
2024-12-09 16:30:40 +08:00
<template #content>
2024-12-17 20:57:07 +08:00
<span>数据绑定</span>
2024-12-09 16:30:40 +08:00
</template>
<SetUp @click.stop="bind(node, data)" />
</el-tooltip>
2024-11-19 10:39:46 +08:00
</el-icon>
</a>
2024-06-18 16:35:53 +08:00
<a :style="{ marginRight: '0.5rem' }">
2024-12-09 16:30:40 +08:00
<el-icon :style="{ color: '#0000FF' }">
2024-12-13 14:36:23 +08:00
<el-tooltip placement="bottom" :hide-after="0">
2024-12-09 16:30:40 +08:00
<template #content>
2024-12-17 20:57:07 +08:00
<span> {{ data.pid ? '修改测试项' : ' 修改测试方案' }}</span>
2024-12-09 16:30:40 +08:00
</template>
<Edit @click.stop="edit(node, data)" />
</el-tooltip>
2024-06-18 16:35:53 +08:00
</el-icon>
2024-12-13 14:36:23 +08:00
2024-06-18 16:35:53 +08:00
</a>
<a :style="{ marginRight: '0.5rem' }">
2024-12-09 16:30:40 +08:00
<el-icon :style="{ color: '#DA3434' }">
<Delete @click.stop="del(node, data)" />
2024-06-18 16:35:53 +08:00
</el-icon>
</a>
</div>
</span>
</template>
</el-tree>
</div>
</div>
<popup ref="dialogRef" @onSubmit="getTreeList"></popup>
</div>
</template>
<script lang="ts" setup>
2024-10-30 15:02:25 +08:00
import { ref, nextTick, watch, defineProps, defineEmits } from 'vue'
2024-09-26 11:20:12 +08:00
import { getSchemeTree, getTestRecordInfo } from '@/api/cs-device-boot/planData'
2024-06-18 16:35:53 +08:00
import { useConfig } from '@/stores/config'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { ElTree } from 'element-plus'
2024-12-09 16:30:40 +08:00
import { Plus, Edit, Delete, SetUp } from '@element-plus/icons-vue'
2024-06-18 16:35:53 +08:00
import { delRecord } from '@/api/cs-device-boot/planData'
import popup from './popup.vue'
2024-12-17 20:57:07 +08:00
import { getDeviceList } from '@/api/cs-device-boot/planData'
2024-06-24 14:38:42 +08:00
import { ElMessage, ElMessageBox } from 'element-plus'
2024-06-18 16:35:53 +08:00
defineOptions({
name: 'govern/schemeTree'
})
interface Props {
width?: string
canExpand?: boolean
}
2024-12-17 20:57:07 +08:00
2024-12-09 16:30:40 +08:00
const visible1 = ref(false)
const visible2 = ref(false)
const visible3 = ref(false)
const visible4 = ref(false)
2024-06-18 16:35:53 +08:00
const { proxy } = useCurrentInstance()
const menuCollapse = ref(false)
const filterText = ref('')
const treeRef = ref<InstanceType<typeof ElTree>>()
watch(filterText, val => {
2024-06-24 14:38:42 +08:00
treRef.value!.filter(val)
2024-06-18 16:35:53 +08:00
})
const onMenuCollapse = () => {
menuCollapse.value = !menuCollapse.value
proxy.eventBus.emit('cnTreeCollapse', menuCollapse)
}
2024-09-29 18:15:54 +08:00
const filterNode = (value: string, data: any, node: any) => {
2024-06-18 16:35:53 +08:00
if (!value) return true
2024-09-29 18:15:54 +08:00
// return data.name.includes(value)
if (data.name) {
return chooseNode(value, data, node)
}
2024-06-18 16:35:53 +08:00
}
2024-09-29 18:15:54 +08:00
const chooseNode = (value: string, data: any, node: any) => {
if (data.name.indexOf(value) !== -1) {
return true
}
const level = node.level
// 如果传入的节点本身就是一级节点就不用校验了
if (level === 1) {
return false
}
// 先取当前节点的父节点
let parentData = node.parent
// 遍历当前节点的父节点
let index = 0
while (index < level - 1) {
// 如果匹配到直接返回此处name值是中文字符enName是英文字符。判断匹配中英文过滤
if (parentData.data.name.indexOf(value) !== -1) {
return true
}
// 否则的话再往上一层做匹配
parentData = parentData.parent
index++
}
// 没匹配到返回false
return false
}
2024-11-19 10:39:46 +08:00
// 新增方案
const onAdd = () => {
emit('onAdd')
}
// 绑定数据
const bind = (node: any, data: any) => {
2024-12-09 16:30:40 +08:00
emit('bind', data)
2024-11-19 10:39:46 +08:00
}
2024-06-18 16:35:53 +08:00
/** 树形结构数据 */
const defaultProps = {
children: 'children',
label: 'name',
value: 'id'
}
const props = withDefaults(
defineProps<{
showCheckbox?: boolean
defaultCheckedKeys?: any
}>(),
{
showCheckbox: false,
defaultCheckedKeys: []
}
)
2024-12-09 16:30:40 +08:00
const emit = defineEmits(['init', 'checkChange', 'nodeChange', 'editNode', 'getChart', 'onAdd', 'bind'])
2024-06-18 16:35:53 +08:00
const config = useConfig()
const tree = ref()
const treRef = ref()
const id: any = ref(null)
2024-10-17 15:42:07 +08:00
const treeData = ref({})
2024-06-18 16:35:53 +08:00
//获取方案树形数据
const getTreeList = () => {
getSchemeTree().then(res => {
let arr: any[] = []
2024-10-21 09:09:56 +08:00
2024-06-18 16:35:53 +08:00
res.data.forEach((item: any) => {
2024-06-24 14:38:42 +08:00
item.icon = 'el-icon-Menu'
2024-06-18 16:35:53 +08:00
item.color = config.getColorVal('elementUiPrimary')
2024-10-21 09:09:56 +08:00
item?.children.forEach((item2: any) => {
2024-06-24 14:38:42 +08:00
item2.icon = 'el-icon-Document'
2024-06-18 16:35:53 +08:00
item2.color = config.getColorVal('elementUiPrimary')
arr.push(item2)
})
})
tree.value = res.data
nextTick(() => {
if (arr.length) {
2024-10-17 15:42:07 +08:00
treRef.value.setCurrentKey(id.value || arr[0].id)
2024-10-21 09:09:56 +08:00
let list = id.value ? arr.find((item: any) => item.id == id.value) : arr[0]
2024-06-18 16:35:53 +08:00
// 注册父组件事件
emit('init', {
level: 2,
2024-10-17 15:42:07 +08:00
...list
2024-06-18 16:35:53 +08:00
})
} else {
emit('init')
}
})
})
}
getTreeList()
const dialogRef = ref()
const handleOpen = (val: any, id: any) => {
dialogRef.value.open(val, id)
}
2024-06-24 14:38:42 +08:00
//方案id
2024-06-18 16:35:53 +08:00
const planId: any = ref('')
2024-06-24 14:38:42 +08:00
//测试项id
const monitorId: any = ref('')
2024-06-18 16:35:53 +08:00
const planData: any = ref({})
const getPlanData = (row: any) => {
planData.value = {}
planData.value = JSON.parse(JSON.stringify(row))
}
/** 添加树节点 */
2024-06-24 14:38:42 +08:00
// 0 新增方案 1 修改方案 2 新增测试项 3 修改测试项 4 设备信息
2024-06-18 16:35:53 +08:00
const add = (node: any, data: any) => {
planId.value = data.id
//添加测试项
2024-10-21 09:09:56 +08:00
if (data?.children) {
dialogRef.value.detailsType('tree')
2024-06-18 16:35:53 +08:00
handleOpen(2, planId.value)
}
}
/** 编辑树节点 */
const edit = async (node: Node, data: any) => {
planId.value = data.id
2024-09-26 11:20:12 +08:00
await getTestRecordInfo(planId.value)
.then(res => {
//修改方案
2024-10-21 09:09:56 +08:00
if (data?.children) {
dialogRef.value.detailsType('')
2024-09-29 11:15:34 +08:00
dialogRef.value.details(res.data)
2024-09-26 11:20:12 +08:00
handleOpen(1, planId.value)
}
//修改测试项
else {
monitorId.value = data.id
dialogRef.value.detailsType('tree')
2024-09-29 11:15:34 +08:00
dialogRef.value.details(res.data.records[0])
2024-09-26 11:20:12 +08:00
handleOpen(3, planId.value)
}
})
2024-11-19 10:39:46 +08:00
.catch(e => { })
2024-06-18 16:35:53 +08:00
}
/** 删除树节点 */
2024-12-25 10:53:07 +08:00
const del = async (node: Node, data: any) => {
2024-12-17 20:57:07 +08:00
let titleList = ''
2024-06-18 16:35:53 +08:00
planId.value = data.id
2024-12-25 10:53:07 +08:00
await getDeviceList({
2024-12-17 20:57:07 +08:00
id: data.id,
isTrueFlag: 1
}).then(res => {
if (res.data.length > 0) {
titleList = '已绑定数据_'
}
})
2024-06-18 16:35:53 +08:00
//删除方案/测试项
2024-12-17 20:57:07 +08:00
ElMessageBox.confirm(titleList + '是否确认删除?', {
2024-06-24 14:38:42 +08:00
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
2024-06-18 16:35:53 +08:00
})
2024-06-24 14:38:42 +08:00
.then(() => {
2024-09-25 16:31:45 +08:00
delRecord({ id: data.id }).then((res: any) => {
2024-06-24 14:38:42 +08:00
if (res.code == 'A0000') {
ElMessage.success('删除成功')
2024-11-19 10:39:46 +08:00
id.value = null
2024-06-24 14:38:42 +08:00
getTreeList()
}
})
})
.catch(() => {
ElMessage({
type: 'info',
message: '已取消'
})
})
2024-06-18 16:35:53 +08:00
}
2024-06-24 14:38:42 +08:00
//取消删除
2024-11-19 10:39:46 +08:00
const cancelDel = () => { }
2024-06-18 16:35:53 +08:00
const clickNode = (e: anyObj) => {
2024-10-21 09:09:56 +08:00
e?.children ? (planId.value = e.id) : (planId.value = e.pid)
2024-10-17 15:42:07 +08:00
id.value = e.id
2024-06-18 16:35:53 +08:00
emit('nodeChange', e)
}
2024-09-25 16:31:45 +08:00
const setCheckedNode = (e: anyObj) => {
// console.log('🚀 ~ setCheckedNode ~ e:', e)
2024-10-21 09:09:56 +08:00
id.value = e
2024-09-25 16:31:45 +08:00
treRef.value.setCurrentKey(e)
}
2024-06-18 16:35:53 +08:00
watch(
() => planData.value,
(val, oldVal) => {
if (val && dialogRef.value) {
2024-06-24 14:38:42 +08:00
const obj = JSON.parse(JSON.stringify(val))
obj.records = [
val.records.find(item => {
return item.id == monitorId.value
})
]
dialogRef.value.details(obj)
2024-06-18 16:35:53 +08:00
}
},
{
immediate: true,
deep: true
}
)
2024-09-25 16:31:45 +08:00
defineExpose({ treeRef, getPlanData, getTreeList, setCheckedNode })
2024-06-18 16:35:53 +08:00
</script>
<style lang="scss" scoped>
.cn-tree {
flex-shrink: 0;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: 10px;
height: 100%;
width: 100%;
:deep(.el-tree) {
border: 1px solid var(--el-border-color);
}
:deep(.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content) {
background-color: var(--el-color-primary-light-7);
}
.menu-collapse {
color: var(--el-color-primary);
}
}
2024-06-24 14:38:42 +08:00
2024-06-18 16:35:53 +08:00
.ml10 {
margin-bottom: 0 !important;
}
2024-09-29 18:15:54 +08:00
2024-06-18 16:35:53 +08:00
.add_plan {
width: 100%;
height: 40px;
display: flex;
justify-content: flex-end;
align-items: center;
}
2024-09-29 18:15:54 +08:00
2024-06-18 16:35:53 +08:00
.custom-tree-node {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
2024-09-29 18:15:54 +08:00
2024-06-24 14:38:42 +08:00
.left,
.right {
display: flex;
align-items: center;
}
2024-09-29 18:15:54 +08:00
2024-09-25 16:31:45 +08:00
.left {
span {
2024-06-24 14:38:42 +08:00
margin-left: 2px;
}
}
2024-09-29 18:15:54 +08:00
2024-09-25 16:31:45 +08:00
.right {
a {
2024-06-24 14:38:42 +08:00
margin-left: 2px;
}
}
2024-06-18 16:35:53 +08:00
}
</style>