Files
fangke/admin/src/utils/tracker/index.ts
T

58 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-09-15 19:58:23 +08:00
/**
* 访客统计共享格式化工具(列表/详情页复用)
*
* 后端时间戳已统一 timezone.localtime().isoformat()(上海时间)序列化,
* 前端切串直显即可,不做时区换算。
*/
/** 国家 → 国旗 emoji(ip2region 中国 IP 为中文"中国",海外为英文名;未命中默认 🌐) */
const COUNTRY_FLAGS: Record<string, string> = {
中国: '🇨🇳',
'United States': '🇺🇸',
Japan: '🇯🇵',
'South Korea': '🇰🇷',
Singapore: '🇸🇬',
'United Kingdom': '🇬🇧',
Germany: '🇩🇪',
France: '🇫🇷',
Canada: '🇨🇦',
Australia: '🇦🇺',
Russia: '🇷🇺',
India: '🇮🇳',
Brazil: '🇧🇷',
Vietnam: '🇻🇳',
Thailand: '🇹🇭',
Malaysia: '🇲🇾',
Indonesia: '🇮🇩',
Philippines: '🇵🇭',
Netherlands: '🇳🇱',
Italy: '🇮🇹',
Spain: '🇪🇸',
Turkey: '🇹🇷',
Iran: '🇮🇷'
}
/** 国家名 → 国旗 emoji(空/未命中返回 🌐) */
export const countryFlag = (country?: string | null): string =>
(country && COUNTRY_FLAGS[country]) || '🌐'
/** ISO 时间 → YYYY-MM-DD HH:mm(后端已是上海时间;空值显示 —) */
export const formatDateTime = (iso?: string | null): string =>
iso ? iso.replace('T', ' ').slice(0, 16) : '—'
/** 毫秒 → x天x小时x分x秒(智能省略:为 0 的单位不显示;全 0 / 不足 1 秒显示 0秒) */
export const formatStay = (ms?: number | null): string => {
const totalSec = Math.max(0, Math.floor((ms ?? 0) / 1000))
const days = Math.floor(totalSec / 86400)
const hours = Math.floor((totalSec % 86400) / 3600)
const minutes = Math.floor((totalSec % 3600) / 60)
const seconds = totalSec % 60
const parts: string[] = []
if (days) parts.push(`${days}天`)
if (hours) parts.push(`${hours}小时`)
if (minutes) parts.push(`${minutes}分`)
// 秒恒显示(含 0),避免"0秒/不足 1 秒"整行变空
if (seconds || !parts.length) parts.push(`${seconds}秒`)
return parts.join('')
}