吉安感知网项目-前端
罗广辉
2026-01-06 457ec00abf3dbc49fca614a4e0a2ea122f7fad3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<template>
  <div class="table-overlay">
    <div class="searchBox">
      <el-input
        class="ztzf-layer-input inputName"
        v-model="formData.dkbh"
        placeholder="输入名称模糊搜索"
      ></el-input>
      <div class="searchAndReset">
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button class="ztzf-button" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>
    <div class="btnGroups">
      <el-button v-if="activeName !== '国土空间规划'" class="ztzf-button" @click="addFence">{{
        props.activeName === '自定义识别区' ? '新增识别区' : '新增禁飞区'
      }}</el-button>
      <el-button v-if="activeName !== '国土空间规划'" class="ztzf-button" @click="addFolder"
        >新增文件夹</el-button
      >
      <el-upload
        v-if="activeName === '国土空间规划'"
        action="#"
        :show-file-list="false"
        :before-upload="e => uploadFlightFile(e, '1')"
        accept=".zip"
      >
        <el-button class="ztzf-button"> 上传 </el-button>
      </el-upload>
 
      <!-- <template v-if="activeName === '自定义禁飞区'">
        <template v-if="failuresInfo.length">
          <el-button class="ztzf-button" @click="synchronize"> 继续同步 </el-button>
          <el-button class="ztzf-button" @click="failuresDel"> 取消同步 </el-button>
        </template>
        <el-button v-else class="ztzf-button" @click="synchronize"> 同步 </el-button>
      </template> -->
 
      <!-- <el-button class="ztzf-button" @click="selectAll">全选</el-button> -->
      <el-button class="ztzf-button" @click="selectAll">
  {{ checkedKeys.length === getAllFenceKeys().length ? '取消全选' : '全选' }}
</el-button>
    </div>
    <div
      class="tableListBox"
      v-loading="loading"
      element-loading-background="rgba(0, 0, 0, 0.2)"
      element-loading-text="加载中..."
    >
      <el-tree
        class="ztzf-el-tree"
        :class="{ isTheTerritory: activeName === '国土空间规划' }"
        ref="treeRef"
        v-model="checkedKeys"
        :data="treeData"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
         :check-on-click-node="true"
        :filter-node-method="filterNode"
        highlight-current
        @check="handleCheck"
        :current-node-key="currentNodeKey"
        @node-click="handleNodeClick"
        :props="{
          label: 'name',
          children: 'children',
        }"
      >
        <!-- 自定义节点内容 -->
        <template #default="{ node }">
          <div class="tree-node" >
            <!-- 节点名称 -->
            <span class="nodeName" >{{ node.label }}</span>
            <!-- 操作按钮组 -->
            <div class="tree-node-actions" >
              <!-- <el-button
                link
               icon="el-icon-location"
                @click.stop="handleLocation(node)"
                :disabled="node.data.level === 2 && (!node.data.children || node.data.children.length === 0)"
              class="location-btn"
              ></el-button> -->
              <el-button icon="el-icon-edit" link @click.stop="handleEdit(node)"></el-button>
              <el-button icon="el-icon-delete" :class="isDisabledColor ? 'location-btn' : ''" link @click.stop="handleDelete(node)" 
                :disabled="!!formData.dkbh.trim() && node.data.level === 2 && props.activeName !== '国土空间规划' && (
     filteredShowNodeIds.has(node.data.id) || isNodeOrChildrenMatch(node.data, formData.dkbh.trim())
     )"></el-button>
            </div>
          </div>
        </template>
      </el-tree>
    </div>
  </div>
</template>
 
<script setup>
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  treeDataApi,
  deleteFenceApi,
  deleteFolderApi,
  spatialPlanningApi,
  failuresListApi,
  failuresDelApi,
  areasUpdateApi,
} from '@/api/layer/index';
import { inject, nextTick, onMounted, watch } from 'vue';
const emit = defineEmits(['update:coverData', 'update:deitData', 'newFencesMethods']);
import EventBus from '@/utils/eventBus';
const coverData = ref();
const layerParams = inject('layerParams');
const checkedKeys = ref([]); // 存储勾选的节点key
const treeRef = ref(null); // 获取tree实例
const checkedNodes = ref([]); // 存储勾选的节点数据
const props = defineProps(['activeName','resetCheck']);
const formData = ref({
  isPush: undefined,
  page: 1,
  isGenerated: undefined,
  pageSize: 17,
  dkbh: '',
  patchesName: '',
});
const loading = ref(false);
const treeData = ref([]);
const treeAllData = ref([]);
// 存储过滤后显示的节点ID(返回true的节点)
const filteredShowNodeIds = ref(new Set());
const isDisabledColor = ref(false)
// 存储当前选中节点的key
const currentNodeKey = ref(null); 
// 节点点击事件:切换选中/取消选中
const handleNodeClick = (nodeData, node) => {
  // 如果点击的是已经高亮的节点,则取消高亮
  if (!node.checked) {
    treeRef.value?.setCurrentKey(null);
  }
};
// 获取数据
const gettreeDataApi = async () => {
  try {
    loading.value = true;
    const res = await treeDataApi({filterExpired:false});
    treeAllData.value = res.data.data;
    // 加载数据后立即清空标记
    filteredShowNodeIds.value.clear();
    if(coverData.value.length===0){
       checkedKeys.value = [];
    checkedNodes.value = [];
    coverData.value = [];
     nextTick(() => {
      if (treeRef.value) {
        treeRef.value.setCheckedKeys([]);
      }
    });
    }
 
    setupWatch();
  } catch (error) {
    console.error('获取数据失败:', error);
  } finally {
    loading.value = false;
  }
};
 
