congming_huose-apk/common/utils/deviceRoomOverlay.js

46 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 设备列表接口可能不返回或延迟返回 roomName修改房间后写入本地覆盖列表拉取后合并展示 */
const STORAGE_KEY = 'deviceRoomOverlay'
export function patchDeviceRoomInStorage(deviceId, roomId, roomName) {
if (deviceId === undefined || deviceId === null || deviceId === '') return
try {
let map = {}
const raw = uni.getStorageSync(STORAGE_KEY)
if (raw) {
map = typeof raw === 'string' ? JSON.parse(raw) : raw
}
if (!map || typeof map !== 'object') map = {}
map[String(deviceId)] = {
roomId,
roomName: roomName != null ? String(roomName) : '',
ts: Date.now()
}
uni.setStorageSync(STORAGE_KEY, JSON.stringify(map))
} catch (e) {}
}
export function readDeviceRoomOverlayMap() {
try {
const raw = uni.getStorageSync(STORAGE_KEY)
if (!raw) return {}
return typeof raw === 'string' ? JSON.parse(raw) : raw
} catch (e) {
return {}
}
}
/** 就地合并到行数据,保证首页能显示刚改过的房间名 */
export function mergeDeviceRoomOverlayIntoRows(rows) {
if (!rows || !rows.length) return rows
const map = readDeviceRoomOverlayMap()
rows.forEach(d => {
if (!d) return
const sid = d.id != null ? String(d.id) : ''
if (!sid || !map[sid]) return
const o = map[sid]
if (o.roomName) d.roomName = o.roomName
if (o.roomId !== undefined && o.roomId !== null) d.roomId = o.roomId
})
return rows
}