const config = require('./config.js'); let setIntervalKey; function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } var util = { formatTime: function(date) { var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber) .join( ':') }, formatCNTime: function(val) { const year = val.getFullYear(); const month = val.getMonth() + 1; const day = val.getDate(); const hour = val.getHours(); const minute = val.getMinutes(); const second = val.getSeconds(); return year + '年' + (month > 9 ? month : '0' + month) + '月' + day + '日' + hour + '时' + (minute > 9 ? minute : '0' + minute) + '分' + second + '秒'; }, formatCNTime1: function(val) { const year = val.getFullYear(); const month = val.getMonth() + 1; const day = val.getDate(); const hour = val.getHours(); const minute = val.getMinutes(); const second = val.getSeconds(); return year + '年' + (month > 9 ? month : '0' + month) + '月' + (day > 9 ? day : '0' + day) + '日'; }, buttonClicked(self, time) { self.buttonClicked = false; setTimeout(function() { self.buttonClicked = true; }, time) }, formatTodayTime: function(val) { if (typeof val !== 'object') { console.log('时间格式不对,不予转换') return val } const year = val.getFullYear(); const month = val.getMonth() + 1; const day = val.getDate(); const hour = val.getHours(); const minute = val.getMinutes(); const second = val.getSeconds(); if (month == new Date().getMonth() + 1 && day + 1 == new Date().getDate()) { return '昨天' + hour + ':' + (minute > 9 ? minute : '0' + minute); } else if (month == new Date().getMonth() + 1 && day == new Date().getDate()) { return hour + ':' + (minute > 9 ? minute : '0' + minute) + ':' + (second > 9 ? second : '0' + second); } else { return year + '-' + (month > 9 ? month : '0' + month) + '-' + (day > 9 ? day : '0' + day) + ' ' + hour + ':' + ( minute > 9 ? minute : '0' + minute) + ':' + (second > 9 ? second : '0' + second); } }, formatMinuteTime: function(val) { const year = val.getFullYear(); const month = val.getMonth() + 1; const day = val.getDate(); const hour = val.getHours(); const minute = val.getMinutes(); const second = val.getSeconds(); return year + '.' + (month > 9 ? month : '0' + month) + '.' + (day > 9 ? day : '0' + day) + ' ' + hour + ':' + ( minute > 9 ? minute : '0' + minute) }, formatSucTime: function(val) { const year = val.getFullYear(); const month = val.getMonth() + 1; const day = val.getDate(); const hour = val.getHours(); const minute = val.getMinutes(); if (month == new Date().getMonth() + 1 && day + 1 == new Date().getDate()) { return '昨天' + hour + ':' + (minute > 9 ? minute : '0' + minute); } else if (month == new Date().getMonth() + 1 && day == new Date().getDate()) { return hour + ':' + (minute > 9 ? minute : '0' + minute); } else { return year + '-' + (month > 9 ? month : '0' + month) + '-' + day + ' ' + hour + ':' + (minute > 9 ? minute : '0' + minute); } }, //此时此刻日期转化 timesData: function(timestamp) { var date = new Date(timestamp); var Y = date.getFullYear() + '/'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/'; var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '; // var h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':'; // var m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()); return Y + M + D; }, //此时此刻日期转化格式 dateFormat: function(timestamp) { var date = new Date(timestamp); var Y = date.getFullYear() + '年'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月'; var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + '日 '; var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'; var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':'; var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds()); return Y + M + D + h + m + s; }, //此时此刻日期转化格式 dateFormats: function(timestamp) { var date = new Date(timestamp); var Y = date.getFullYear() + '年'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月'; var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + '日 '; return Y + M + D; }, //此时此刻时间转化 timestampToTime: function(timestamp) { var date = new Date(timestamp); var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'; var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()); return h + m; }, formatDate: function(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() } for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + '' fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str)) } } return fmt }, padLeftZero: function(str) { return ('00' + str).substr(str.length) }, hexToRgba(hex, opacity) { if (!hex || !opacity) { return hex } var values; let RGBA; if (hex.includes('#') && hex.length == 7) { RGBA = "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")"; } else if (hex.includes('rgb(')) { values = hex .replace(/rgb?\(/, '') .replace(/\)/, '') .replace(/[\s+]/g, '') .split(',') return `rgba(${values[0]},${values[1]},${values[2]},${opacity})` } else if (hex.includes('rgba(')) { values = hex .replace(/rgba?\(/, '') .replace(/\)/, '') .replace(/[\s+]/g, '') .split(',') return `rgba(${values[0]},${values[1]},${values[2]},${opacity})` } else { return hex } return RGBA }, deepEqual(obj1, obj2) { if (obj1 === obj2) { return true; // 引用相同或都为 null } if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) { return false; // 其中一个不是对象 } const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length !== keys2.length) { return false; // 属性数量不同 } for (const key of keys1) { if (!keys2.includes(key) || !this.deepEqual(obj1[key], obj2[key])) { return false; // 属性名或属性值不同 } } return true; }, getHexColor(color) { var values = color .replace(/rgba?\(/, '') .replace(/\)/, '') .replace(/[\s+]/g, '') .split(',') var a = parseFloat(values[3] || 1), r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255), g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255), b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255) return '#' + ('0' + r.toString(16)).slice(-2) + ('0' + g.toString(16)).slice(-2) + ('0' + b.toString(16)).slice(-2) }, // websocket 返回的数据转义成object类型 parseWSData(data) { if (data.startsWith("a[")) { let endLength = data.length - 6; let textData = data.substr(3, endLength); let request = textData.split("\\n\\n"); let headerBody = request[0]; let body = request[1]; let headerArray = headerBody.split("\\n"); let header = {}; for (let i = 1; i < headerArray.length; i++) { let body = headerArray[i]; let resultBody = body.split(":"); header[resultBody[0]] = resultBody[1]; } let result = {}; result["command"] = headerArray[0]; result["header"] = header; if (body) { try { // let bd = body.replaceAll(/\\+/ig, ""); // let bd = body.replace(/\\"/g, '"') // 将 \\" 替换为 " // .replace(/\\n/g, '\n'); // 将 \\n 替换为实际的换行符 // result["body"] = JSON.parse(bd); let cleanBody = body .replace(/\\\\/g, '\\') .replace(/\\"/g, '"'); // 使用更精确的正则表达式匹配 const eventMatch = cleanBody.match(/"event"\s*:\s*"([^"]+)"/); let dataMatch = cleanBody.replaceAll(/\\u([\dA-Fa-f]{4})/g, (_, p1) => String.fromCharCode(parseInt(p1, 16)) ).match(/"data"\s*:\s*"([\s\S]*?)(?="\s*})/); result["body"] = { event: eventMatch ? eventMatch[1] : '', data: dataMatch ? dataMatch[1] .replace(/\\n/g, '\n') // 处理换行符 .replace(/\\"/g, '"') // 处理引号 .replace(/\\\\/g, '\\') // 处理反斜杠 .replace(/\\t/g, '\t') // 处理制表符 .replace(/\\r/g, '') // 移除回车符 .replace(/\\u000/g, '') // 移除 \u000 .trim() : '' }; } catch (e) { result["body"] = body; } } return result; } return data; }, converToUrl(requestParams) { let params = []; Object.entries(requestParams).forEach(([key, value]) => { let param = key + '=' + value; params.push(param); }); return '?' + params.join('&'); }, /** * 解析页面路由参数 * @param name * @returns {null} */ getQueryString(url, name) { let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); let arr = url.split('?'); let result = null; arr.forEach((item, index) => { let res = item.match(reg); if (res != null) { result = unescape(res[2]); } }) return result }, /** * 数字取整 * 例如:145返回100 452返回400 * @param num * @returns {number} */ getMaxNum(num) { let str = num.toString(); let len = str.length; return Math.floor(num / Math.pow(10, len - 1)) * Math.pow(10, len - 1); }, }; module.exports = util;