index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * 格式化秒数为年、月、日、时、分、秒的字符串表示
  3. * @param {number} seconds - 秒数
  4. * @returns {string} 格式化后的字符串,例如:"1年2月3日4时5分6秒"
  5. * @example
  6. * formatSeconds(30); // 30秒
  7. * formatSeconds(3600); // 1时1分
  8. */
  9. export function formatSeconds(seconds: number) {
  10. // 处理 NaN 和无效输入
  11. if (isNaN(seconds) || seconds === null || seconds === undefined) {
  12. return ''
  13. }
  14. // 处理边界情况:0秒
  15. if (seconds === 0) return '0秒'
  16. // 确保输入为整数
  17. seconds = Math.floor(Number(seconds))
  18. // 计算各时间单位
  19. const totalDays = Math.floor(seconds / 86400)
  20. const years = Math.floor(totalDays / 365)
  21. const months = Math.floor((totalDays % 365) / 30)
  22. const days = (totalDays % 365) % 30
  23. const hours = Math.floor(seconds / 3600) % 24
  24. const minutes = Math.floor((seconds % 3600) / 60)
  25. const secs = seconds % 60
  26. // 构建结果数组(自动过滤0值)
  27. const parts = []
  28. if (years > 0) parts.push(`${years}年`)
  29. if (months > 0) parts.push(`${months}月`)
  30. if (days > 0) {
  31. parts.push(`${days}日`)
  32. parts.push(`${days}天`)
  33. }
  34. if (hours > 0) parts.push(`${hours}时`)
  35. if (minutes > 0) parts.push(`${minutes}分`)
  36. if (secs > 0) parts.push(`${secs}秒`)
  37. // 处理所有单位都是0的情况(当seconds为小数时可能发生)
  38. return parts.length > 0 ? parts.join('') : '0秒'
  39. }
  40. /**
  41. *
  42. * @param d 日期对象
  43. * @returns 格式化后的日期时间字符串,例如:"2025-01-01 12:00:00"
  44. */
  45. export function formatDateTime(d: Date) {
  46. const pad = (n: number) => String(n).padStart(2, '0')
  47. return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
  48. }
  49. /**
  50. * 获取坐标位置
  51. */
  52. /**
  53. * 获取坐标位置
  54. * @note 雷达的检测范围
  55. * @param xstart x 开始坐标
  56. * @param xend x 结束坐标
  57. * @param ystart y 开始坐标
  58. * @param yend y 结束坐标
  59. * @note 配置家具时,点击的家具元素的坐标
  60. * @param offsetLeft 元素基于父容器的X坐标
  61. * @param offsetTop 元素基于父容器的Y坐标
  62. */
  63. export const getOriginPosition = (
  64. [xstart, xend, ystart, yend]: number[],
  65. [offsetLeft, offsetTop]: number[]
  66. ) => {
  67. // 容器宽高
  68. const containerWidth = Math.abs(xstart) + Math.abs(xend)
  69. const containerHeight = Math.abs(ystart) + Math.abs(yend)
  70. // 原点在容器中的坐标
  71. const originX = Math.abs(xstart)
  72. const originY = Math.abs(yend)
  73. // 元素基于父容器的偏移量
  74. const offsetX = offsetLeft ?? 0
  75. const offsetY = offsetTop ?? 0
  76. // 元素基于原点的偏移量
  77. const originOffsetX = offsetX - originX
  78. const originOffsetY = originY - offsetY
  79. // 雷达尺寸
  80. const radarWidth = 20
  81. const radarHeight = 20
  82. // 雷达基于原点的偏移量
  83. const radarX = Math.round(originX - radarWidth / 2)
  84. const radarY = Math.round(originY - radarHeight / 2)
  85. const data = {
  86. width: containerWidth, // 容器宽度
  87. height: containerHeight, // 容器高度
  88. originX: Math.round(originX), // 原点X坐标
  89. originY: Math.round(originY), // 原点Y坐标
  90. offsetX: Math.round(offsetX), // 元素基于父容器的偏移量 X坐标
  91. offsetY: Math.round(offsetY), // 元素基于父容器的偏移量 Y坐标
  92. originOffsetX: Math.round(originOffsetX), // 元素基于原点的偏移量 X坐标
  93. originOffsetY: Math.round(originOffsetY), // 元素基于原点的偏移量 Y坐标
  94. radarX, // 雷达X坐标
  95. radarY, // 雷达Y坐标
  96. radarWidth, // 雷达宽度
  97. radarHeight, // 雷达高度
  98. }
  99. return data
  100. }