feat:蓝牙绑定和在线客服
This commit is contained in:
@@ -1,54 +1,44 @@
|
||||
import type { AppDeviceVo, DeviceListItem, RoomTab } from './device.types'
|
||||
import type { RoomOption } from '@/types/location'
|
||||
import { sameId } from '@/utils/device-id'
|
||||
|
||||
/** 位置 ID → 房间名(后端暂无 locationName 时的本地映射) */
|
||||
const LOCATION_NAME_MAP: Record<number, string> = {
|
||||
1: '客厅',
|
||||
2: '主卧',
|
||||
3: '卧室',
|
||||
4: '厨房',
|
||||
5: '卫生间',
|
||||
6: '书房',
|
||||
}
|
||||
export type LocationMap = Map<string, RoomOption[]>
|
||||
|
||||
export function resolveLocationName(locationId?: number): string {
|
||||
function resolveLocationName(
|
||||
locationId?: string | number,
|
||||
locations: RoomOption[] = [],
|
||||
): string {
|
||||
if (locationId == null) return '未分配'
|
||||
return LOCATION_NAME_MAP[locationId] ?? `位置${locationId}`
|
||||
const matched = locations.find((item) => sameId(item.locationId, locationId))
|
||||
return matched?.name ?? '未分配'
|
||||
}
|
||||
|
||||
export function mapDeviceItem(device: AppDeviceVo): DeviceListItem {
|
||||
export function mapDeviceItem(
|
||||
device: AppDeviceVo,
|
||||
locationMap: LocationMap,
|
||||
): DeviceListItem {
|
||||
const homeKey = device.homeId != null ? String(device.homeId) : ''
|
||||
const locations = homeKey ? locationMap.get(homeKey) ?? [] : []
|
||||
return {
|
||||
...device,
|
||||
locationName: resolveLocationName(device.locationId),
|
||||
locationName: resolveLocationName(device.locationId, locations),
|
||||
isOnline: device.status === '1',
|
||||
isCharging: device.charging === '1',
|
||||
}
|
||||
}
|
||||
|
||||
/** 根据家庭房间位置列表构建首页 Tab(与所在房间页数据源一致) */
|
||||
export function buildRoomTabs(
|
||||
devices: DeviceListItem[],
|
||||
customRooms: RoomTab[] = [],
|
||||
locations: { locationId: string | number; name: string }[],
|
||||
): RoomTab[] {
|
||||
const tabs: RoomTab[] = [{ id: 'all', name: '全部' }]
|
||||
const seen = new Set<string>()
|
||||
|
||||
for (const device of devices) {
|
||||
if (device.locationId == null) continue
|
||||
const id = String(device.locationId)
|
||||
if (seen.has(id)) continue
|
||||
seen.add(id)
|
||||
for (const location of locations) {
|
||||
tabs.push({
|
||||
id,
|
||||
name: device.locationName,
|
||||
locationId: device.locationId,
|
||||
id: String(location.locationId),
|
||||
name: location.name,
|
||||
locationId: location.locationId,
|
||||
})
|
||||
}
|
||||
|
||||
for (const room of customRooms) {
|
||||
if (seen.has(room.id)) continue
|
||||
seen.add(room.id)
|
||||
tabs.push(room)
|
||||
}
|
||||
|
||||
return tabs
|
||||
}
|
||||
|
||||
@@ -57,9 +47,7 @@ export function filterDevicesByRoom(
|
||||
activeRoomId: string,
|
||||
): DeviceListItem[] {
|
||||
if (activeRoomId === 'all') return devices
|
||||
const locationId = Number(activeRoomId)
|
||||
if (Number.isNaN(locationId)) return devices
|
||||
return devices.filter((item) => item.locationId === locationId)
|
||||
return devices.filter((item) => sameId(item.locationId, activeRoomId))
|
||||
}
|
||||
|
||||
export function countOnlineDevices(devices: DeviceListItem[]): number {
|
||||
|
||||
Reference in New Issue
Block a user