utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * 通用工具库(原生 JS 版),替代 common/tools.js 的常用能力
  3. */
  4. const Tools = {
  5. getQueryParam(name) {
  6. const params = new URLSearchParams(window.location.search);
  7. let val = params.get(name);
  8. if (!val && window.location.hash) {
  9. const hash = window.location.hash;
  10. const idx = hash.indexOf('?');
  11. if (idx >= 0) {
  12. const hashParams = new URLSearchParams(hash.slice(idx + 1));
  13. val = hashParams.get(name);
  14. }
  15. }
  16. return val;
  17. },
  18. objectToQueryString(obj) {
  19. return Object.keys(obj)
  20. .filter(k => obj[k] !== undefined && obj[k] !== null)
  21. .map(k => k + '=' + encodeURIComponent(obj[k]))
  22. .join('&');
  23. },
  24. appAction(url, actType = '') {
  25. if (window.Bridge && window.Bridge.appAction) {
  26. window.Bridge.appAction(url, actType);
  27. return;
  28. }
  29. if (url.indexOf('http') !== -1 || url.startsWith('action://')) {
  30. window.location.href = url;
  31. } else if (url === 'reload') {
  32. window.location.reload();
  33. } else {
  34. window.location.href = url;
  35. }
  36. },
  37. fmtMcTime(timestamp) {
  38. if (!timestamp) return '--';
  39. const date = new Date(timestamp * 1000);
  40. const M = (date.getMonth() + 1).toString().padStart(2, '0');
  41. const D = date.getDate().toString().padStart(2, '0');
  42. const h = date.getHours().toString().padStart(2, '0');
  43. const m = date.getMinutes().toString().padStart(2, '0');
  44. return `${M}-${D} ${h}:${m}`;
  45. },
  46. getActtime(beginSecond, endSecond) {
  47. if (!beginSecond || !endSecond) return '-- 至 --';
  48. return `${this.fmtMcTime(beginSecond)} 至 ${this.fmtMcTime(endSecond)}`;
  49. },
  50. fmtMcTime2(timestamp1, timestamp2) {
  51. if (!timestamp1 || !timestamp2) return '';
  52. const date1 = new Date(timestamp1 * 1000);
  53. const date2 = new Date(timestamp2 * 1000);
  54. const parts1 = [date1.getFullYear(), date1.getMonth() + 1, date1.getDate()];
  55. const parts2 = [date2.getFullYear(), date2.getMonth() + 1, date2.getDate()];
  56. let suffix = '';
  57. if (parts2[0] !== parts1[0]) suffix = `${parts2[0]}.${parts2[1]}.${parts2[2]}`;
  58. else if (parts2[1] !== parts1[1]) suffix = `${parts2[1]}.${parts2[2]}`;
  59. else if (parts2[2] !== parts1[2]) suffix = `${parts2[2]}`;
  60. return suffix ? `${parts1[0]}.${parts1[1]}.${parts1[2]}-${suffix}` : `${parts1[0]}.${parts1[1]}.${parts1[2]}`;
  61. },
  62. timestampToTime(timestamp, type = 1) {
  63. if (!timestamp) return '--';
  64. const date = new Date(timestamp * 1000);
  65. const Y = date.getFullYear();
  66. const M = (date.getMonth() + 1).toString().padStart(2, '0');
  67. const D = date.getDate().toString().padStart(2, '0');
  68. const h = date.getHours().toString().padStart(2, '0');
  69. const m = date.getMinutes().toString().padStart(2, '0');
  70. const s = date.getSeconds().toString().padStart(2, '0');
  71. return type === 2 ? `${Y}.${M}.${D}` : `${Y}-${M}-${D} ${h}:${m}:${s}`;
  72. },
  73. convertSecondsToDHM(seconds) {
  74. if (seconds <= 0) return '00小时00分钟';
  75. const days = Math.floor(seconds / (3600 * 24));
  76. const hours = Math.floor((seconds % (3600 * 24)) / 3600);
  77. const minutes = Math.floor((seconds % (3600 * 24)) % 3600 / 60);
  78. return days > 0 ? `${days}天${hours}小时` : `${hours}小时${minutes}分钟`;
  79. },
  80. convertSecondsToHMS(seconds, type = 1) {
  81. if (seconds === undefined || seconds === null || Number.isNaN(seconds)) return '--';
  82. const s = Math.max(0, Math.floor(seconds));
  83. const h = Math.floor(s / 3600);
  84. const m = Math.floor((s % 3600) / 60);
  85. const sec = s % 60;
  86. if (type === 2) return `${m.toString().padStart(2, '0')}'${sec.toString().padStart(2, '0')}"`;
  87. return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${sec.toString().padStart(2, '0')}`;
  88. },
  89. fmtDistance(val) {
  90. if (!val || val < 0) return '0';
  91. if (val < 10000) return Math.round(val * 100 / 1000) / 100;
  92. return Math.round(val / 1000);
  93. },
  94. checkMcState(beginSecond, endSecond) {
  95. let mcState = 0;
  96. if (beginSecond > 0 && endSecond > 0) {
  97. const now = Date.now() / 1000;
  98. const dif1 = beginSecond - now;
  99. const dif2 = endSecond - now;
  100. if (dif1 > 0) mcState = 0;
  101. else if (dif2 > 0) mcState = 1;
  102. else mcState = 2;
  103. }
  104. return mcState;
  105. },
  106. showToast(title, icon = 'none', duration = 2000) {
  107. if (window.Bridge && window.Bridge.showToast) {
  108. window.Bridge.showToast(title, icon, duration);
  109. return;
  110. }
  111. const div = document.createElement('div');
  112. div.style.cssText = `
  113. position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  114. background: rgba(0,0,0,0.7); color: white; padding: 10px 20px;
  115. border-radius: 5px; font-size: 14px; z-index: 9999; text-align: center;
  116. `;
  117. div.innerText = title;
  118. document.body.appendChild(div);
  119. setTimeout(() => document.body.removeChild(div), duration);
  120. }
  121. };
  122. window.Tools = Tools;