ZZJ
2022-07-15 5c0c6ad7ac6f28e970532af9936de638830f1161
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
import {
  getLocalCameraTree,
  getGB28181CameraTree,
  getClusterTree,
  addAreaTreeData,
  delAreaTreeData,
  updateAreaTreeData,
  getGb28181Pool,
  refreshGB28181Tree,
  updateCameraArea,
  getCameraBaseImage
} from '@/api/area'
 
import {getClusterDevList} from '@/api/clusterManage'
 
export default class TreeDataPool {
  public openeds: Array<boolean>
  public activeTreeData: Array<object>
 
  public treeData: Array<object>
  public clusterData: Array<object>
  public gb28181Data: Array<object>
  public treeDataPure: Array<object>
  public gb28181DataPure: Array<object>
  public clusterDataPure: Array<object>
  public videoArr: Array<string | undefined | object>
  public searchCamType: number
  public searchInput: string
  public activeVideoIndex: number | string
  public activeVideoId: number | string
  public activeForceChoose: boolean
  public readonly: boolean
  public gbReadonly: boolean
  public multiple: boolean
  public searchFrom: string = ''
  public showTreeBox: boolean
  public selectedNodes: Array<string>
  public selectedNode: any
  // 记录当前操作的树
  public treeType: string
  // 记录收起的节点
  public foldNodeList: object
  //记录左侧tab:activeName
  public treeActiveName: string
  //控制开始、暂停按钮显示状态
  public btnStaus: string
  //本地视频类型
  public searchLocalType: number
  //记录复制的摄像机id
  public ctrlCameraId: string
  //记录复制的摄像机name
  public ctrlCameraName: string
 
  // 是否使用ztree
  public zTree: boolean
 
  public checkedTreeNode: Array<object>
 
  // 选中的摄像机个数
  public gb28181CheckedCount: number
  // 总摄像机个数
  public gb28181ChildNodeCount: number
 
  // 国标摄像机池的底图
  public cameraNameForBaseImage: string
  public gb28181CameraBaseImage: string
  public baseImageLoading: boolean
 
  public activeNode: string
 
 
  constructor() {
    this.openeds = [true, true, false]
    this.activeTreeData = []
    this.treeData = []
    this.gb28181Data = []
    this.clusterData = []
    this.treeDataPure = []
    this.gb28181DataPure = []
    this.clusterDataPure = []
    this.videoArr = ['']
    this.searchCamType = 0
    this.searchInput = ''
    this.activeVideoIndex = ''
    this.activeVideoId = ''
    this.activeForceChoose = false
    this.showTreeBox = true
    this.readonly = true
    this.gbReadonly = true
    this.multiple = false
    this.selectedNodes = []
    this.selectedNode = {}
    this.treeType = ''
    this.foldNodeList = {}
    this.treeActiveName = 'camera'
    this.searchLocalType = 0
 
    //1:暂停状态;2:等待状态;3:置灰
    this.btnStaus = '3'
    this.ctrlCameraId = ''
    this.ctrlCameraName = ''
    this.zTree = false
    this.checkedTreeNode = []
    this.gb28181CheckedCount = 0
    this.gb28181ChildNodeCount = 0
 
    this.cameraNameForBaseImage = ""
    this.gb28181CameraBaseImage = ""
    this.baseImageLoading = false
    this.activeNode = ''
 
 
  }
 
 
  setVideoArr(index: number, value: object, vue: any): void {
    vue.$set(this.videoArr, index, value)
    this.activeForceChoose = false
  }
 
  updateSelectedNodes() {
    this.selectedNodes = []
    if (!this.multiple) {
      this.selectedNodes = [this.selectedNode.id]
      return
    }
    let _selected = this.selectedNodes
    function nodeFilter(node: any) {
      if (node.type === '4' && node.selected) {
        _selected.push(node.id)
        sessionStorage.setItem('cameraDevId',node.devId)
      }
      if (node.children) {
        node.children.forEach((n: any) => {
          nodeFilter(n)
        })
      }
    }
    if (this.selectedNode.cameraType === 0) {
      //摄像机树
      if (this.treeActiveName == 'camera') {
        this.treeData.forEach((n: any) => {
          nodeFilter(n)
        })
      } else if (this.treeActiveName == 'cluster') {
        //集群树
        this.clusterData.forEach((n: any) => {
          nodeFilter(n)
        })
      }
    }
    if (this.selectedNode.cameraType === 1) {
      this.gb28181Data.forEach((n: any) => {
        nodeFilter(n)
      })
    }
    // if (this.selectedNode.cameraType === -1) {
    //   this.clusterData.forEach((n: any) => {
    //     nodeFilter(n);
    //   });
    // }
  }
 
