zhangzengfei
2020-09-17 68fe1629ea048cf3b806b700f9934f990aa4bfdf
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
import {
  getLocalCameraTree,
  getGB28181CameraTree,
  addAreaTreeData,
  delAreaTreeData,
  updateAreaTreeData,
  refreshGB28181Tree
} from "@/api/area";
 
export default class TreeDataPool {
  public openeds: Array<boolean>;
  public treeData: Array<object>;
  public gb28181Data: Array<object>;
  public treeDataPure: Array<object>;
  public gb28181DataPure: 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 showTreeBox: boolean;
  public selectedNodes: Array<string>;
  public selectedNode: any;
  // 记录当前操作的树
  public treeType: string;
 
  constructor() {
    this.openeds = [true, true, false];
    this.treeData = [];
    this.gb28181Data = [];
    this.treeDataPure = [];
    this.gb28181DataPure = [];
    this.videoArr = new Array(8);
    this.searchCamType = 0;
    this.searchInput = "";
    this.activeVideoIndex = 0;
    this.activeVideoId = "";
    this.activeForceChoose = false;
    this.showTreeBox = false;
    this.readonly = true;
    this.gbReadonly = true;
    this.multiple = false;
    this.selectedNodes = [];
    this.selectedNode = {};
    this.treeType = ""
  }
 
  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);
      }
      if (node.children) {
        node.children.forEach((n: any) => {
          nodeFilter(n);
        });
      }
    }
    this.treeData.forEach((n: any) => {
      nodeFilter(n);
    });
 
    this.gb28181Data.forEach((n: any) => {
      nodeFilter(n);
    });
  }
 
  getParent(id: string): 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;
    }
 
    nodeFilter(this.treeData);
 
    return parent;
  }
 
  clean() {
    this.treeData = JSON.parse(JSON.stringify(this.treeDataPure));
    this.gb28181Data = JSON.parse(JSON.stringify(this.gb28181DataPure));
    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));
    }
  }
 
  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) {
        this.sortTreeData(n.children)
      }
    })
  }
 
  async fetchLocalTree() {
    const rsp: any = await getLocalCameraTree({
      searchType: this.searchCamType,
      cameraName: this.searchInput,
      isPlatform: 1
    });
 
    if (rsp && rsp.success) {
      this.treeData = rsp.data ? rsp.data : []
      if (this.treeData && this.treeData.length > 0) {
        this.sortTreeData(this.treeData)
      }
 
      // 设置禁止拖拽摄像机到摄像机节点
      this.treeDataPure = JSON.parse(JSON.stringify(this.treeData));
    }
  }
 
  async fetchGbTree() {
    const rsp: any = await getGB28181CameraTree({
      searchType: this.searchCamType,
      cameraName: this.searchInput
    });
 
    if (rsp && rsp.success) {
      this.gb28181Data = rsp.data
      if (this.gb28181Data && this.gb28181Data.length > 0) {
        this.sortTreeData(this.gb28181Data)
      }
 
      this.gb28181DataPure = JSON.parse(JSON.stringify(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) {
    await updateAreaTreeData({
      id: id,
      name: name,
      parentId: this.getParent(id),
      alias: alias
    });
 
    this.fetchTreeData();
  }
 
  async refreshGB28181() {
    await refreshGB28181Tree()
    this.fetchGbTree()
  }
}