123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /**
- * 格式化秒数为年、月、日、时、分、秒的字符串表示
- * @param {number} seconds - 秒数
- * @returns {string} 格式化后的字符串,例如:"1年2月3日4时5分6秒"
- * @example
- * formatSeconds(30); // 30秒
- * formatSeconds(3600); // 1时1分
- */
- export function formatSeconds(seconds: number) {
- // 处理 NaN 和无效输入
- if (isNaN(seconds) || seconds === null || seconds === undefined) {
- return ''
- }
- // 处理边界情况:0秒
- if (seconds === 0) return '0秒'
- // 确保输入为整数
- seconds = Math.floor(Number(seconds))
- // 计算各时间单位
- const totalDays = Math.floor(seconds / 86400)
- const years = Math.floor(totalDays / 365)
- const months = Math.floor((totalDays % 365) / 30)
- const days = (totalDays % 365) % 30
- const hours = Math.floor(seconds / 3600) % 24
- const minutes = Math.floor((seconds % 3600) / 60)
- const secs = seconds % 60
- // 构建结果数组(自动过滤0值)
- const parts = []
- if (years > 0) parts.push(`${years}年`)
- if (months > 0) parts.push(`${months}月`)
- if (days > 0) {
- parts.push(`${days}日`)
- parts.push(`${days}天`)
- }
- if (hours > 0) parts.push(`${hours}时`)
- if (minutes > 0) parts.push(`${minutes}分`)
- if (secs > 0) parts.push(`${secs}秒`)
- // 处理所有单位都是0的情况(当seconds为小数时可能发生)
- return parts.length > 0 ? parts.join('') : '0秒'
- }
- /**
- *
- * @param d 日期对象
- * @returns 格式化后的日期时间字符串,例如:"2025-01-01 12:00:00"
- */
- export function formatDateTime(d: Date) {
- const pad = (n: number) => String(n).padStart(2, '0')
- return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
- }
- /**
- * 获取坐标位置
- */
- /**
- * 获取坐标位置
- * @note 雷达的检测范围
- * @param xstart x 开始坐标
- * @param xend x 结束坐标
- * @param ystart y 开始坐标
- * @param yend y 结束坐标
- * @note 配置家具时,点击的家具元素的坐标
- * @param offsetLeft 元素基于父容器的X坐标
- * @param offsetTop 元素基于父容器的Y坐标
- */
- export const getOriginPosition = (
- [xstart, xend, ystart, yend]: number[],
- [offsetLeft, offsetTop]: number[]
- ) => {
- // 容器宽高
- const containerWidth = Math.abs(xstart) + Math.abs(xend)
- const containerHeight = Math.abs(ystart) + Math.abs(yend)
- // 原点在容器中的坐标
- const originX = Math.abs(xstart)
- const originY = Math.abs(yend)
- // 元素基于父容器的偏移量
- const offsetX = offsetLeft ?? 0
- const offsetY = offsetTop ?? 0
- // 元素基于原点的偏移量
- const originOffsetX = offsetX - originX
- const originOffsetY = originY - offsetY
- // 雷达尺寸
- const radarWidth = 20
- const radarHeight = 20
- // 雷达基于原点的偏移量
- const radarX = Math.round(originX - radarWidth / 2)
- const radarY = Math.round(originY - radarHeight / 2)
- const data = {
- width: containerWidth, // 容器宽度
- height: containerHeight, // 容器高度
- originX: Math.round(originX), // 原点X坐标
- originY: Math.round(originY), // 原点Y坐标
- offsetX: Math.round(offsetX), // 元素基于父容器的偏移量 X坐标
- offsetY: Math.round(offsetY), // 元素基于父容器的偏移量 Y坐标
- originOffsetX: Math.round(originOffsetX), // 元素基于原点的偏移量 X坐标
- originOffsetY: Math.round(originOffsetY), // 元素基于原点的偏移量 Y坐标
- radarX, // 雷达X坐标
- radarY, // 雷达Y坐标
- radarWidth, // 雷达宽度
- radarHeight, // 雷达高度
- }
- return data
- }
|