const setupWatch = () => {
  watch(
    () => props.activeName,
    newVal => {
      const matchedCategory = treeAllData.value.find(item => item.name === newVal);
      treeData.value = matchedCategory.children;
      nextTick(search);
      if (treeData.value && treeData.value.length > 0) {
        const folderNode = treeData.value[0];
        // 赋值给 layerParams
        layerParams.value.total_count = folderNode.total_count ? folderNode.total_count : 0;
        layerParams.value.total_area = folderNode.total_area ? folderNode.total_area : 0;
      }
    },
    { immediate: true }
  );
};
const handleCheck = (checkedNodesData, checkedKeysData) => {
layerParams.value.isSingleLocating = false
  const filteredNodes = checkedKeysData.checkedNodes.filter(node => node.level === 3);
  checkedNodes.value = filteredNodes
  checkedKeys.value = filteredNodes.map(node => node.id);
  coverData.value = filteredNodes;
  emit('update:coverData', coverData.value);
};
// 全选方法
const getAllFenceKeys = () => {
  return treeData.value.flatMap(
    folder => folder.children.map(fence => fence.id)
  );
};
const selectAll = () => {
  currentNodeKey.value = null;
 layerParams.value.currentLocationFolderId = null;
layerParams.value.isSingleLocating = false
  // 清除高亮
  currentNodeKey.value = null;
  treeRef.value?.setCurrentKey(null);
  if (treeRef.value) {
    const allFenceKeys = getAllFenceKeys();
    const allFenceNodes = treeData.value.flatMap(folder => folder.children);
    if (checkedKeys.value.length === allFenceKeys.length) {
      // 取消全选
      treeRef.value.setCheckedKeys([]);
      checkedKeys.value = [];
      checkedNodes.value = [];
      coverData.value = [];
      emit('update:coverData', coverData.value);
    } else {
      // 全选
      treeRef.value.setCheckedKeys(allFenceKeys);
      checkedKeys.value = allFenceKeys;
      checkedNodes.value = allFenceNodes;
      coverData.value = allFenceNodes;
      emit('update:coverData', coverData.value);
 
    }
 
  }
};
// 定位按钮事件
const handleLocation = node => {
 const currentKey = treeRef.value.getCurrentKey();
      if (currentKey === node.data.id) {
        treeRef.value.setCurrentKey(null); // 取消高亮
      } else {
        treeRef.value.setCurrentKey(node.data.id); // 高亮当前节点
      }
  if (node.data.level === 2) {
    layerParams.value.isSingleLocating = false;
    // 新增:标记当前定位的文件夹ID
    layerParams.value.currentLocationFolderId = node.data.id;
  } else if (node.data.level === 3) {
    layerParams.value.isSingleLocating = true;
    // 清空文件夹定位标识
    layerParams.value.currentLocationFolderId = null;
  }
 
  const currentCheckedKeys = [...checkedKeys.value];
  const currentCheckedNodes = [...checkedNodes.value];
  EventBus.emit('focusOnNode', node.data);
  if (node.data.level === 2) {
    const folderChildren = node.data.children || [];
    const childrenIds = folderChildren.map(child => child.id);
    const newChildrenIds = childrenIds.filter(id => !currentCheckedKeys.includes(id));
    const newChildrenNodes = folderChildren.filter(child => newChildrenIds.includes(child.id));
 
    const newCheckedKeys = [...new Set([...currentCheckedKeys, ...newChildrenIds])];
    const newCheckedNodes = [...currentCheckedNodes, ...newChildrenNodes];
 
    treeRef.value.setCheckedKeys(newCheckedKeys);
    checkedKeys.value = newCheckedKeys;
    checkedNodes.value = newCheckedNodes;
    coverData.value = newCheckedNodes;
    emit('update:coverData', coverData.value);
  } else if (!currentCheckedKeys.includes(node.data.id)) {
    layerParams.value.isSingleLocating = true;
    currentCheckedKeys.push(node.data.id);
    currentCheckedNodes.push(node.data);
    treeRef.value.setCheckedKeys(currentCheckedKeys);
    checkedKeys.value = currentCheckedKeys;
    checkedNodes.value = currentCheckedNodes;
    coverData.value = currentCheckedNodes;
    emit('update:coverData', coverData.value);
  }
};
// 编辑按钮事件
const handleEdit = node => {
  if (node.data.level == 2) {
    layerParams.value.editFolder = true;
    layerParams.value.addAnEditingFolder = 2;
    if (props.activeName === '自定义识别区') {
      layerParams.value.fileType = 1;
    } else if (props.activeName === '自定义禁飞区') {
      layerParams.value.fileType = 2;
    }
    emit('update:editFolder', node.data);
  } else if (node.data.level === 3) {
    checkedKeys.value = [];
    checkedNodes.value = [];
    coverData.value = [];
    layerParams.value.editNest = true;
    layerParams.value.decideWhetherToAddOrEdit = 2;
    if (props.activeName === '自定义识别区') {
      layerParams.value.fenceType = 1; // 电子围栏类型标识
    } else if (props.activeName === '自定义禁飞区') {
      layerParams.value.fenceType = 2; // 自定义禁飞区类型标识
    }
    emit('update:deitData', node.data);
  }
};
 
