export function formatDateTime(template: string = 'YYYY-MM-DD HH:mm:ss', timestamp?: number): string { const now = timestamp ? new Date(timestamp) : new Date() const tokens: Record = { YYYY: String(now.getFullYear()), MM: String(now.getMonth() + 1).padStart(2, '0'), DD: String(now.getDate()).padStart(2, '0'), HH: String(now.getHours()).padStart(2, '0'), mm: String(now.getMinutes()).padStart(2, '0'), ss: String(now.getSeconds()).padStart(2, '0'), } return template.replace(/YYYY|MM|DD|HH|mm|ss/g, token => tokens[token]!) } export function roundNumber(num: string | number, digits: number) { num = Number(num) if (!num) { return num } digits = digits || 0 return Number(num.toFixed(digits)) } export const deviceStateMap = >{ idle: '空闲', init: '初始化设备', preheat: '预热中', disinfection: '消毒中', degradation: '降解中', finished: '消毒完成', dehumidificationBeforeDisinfection: '消毒前除湿', dehumidificationAfterDisinfection: '消毒后除湿', emptyLineLiquid: '排空管路', } export const sealStateMap = >{ idle: '空闲', initDevice: '初始化', iniflating: '打压中', leakText: '检漏中', stopping: '停止中', } export const compareJSON = (obj1: any, obj2: any) => { const result: Record = {} // 遍历obj1的所有属性 for (const key in obj1) { if (Object.prototype.hasOwnProperty.call(obj1, key)) { if (!Object.prototype.hasOwnProperty.call(obj2, key)) { // obj2中不存在该属性 result[key] = { obj1: obj1[key], obj2: undefined } } else if (typeof obj1[key] !== typeof obj2[key]) { // 类型不同 result[key] = { obj1: obj1[key], obj2: obj2[key] } } else if (typeof obj1[key] === 'object' && obj1[key] !== null && obj2[key] !== null) { // 递归比较对象 const nestedDiff = compareJSON(obj1[key], obj2[key]) if (Object.keys(nestedDiff).length > 0) { result[key] = nestedDiff } } else if (obj1[key] !== obj2[key]) { // 基本类型值不同 result[key] = { oldVal: obj1[key], newVal: obj2[key] } } } } return result } let isFullscreen = false export const openFullscreen = () => { const elem = document.documentElement if (!isFullscreen) { isFullscreen = true elem.requestFullscreen() } else { isFullscreen = false document.exitFullscreen() } } export const convertValuesToString = (jsonObj: Record, exName: string) => { const result: Record = {} for (const [key, value] of Object.entries(jsonObj)) { if (key !== exName) { result[key] = String(value) } else { result[key] = value } } return result } function isNumber(str: string | number) { // 先转换为数字,再用 isNaN 判断是否为 NaN return !Number.isNaN(Number(str)) && Number.isFinite(Number(str)) } export const convertValuesToInt = (jsonObj: Record) => { const result: Record = {} for (const [key, value] of Object.entries(jsonObj)) { if (key !== 'name' && isNumber(value)) { result[key] = Number(value) } else { result[key] = value } } return result } export const DEVICE_STATES = { IDLE: 'idle', // 空闲 INIT: 'init', // 初始化 PREHEAT: 'preheat', // 预热 DISINFECTION: 'disinfection', // 消毒 DEGRADATION: 'degradation', // 降解 FINISHED: 'finished', // 结束 DEHUMIDIFICATION_BEFORE: 'dehumidification_before_disinfection', // 消毒前除湿 DEHUMIDIFICATION_AFTER: 'dehumidification_after_disinfection', // 消毒后除湿 EMPTY_LINE: 'empty_liquid_from_the_line', // 清空管路 }