This commit is contained in:
2025-10-20 15:15:43 +08:00
parent fdc568753c
commit 9c6467176c

View File

@@ -7,30 +7,26 @@ class DeviceService {
*/
async getDevices() {
try {
// 1. 分别、正确地调用接口获取区域主控和所有设备
const [areaControllersResponse, allDevicesResponse] = await Promise.all([
AreaControllerApi.list(), // 现在调用 GET /api/v1/area-controllers
DeviceApi.list() // 现在调用 GET /api/v1/devices
// 1. 并行发起两个独立的API请求一个获取区域主控一个获取普通设备
const [areaControllersResponse, devicesResponse] = await Promise.all([
AreaControllerApi.list(), // 调用 GET /api/v1/area-controllers
DeviceApi.list() // 调用 GET /api/v1/devices
]);
// 2. 处理区域主控数据
// 2. 处理区域主控数据,并添加前端所需的'type'标识
const areaControllers = (areaControllersResponse.data || []).map(controller => ({
...controller,
type: 'area_controller' // 添加类型标识,用于前端区分
type: 'area_controller'
}));
// 3. 处理普通设备数据
const devices = (allDevicesResponse.data || [])
// 注意:后端的 /api/v1/devices 可能也包含了区域主控,前端需要做筛选
.filter(device => !areaControllers.some(ac => ac.id === device.id && device.type === 'area_controller'))
.map(device => ({
...device,
type: 'device', // 添加类型标识
// 使用 area_controller_id 作为 parent_id 以构建树形结构
parent_id: device.area_controller_id
}));
// 3. 处理普通设备数据,并添加'type'和'parent_id'以构建树形结构
const devices = (devicesResponse.data || []).map(device => ({
...device,
type: 'device',
parent_id: device.area_controller_id
}));
// 4. 合并数据,返回给组件
// 4. 合并两份数据,形成完整的列表,返回给UI组件
return [...areaControllers, ...devices];
} catch (error) {
console.error('获取设备列表失败:', error);