1,使用声明文件
declare namespace DeviceTree {
interface firstFloor {
province: string, // 省份
citys: DeviceTree.secondFloor[]
}
interface secondFloor {
city: string, // 城市
voltages: DeviceTree.thirdFloor[]
lines: DeviceTree.fourthFloor[]
}
interface thirdFloor {
voltage: string, // 电压
lines: DeviceTree.fourthFloor[]
}
interface fourthFloor {
lineId: string,
name: string, // 线路
tower: DeviceTree.fifthFloor[],
desc: any // 描述
endLocation: any // 结束位置
startLocation: any // 起始位置
title:string
}
// interface fifthFloor {
// towerName: string // 杆塔
// devices: DeviceTree.sixthFloor[]
// }
interface sixthFloor {
devices: DeviceTree.sixthFloor[]
deviceId: string,
name: string, // 设备名称
type: any
channelModelList:DeviceTree.seventhFloor[]
}
interface seventhFloor {
deviceId: string,
channel: number, // 通道
// type: any
}
interface antTree {
title: string,
key: string,
children: DeviceTree.antTree[],
parentKey?: stringp[]
[propname: string]: any
}
}
2,这是写的树文件
getDeviceTree().then((res: any) => {
const tree = this.buildFirstFloorData(res.data,false);
this.$store.commit("SET_DEVICE_TREE_DATA", tree);
const channelTree = this.buildFirstFloorData(res.data,true);
this.$store.commit("SET_DEVICE_CHANNEL_TREE_DATA", channelTree);
});
}
// 构建第一层设备树结构(在buildFifthFloorData层使用嵌套获取数据后,会丢失几个devices数组里面数据)
buildFirstFloorData(
data: DeviceTree.firstFloor[],
channelShow:boolean,
treeData: DeviceTree.antTree[] = []
) {
for (let i = 0; i < data.length; i++) {
var provinces=data[i].province.split("(")[0]
if(/.*[\u4e00-\u9fa5]+.*$/.test(provinces)){
provinces=data[i].province
}else{
provinces=Base64.decode(data[i].province.split("(")[0])+"("+data[i].province.split("(")[1]
}
const onlyId = createdOnlyId();
const treeNode: DeviceTree.antTree = {
title: provinces, // 省份
key: onlyId,
children: [],
scopedSlots: {
icon: "icon",
},
icon: require("@/assets/device-tree/province-icon.png"),
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
this.buildSecondFloorData(data[i].citys, treeData[i].children, [onlyId],channelShow);
}
this.commitToVuex();
return treeData;
}
// 把数据存到vuex
commitToVuex() {
this.$store.commit("SET_TREE_NODE_LIST", this.treeNode);
this.$store.commit("SET_DEVICE_LIST", this.deviceList);
this.$store.commit("SET_LINE_LIST", this.lineList);
}
// 构建第二层设备树结构
buildSecondFloorData(
data: DeviceTree.secondFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
channelShow:boolean
) {
for (let i = 0; i < data.length; i++) {
var citys=data[i].city.split("(")[0]
if(/.*[\u4e00-\u9fa5]+.*$/.test(citys)){
citys=data[i].city
}else{
citys=Base64.decode(data[i].city.split("(")[0])+"("+data[i].city.split("(")[1]
}
const onlyId = createdOnlyId();
const treeNode: DeviceTree.antTree = {
title:citys, // 城市
key: onlyId,
children: [],
scopedSlots: {
icon: "icon",
},
icon: require("@/assets/device-tree/city-icon.png"),
parentKey,
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
this.buildThirdFloorData(
data[i].voltages,
treeData[i].children,
parentKey.concat(onlyId),
channelShow
);
}
}
// 构建第三层设备树结构
buildThirdFloorData(
data: DeviceTree.thirdFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
channelShow:boolean
) {
for (let i = 0; i < data.length; i++) {
const onlyId = createdOnlyId();
const treeNode: DeviceTree.antTree = {
title: data[i].voltage, // 电压
key: onlyId,
children: [],
parentKey,
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
this.buildFourthFloorData(
data[i].lines,
treeData[i].children,
parentKey.concat(onlyId),
channelShow
);
}
}
// 构建第四层设备树结构
buildFourthFloorData(
data: DeviceTree.fourthFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
channelShow:boolean
) {
for (let i = 0; i < data.length; i++) {
const onlyId = createdOnlyId();
const treeNode: DeviceTree.antTree = {
title: data[i].name, // 线路名称
key: onlyId,
children: [],
scopedSlots: {
icon: "icon"
},
lineIcon: true,
lineId: data[i].lineId, // 线路id
desc: data[i].desc,
startLocation:data[i].startLocation ,//data[i].startLocation
endLocation: data[i].endLocation,//data[i].endLocation
parentKey,
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
this.lineList = this.lineList.concat(treeNode); // 把线路添加到线路列表中
// for (let j = 0; j < data[i].tower.length; j++) {
this.buildFifthFloorData(
data[i].tower,
treeData[i].children,
parentKey.concat(onlyId),
channelShow
);
// }
}
}
// 构建第五层设备树结构
buildFifthFloorData(
data: DeviceTree.sixthFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
channelShow:boolean
) {
for (let i = 0; i < data.length; i++) {
//在这里试下循环看看可不可以直接获取设备显示
for (let j = 0; j < data[i].devices.length; j++) {
var state = data[i].devices[j]["onlineState"]==4 ? 2: 1;
const onlyId = createdOnlyId();
const treeNode: DeviceTree.antTree = {
title: data[i].devices[j].name, // 杆塔名称
key: createdOnlyId(),
children: [],
scopedSlots: {
icon: "icon",
},
icon: require(`@/assets/device-tree/device-icon-${data[i].devices[j].type}-${state}.png`),
parentKey,
};
treeData[j] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
this.buildSeventhFloorData(
data[i].devices[j].channelModelList,
treeData[j].children,
parentKey.concat(onlyId),
data[i].devices[j].name
);
}
}
}
// 构建第六层设备树结构
/* buildSixthFloorData(
data: DeviceTree.sixthFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
channelShow:boolean
) {
for (let i = 0; i < data.length; i++) {
const onlyId = createdOnlyId();
var state = data[i]["onlineState"]==4 ? 2: 1;
const treeNode: DeviceTree.antTree = {
title: data[i].name, // 杆塔名称
key: onlyId,
children: [],
deviceId: data[i].deviceId,
scopedSlots: {
icon: "icon",
},
icon: require(`@/assets/device-tree/device-icon-${data[i].type}-${state}.png`),
parentKey,
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
if(channelShow){
this.deviceList = this.deviceList.concat(treeNode); // 把设备添加到设备列表中
// console.log( this.deviceList)
this.buildSeventhFloorData(
data[i].channelModelList,
treeData[i].children,
parentKey.concat(onlyId),
data[i].name
);
}
}
}*/
// 构建第七层设备树结构
buildSeventhFloorData(
data: DeviceTree.seventhFloor[],
treeData: DeviceTree.antTree[],
parentKey: string[],
deviceName:String,
) {
for (let i = 0; i < data.length; i++) {
const treeNode: DeviceTree.antTree = {
title: deviceName+"-"+String(data[i].channel), // 通道
deviceId: data[i].deviceId,
channel:data[i].channel,
key: createdOnlyId(),
children: [],
parentKey,
};
treeData[i] = treeNode;
this.treeNode.push(treeNode); // 把节点添加到树节点列表中
}