  updateZTreeCheckNodes(checkedNodes) {
    let _this = this
    _this.selectedNodes = []
    if (!_this.multiple) {
      _this.selectedNodes = [checkedNodes.id]
      return
    }
 
    function nodeFilter(node: any) {
      if (node.type === '4' && (node.selected || node.checked)) {
      sessionStorage.setItem("cameraDevId", node.devId);
        _this.selectedNodes.push(node.id)
      }
      if (node.children) {
        node.children.forEach((n: any) => {
          nodeFilter(n)
        })
      }
    }
 
    checkedNodes.forEach((n: any) => {
      nodeFilter(n)
    })
  }
 
  getCameraInfoByIp(ipaddr) {
    let camera = null
 
    function nodeFilter(node: any) {
      if (node.rtsp && node.rtsp.indexOf(ipaddr) != -1) {
        camera = node
        return
      }
      if (node.children) {
        node.children.forEach((n: any) => {
          nodeFilter(n)
        })
      }
    }
 
    this.treeData.forEach((n: any) => {
      nodeFilter(n)
    })
 
    if (!camera) {
      this.gb28181Data.forEach((n: any) => {
        nodeFilter(n)
      })
    }
 
    return camera
  }
 
  getCameraInfoById(id) {
    let camera = null
 
    function nodeFilter(node: any) {
      if (node.id === id) {
        camera = node
        return
      }
      if (node.children) {
        node.children.forEach((n: any) => {
          nodeFilter(n)
        })
      }
    }
 
    this.treeData.forEach((n: any) => {
      nodeFilter(n)
    })
 
    if (!camera) {
      this.gb28181Data.forEach((n: any) => {
        nodeFilter(n)
      })
    }
 
    return camera
  }
 
  getParent(id: string, isGB: boolean): string {
    let parent = '0'
 
    function nodeFilter(node: Array<any>): any {
      for (let i = 0; i < node.length; i++) {
        if (node[i].id == id) {
          return true
        }
        if (node[i].children && node[i].children.length > 0) {
          if (nodeFilter(node[i].children)) {
            parent = node[i].id
          }
        }
      }
      return false
    }
 
    if (isGB) {
      nodeFilter(this.gb28181Data)
    } else {
      nodeFilter(this.treeData)
    }
 
    return parent
  }
 
  clean() {
    this.treeData = JSON.parse(JSON.stringify(this.treeDataPure))
    this.gb28181Data = JSON.parse(JSON.stringify(this.gb28181DataPure))
 
    this.isFold(this.treeData)
    this.isFold(this.gb28181Data)
    this.selectedNodes = []
    this.selectedNode = {}
  }
 
  cleanTree(tree) {
    if (tree === 'localTree') {
      this.treeData = JSON.parse(JSON.stringify(this.treeDataPure))
    }
    if (tree === 'gb28182Tree') {
      this.gb28181Data = JSON.parse(JSON.stringify(this.gb28181DataPure))
    }
  }
 
  isFold(node) {
    if (!node) {
      return
    }
    node.forEach(n => {
      // vue-js-tree 默认展开,控制部分折叠. z-tree 默认折叠, 控制部分展开
      if (this.foldNodeList[n.id]) {
        if (this.zTree) {
          n.open = false
        } else {
          n.opened = false
        }
      }
      if (n.children && n.children.length > 0) {
        this.isFold(n.children)
      }
    })
  }
 
  setDropDisable(node) {
    if (!node) {
      return
    }
    node.forEach(n => {
      if (n.children && n.children.length > 0) {
        this.setDropDisable(n.children)
      } else {
        if (n.type === '4') {
          n.dropDisabled = true
        }
      }
    })
  }
 
  sortTreeData(node) {
    if (!node) {
      return
    }
    node.sort(function (obj1: any, obj2: any) {
      var val1 = obj1.name
      var val2 = obj2.name
      if (val1 < val2) {
        return -1
      } else if (val1 > val2) {
        return 1
      } else {
        return 0
      }
    })
 
    node.forEach(n => {
      if (n.children && n.children.length > 0) {
        if (this.zTree) {
          n.open = true
        }
        this.sortTreeData(n.children)
      }
    })
  }
 
  async fetchLocalTree() {
    let params: any = {
      parentId:"",
      searchType: this.searchCamType,
      cameraName: this.searchInput,
    
      //isPlatform: 1
    }
    if (this.searchFrom == 'cluster') {
      params.isPlatform = 1
    }
  
 
    const rsp: any = await getLocalCameraTree(params)
 
    if (rsp && rsp.success) {
    
      this.treeData = rsp.data.treeMenu ? rsp.data.treeMenu : []
      if (this.treeData && this.treeData.length > 0) {
        this.sortTreeData(this.treeData)
      }
 
      // 设置禁止拖拽摄像机到摄像机节点
      this.setDropDisable(this.treeData)
      this.treeDataPure = JSON.parse(JSON.stringify(this.treeData))
      this.isFold(this.treeData)
    }
  }
 
