/** * 公共工具函数 */ /** * 生成随机浮点数参数(rfm) * 用于接口请求的防重复参数 * @returns {number} 0到1之间的随机浮点数 */ export function generateRfm() { return Math.random(); } /** * 获取当前时间戳 * @returns {number} 当前时间戳 */ export function getTimestamp() { return Date.now(); } /** * 生成带时间戳的随机参数 * @returns {number} 带时间戳的随机数 */ export function generateTimestampRfm() { return Math.random() + Date.now() / 1000000; } /** * 构建通用请求参数 * @param {Object} customParams - 自定义参数 * @returns {Object} 包含通用参数的对象 */ export function buildCommonParams(customParams = {}) { return { rfm: generateRfm(), ...customParams }; } /* * 转换成GB * */ export function convertToGB(value, type) { if (type === 'flowSize') { // flowSize 单位是 MB,转换为 GB return (value / 1024).toFixed(2); } else if (type === 'totalBytesCnt') { // totalBytesCnt 单位是 MB,转换为 GB return (value / 1024).toFixed(2); } return value; } /** * 秒转时间字符串 * @param {number} seconds 秒 * @returns {string} HH:mm:ss */ export function formatSecondsToTime(seconds) { if (!seconds && seconds !== 0) return '00:00:00'; const h = Math.floor(seconds / 3600); const m = Math.floor((seconds % 3600) / 60); const s = seconds % 60; return ( String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0') + ':' + String(s).padStart(2, '0') ); } /** * 根据信号强度(dBm)返回信号等级与体验描述 * @param {number} dbm 信号强度(负数,如 -65) * @returns {string} */ export function getSignalText(dbm) { if (!dbm) { return '未知' } if (dbm >= -50) { return '极强' } if (dbm >= -70) { return '良好' } return '一般' }