73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
export function formatDistance(meters: number): string {
|
|
if (meters < 1000) return `${Math.round(meters)}m`
|
|
return `${(meters / 1000).toFixed(1)}km`
|
|
}
|
|
|
|
export function formatTime(isoString: string): string {
|
|
const date = new Date(isoString)
|
|
const now = new Date()
|
|
const diffMs = now.getTime() - date.getTime()
|
|
const diffMin = Math.floor(diffMs / 60000)
|
|
const diffHour = Math.floor(diffMin / 60)
|
|
const diffDay = Math.floor(diffHour / 24)
|
|
|
|
if (diffMin < 1) return '刚刚'
|
|
if (diffMin < 60) return `${diffMin}分钟前`
|
|
if (diffHour < 24) return `${diffHour}小时前`
|
|
if (diffDay < 7) return `${diffDay}天前`
|
|
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
return `${month}月${day}日`
|
|
}
|
|
|
|
export function formatLastSeen(isoString: string): string {
|
|
const diffMin = Math.floor((Date.now() - new Date(isoString).getTime()) / 60000)
|
|
if (diffMin < 2) return '刚刚在线'
|
|
if (diffMin < 60) return `${diffMin}分钟前`
|
|
const diffHour = Math.floor(diffMin / 60)
|
|
if (diffHour < 24) return `${diffHour}小时前`
|
|
return '昨天在线'
|
|
}
|
|
|
|
export function formatCount(n: number): string {
|
|
if (n < 1000) return String(n)
|
|
if (n < 10000) return `${(n / 1000).toFixed(1)}k`
|
|
return `${Math.floor(n / 10000)}w`
|
|
}
|
|
|
|
export function getAvatarGradient(seed: string): string {
|
|
const gradients = [
|
|
'linear-gradient(135deg, #ffd6e8, #fff1a6)',
|
|
'linear-gradient(135deg, #ffe7a0, #b7f4d2)',
|
|
'linear-gradient(135deg, #c9f7df, #d9d2ff)',
|
|
'linear-gradient(135deg, #cfe8ff, #ffd6e8)',
|
|
'linear-gradient(135deg, #e3d8ff, #c9f7df)',
|
|
]
|
|
let hash = 0
|
|
for (let i = 0; i < seed.length; i++) hash = (hash * 31 + seed.charCodeAt(i)) & 0xffffffff
|
|
return gradients[Math.abs(hash) % gradients.length]
|
|
}
|
|
|
|
export function getPetEmoji(species: string, breed?: string): string {
|
|
if (species === 'cat') return '🐱'
|
|
if (species === 'other') return '🐾'
|
|
const dogBreedEmoji: Record<string, string> = {
|
|
'比熊': '🐩',
|
|
'柴犬': '🐕',
|
|
'金毛': '🦮',
|
|
'哈士奇': '🐺',
|
|
'萨摩耶': '🐕🦺',
|
|
}
|
|
return (breed && dogBreedEmoji[breed]) || '🐶'
|
|
}
|
|
|
|
export function calcDistance(lat1: number, lng1: number, lat2: number, lng2: number): number {
|
|
const R = 6371000
|
|
const dLat = (lat2 - lat1) * Math.PI / 180
|
|
const dLng = (lng2 - lng1) * Math.PI / 180
|
|
const a = Math.sin(dLat / 2) ** 2 +
|
|
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLng / 2) ** 2
|
|
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
|
}
|