  async fetchClusterTree() {
    const rsp: any = await getClusterTree({
      searchType: this.searchCamType,
      cameraName: this.searchInput
    })
    if (rsp && rsp.success) {
      this.clusterData = rsp.data ? rsp.data : []
      if (this.clusterData && this.clusterData.length > 0) {
        this.sortTreeData(this.clusterData)
      }
      this.clusterDataPure = JSON.parse(JSON.stringify(this.clusterData))
      this.isFold(this.clusterData)
    }
  }
 
  async fetchGbTree() {
    const rsp: any = await getGB28181CameraTree({
      parentId: "",
      searchType: this.searchCamType,
      cameraName: this.searchInput
    })
 
    if (rsp && rsp.success) {
      this.gb28181Data = rsp.data.treeMenu ? rsp.data.treeMenu : []
      if (this.gb28181Data && this.gb28181Data.length > 0) {
        this.sortTreeData(this.gb28181Data)
      }
 
      this.gb28181DataPure = JSON.parse(JSON.stringify(this.gb28181Data))
      this.isFold(this.gb28181Data)
    }
  }
 
  async fetchTreeData() {
    if (this.openeds[0]) {
      this.fetchLocalTree()
    }
    if (this.openeds[1]) {
      this.fetchGbTree()
    }
  }
 
  async add(name: string, parent: string) {
    await addAreaTreeData({
      name: name,
      parentId: parent,
    })
 
    this.fetchTreeData()
  }
 
  async del(id: string) {
    await delAreaTreeData({
      id: id,
    })
 
    this.fetchTreeData()
  }
 
  async update(name: string, id: string, alias: string, isGb: boolean) {
    await updateAreaTreeData({
      id: id,
      name: name,
      parentId: this.getParent(id, isGb),
      alias: alias,
    })
 
    this.fetchTreeData()
  }
 
  async refreshGB28181() {
    await refreshGB28181Tree()
    // this.fetchGbTree()
  }
 
  async dropNode(cameraId: string, areaId: string) {
    await updateCameraArea({ cameraId: cameraId, areaId: areaId })
  }
 
  getAllChildrenNodes(treeNode, arr) {
    for (var i = 0; i < treeNode.length; i++) {
      var sonList = treeNode[i].children;
      if (!sonList) {
        if (treeNode[i].type == "camera") {
          arr.push(treeNode[i]);
 
          if (treeNode[i].checked) {
            this.gb28181CheckedCount += 1;
          }
        }
      } else {
        this.getAllChildrenNodes(sonList, arr);
      }
    }
    return arr;
  }
  countChildrenNodes(treeNode) {
    let arry = []
 
    this.getAllChildrenNodes(treeNode, arry)
 
    return arry.length
  }
 
  async fetchVideosvrCameras(update: Boolean) {
    let fn = getGb28181Pool
    if (update) {
      fn = refreshGB28181Tree
    }
    const rsp: any = await fn()
 
    if (rsp && rsp.success) {
      this.selectedNode = {}
      this.treeData = rsp.data ? rsp.data : []
      if (this.treeData && this.treeData.length > 0) {
        this.sortTreeData(this.treeData)
      }
 
      // 设置禁止拖拽摄像机到摄像机节点
      this.setDropDisable(this.treeData)
      this.isFold(this.treeData)
 
      this.gb28181CheckedCount = 0
      this.gb28181ChildNodeCount = this.countChildrenNodes(this.treeData)
      this.activeTreeData = this.treeData
    }
  }
 
  removeNoCheckedNode(nodes: Array<any>) {
    for (let i = 0; i < nodes.length;) {
      if (!nodes[i].checked) {
        nodes.splice(i, 1)
        continue
      }
 
      if (nodes[i].children && nodes[i].children.length) {
        this.removeNoCheckedNode(nodes[i].children)
      }
      i++
    }
  }
 
  countCheckedNodes(nodes: Array<any>) {
    let count = 0
    nodes.forEach(n => {
      if (n.type == "camera") {
        count++
      }
    })
    this.gb28181CheckedCount = count
  }
 
  newTreeByChecked(nodes: Array<object>) {
    let newTree = JSON.parse(JSON.stringify(nodes))
    this.removeNoCheckedNode(newTree)
 
    return newTree
  }
 
  async showBaseImage(node: any) {
    this.baseImageLoading = true
    this.gb28181CameraBaseImage = ""
    this.cameraNameForBaseImage = node.name
 
    try {
      const rsp: any = await getCameraBaseImage({ id: node.id })
 
      if (rsp && rsp.data) {
        this.gb28181CameraBaseImage = rsp.data
      }
    } catch (error) {
      console.log(error)
    }
 
    this.baseImageLoading = false
  }
}