| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /**
- * 通用工具库(原生 JS 版),替代 common/tools.js 的常用能力
- */
- const Tools = {
- getQueryParam(name) {
- const params = new URLSearchParams(window.location.search);
- let val = params.get(name);
- if (!val && window.location.hash) {
- const hash = window.location.hash;
- const idx = hash.indexOf('?');
- if (idx >= 0) {
- const hashParams = new URLSearchParams(hash.slice(idx + 1));
- val = hashParams.get(name);
- }
- }
- return val;
- },
- objectToQueryString(obj) {
- return Object.keys(obj)
- .filter(k => obj[k] !== undefined && obj[k] !== null)
- .map(k => k + '=' + encodeURIComponent(obj[k]))
- .join('&');
- },
- appAction(url, actType = '') {
- if (window.Bridge && window.Bridge.appAction) {
- window.Bridge.appAction(url, actType);
- return;
- }
- if (url.indexOf('http') !== -1 || url.startsWith('action://')) {
- window.location.href = url;
- } else if (url === 'reload') {
- window.location.reload();
- } else {
- window.location.href = url;
- }
- },
- fmtMcTime(timestamp) {
- if (!timestamp) return '--';
- const date = new Date(timestamp * 1000);
- const M = (date.getMonth() + 1).toString().padStart(2, '0');
- const D = date.getDate().toString().padStart(2, '0');
- const h = date.getHours().toString().padStart(2, '0');
- const m = date.getMinutes().toString().padStart(2, '0');
- return `${M}-${D} ${h}:${m}`;
- },
- getActtime(beginSecond, endSecond) {
- if (!beginSecond || !endSecond) return '-- 至 --';
- return `${this.fmtMcTime(beginSecond)} 至 ${this.fmtMcTime(endSecond)}`;
- },
- fmtMcTime2(timestamp1, timestamp2) {
- if (!timestamp1 || !timestamp2) return '';
- const date1 = new Date(timestamp1 * 1000);
- const date2 = new Date(timestamp2 * 1000);
- const parts1 = [date1.getFullYear(), date1.getMonth() + 1, date1.getDate()];
- const parts2 = [date2.getFullYear(), date2.getMonth() + 1, date2.getDate()];
- let suffix = '';
- if (parts2[0] !== parts1[0]) suffix = `${parts2[0]}.${parts2[1]}.${parts2[2]}`;
- else if (parts2[1] !== parts1[1]) suffix = `${parts2[1]}.${parts2[2]}`;
- else if (parts2[2] !== parts1[2]) suffix = `${parts2[2]}`;
- return suffix ? `${parts1[0]}.${parts1[1]}.${parts1[2]}-${suffix}` : `${parts1[0]}.${parts1[1]}.${parts1[2]}`;
- },
- timestampToTime(timestamp, type = 1) {
- if (!timestamp) return '--';
- const date = new Date(timestamp * 1000);
- const Y = date.getFullYear();
- const M = (date.getMonth() + 1).toString().padStart(2, '0');
- const D = date.getDate().toString().padStart(2, '0');
- const h = date.getHours().toString().padStart(2, '0');
- const m = date.getMinutes().toString().padStart(2, '0');
- const s = date.getSeconds().toString().padStart(2, '0');
- return type === 2 ? `${Y}.${M}.${D}` : `${Y}-${M}-${D} ${h}:${m}:${s}`;
- },
- convertSecondsToDHM(seconds) {
- if (seconds <= 0) return '00小时00分钟';
- const days = Math.floor(seconds / (3600 * 24));
- const hours = Math.floor((seconds % (3600 * 24)) / 3600);
- const minutes = Math.floor((seconds % (3600 * 24)) % 3600 / 60);
- return days > 0 ? `${days}天${hours}小时` : `${hours}小时${minutes}分钟`;
- },
- convertSecondsToHMS(seconds, type = 1) {
- if (seconds === undefined || seconds === null || Number.isNaN(seconds)) return '--';
- const s = Math.max(0, Math.floor(seconds));
- const h = Math.floor(s / 3600);
- const m = Math.floor((s % 3600) / 60);
- const sec = s % 60;
- if (type === 2) return `${m.toString().padStart(2, '0')}'${sec.toString().padStart(2, '0')}"`;
- return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${sec.toString().padStart(2, '0')}`;
- },
- fmtDistance(val) {
- if (!val || val < 0) return '0';
- if (val < 10000) return Math.round(val * 100 / 1000) / 100;
- return Math.round(val / 1000);
- },
- checkMcState(beginSecond, endSecond) {
- let mcState = 0;
- if (beginSecond > 0 && endSecond > 0) {
- const now = Date.now() / 1000;
- const dif1 = beginSecond - now;
- const dif2 = endSecond - now;
- if (dif1 > 0) mcState = 0;
- else if (dif2 > 0) mcState = 1;
- else mcState = 2;
- }
- return mcState;
- },
- showToast(title, icon = 'none', duration = 2000) {
- if (window.Bridge && window.Bridge.showToast) {
- window.Bridge.showToast(title, icon, duration);
- return;
- }
- const div = document.createElement('div');
- div.style.cssText = `
- position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
- background: rgba(0,0,0,0.7); color: white; padding: 10px 20px;
- border-radius: 5px; font-size: 14px; z-index: 9999; text-align: center;
- `;
- div.innerText = title;
- document.body.appendChild(div);
- setTimeout(() => document.body.removeChild(div), duration);
- }
- };
- window.Tools = Tools;
|