// 删除按钮事件
const handleDelete = node => {
 
  let id = node.data.id;
  ElMessageBox.confirm('确定要删除该内容吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      if (node.data.level === 2) {
        // 删除文件夹
        deleteFolderApi(id).then(res => {
          ElMessage.success('删除成功');
          gettreeDataApi();
          EventBus.emit('deleteMapEntitiesByFolderId', id);
        });
      } else if (node.data.level === 3) {
        // 删除单个围栏/禁飞区
        deleteFenceApi(id).then(res => {
          ElMessage.success('删除成功');
          gettreeDataApi();
          EventBus.emit('deleteMapEntityById', id);
        });
      }
    })
    .catch(() => {
     
    });
};
// 新增围栏
const addFence = () => {
  layerParams.value.addNest = true;
  layerParams.value.decideWhetherToAddOrEdit = 1;
  if (props.activeName === '自定义识别区') {
    layerParams.value.fenceType = 1; // 电子围栏类型标识
  } else if (props.activeName === '自定义禁飞区') {
    layerParams.value.fenceType = 2; // 自定义禁飞区类型标识
  }
  emit('newFencesMethods');
};
// 新增文件夹
const addFolder = () => {
  layerParams.value.addFolder = true;
  layerParams.value.addAnEditingFolder = 1;
  if (props.activeName === '自定义识别区') {
    layerParams.value.fileType = 1;
  } else if (props.activeName === '自定义禁飞区') {
    layerParams.value.fileType = 2;
  }
};
function uploadFlightFile(file, t) {
  const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1);
  if (!['zip'].includes(fileSuffix)) {
    return ElMessage.error('请上传zip格式的文件');
  }
  let data = new FormData();
  const params = {
    file: file,
    fileName: file.name,
  };
  Object.keys(params).forEach(key => {
    data.append(key, params[key]);
  });
  spatialPlanningApi(data).then(res => {
    ElMessage.success('上传成功');
    gettreeDataApi();
  });
}
 
function search() {
  const keyword = formData.value.dkbh.trim();
  // 有搜索词时才过滤,无则清空标记
  if (keyword) {
    treeRef.value?.filter(keyword);
    isDisabledColor.value = true
  } else {
    filteredShowNodeIds.value.clear();
    treeRef.value?.filter('');
  }
}
function reset() {
  isDisabledColor.value = false
  formData.value.dkbh = '';
  filteredShowNodeIds.value.clear();
  nextTick(() => {
    treeRef.value?.filter('');
  });
}
const isNodeOrChildrenMatch = (nodeData, keyword) => {
  // 无关键词时直接返回false
  if (!keyword || keyword.trim() === '') return false;
  // 自身包含关键词
  if (nodeData.name.includes(keyword)) return true;
  // 遍历子节点,判断是否有子节点包含
  if (nodeData.children && nodeData.children.length) {
    return nodeData.children.some(child => isNodeOrChildrenMatch(child, keyword));
  }
  return false;
};
function filterNode(value, data) {
  let isShow = false;
  const keyword = value?.trim() || ''; 
 
  if (props.activeName === '国土空间规划') {
    if (!keyword) {
      isShow = data.level === 2 || data.level === 1;
    } else {
      isShow = (data.level === 2 || data.level === 1) && data.name.includes(keyword);
    }
  } else {
    if (!keyword) {
      isShow = true;
    } else {
      // 非国土空间规划:节点自身/子节点匹配则显示
      isShow = isNodeOrChildrenMatch(data, keyword);
    }
  }
  // 仅当有搜索词时,才更新标记(无搜索词清空)
  if (keyword) {
    if (isShow && data.id) {
      filteredShowNodeIds.value.add(data.id);
    } else if (data.id) {
      filteredShowNodeIds.value.delete(data.id);
    }
  } else {
    filteredShowNodeIds.value.clear();
  }
 
  return isShow;
}
 
// 同步信息
const failuresInfo = ref([]);
function failuresList() {
  failuresListApi().then(res => {
    failuresInfo.value = res.data.data || [];
  });
}
 
// 同步
function synchronize() {
  const data = failuresInfo.value.map(item => item.device_sn);
  areasUpdateApi(data).then(res => {
    if (res.data.data.length) {
      let stringVal = res.data.data.reduce((acc, cur) => {
        return acc + cur.nickname + '(' + cur.reason + ')' + ',';
      }, '');
      stringVal = stringVal.slice(0, -1);
      ElMessageBox.alert(stringVal, '同步失败', {
        confirmButtonText: '确定',
      });
    } else {
      ElMessage.success('同步成功');
    }
  });
}
 
// 取消同步
function failuresDel() {
  failuresDelApi().then(res => {
    ElMessage.success('取消同步成功');
  });
}
// 切换tab清空复选框的数据
watch(
  () => props.resetCheck,
  (newVal) => {
    if (newVal !== undefined) {
      checkedKeys.value = [];
      checkedNodes.value = [];
      coverData.value = [];
      treeRef.value?.setCheckedKeys([]);
    }
  },
  { immediate: true }
);
onMounted(() => {
  failuresList();
  gettreeDataApi();
  EventBus.on('gettreeDataApi', gettreeDataApi);
});
onBeforeUnmount(() => {
  EventBus.off('gettreeDataApi', gettreeDataApi);
});
</script>
 
<style scoped lang="scss">
.table-overlay {
  position: absolute;
  top: 0;
  left: 0;
height: calc(100% - 32px);
  z-index: 99;
  width: 332px;
  overflow: hidden;
  background: rgba(0,0,0,0.51);
  backdrop-filter: blur(4px);
  border-radius: 8px 8px 8px 8px;
  padding: 16px 0  16px 12px;
  display: flex;
  flex-direction: column;
}
 
.searchBox {
  display: flex;
  margin-top: 10px;
padding-right: 12px;
  .inputName {
    width: 140px;
    height: 32px;
  }
  .searchAndReset {
    width: 182px;
    display: flex;
    justify-content: right;
    align-items: center;
    margin-right: 10px;
 
  }
}
.btnGroups {
  margin: 12px 0 14px 0;
  display: flex;
  gap: 0 10px;
padding-right: 12px;
  .el-button {
    margin: 0 !important;
  }
}
.tableListBox {
  overflow-y: scroll;
  height: 0;
  flex-grow: 1;
  // padding-right: 12px;
  :deep(.el-tree-node__content) {
  height: 45px !important;
  line-height: 45px !important;
  // padding-left: 0 !important;
  border-bottom: 1px dotted  rgba(255,255,255,0.1);
  }
  // 定位选中节点的背景色样式
  ::v-deep(.el-tree-node.is-current > .el-tree-node__content) {
    background-color: rgba(64, 158, 255, 0.2) !important; // 浅蓝色半透明背景
  }
  .isTheTerritory {
    :deep() {
      .el-tree-node__expand-icon {
        visibility: hidden;
      }
    }
  }
 
  :deep() {
    .el-checkbox {
      --el-checkbox-bg-color: transparent !important;
    }
  }
  .tree-node {
    display: flex;
    justify-content: space-between;
    align-items: center;
     padding-right: 12px;
    width: 100%;
       .nodeName {
    font-size: 12px !important;
    }
  }
 
  .tree-node-actions {
    display: flex;
    // gap: 8px;
 
    button {
      padding: 0;
      color: #fff;
      width: 17px;
      height: 17px;
      //  color: #409eff;
      // &:hover {
      //   color: #409eff;
      // }
    }
  }
  ::v-deep(.location-btn.el-button.is-link.is-disabled) {
  color:  #999 !important;
}
}
</style>