Files
PetCommunity/miniprogram/utils/format.ts
chenwu b02e26f602 chore: 清理冗余代码并补全私信/搜索/故事/通知功能
- 删除未使用的空组件目录、死代码导出(clearAuth/calcDistance/COLORS/
  AVATAR_GRADIENTS/Message/TabBarItem/deletePost/uploadImage/getFileURL/
  _initLogin),确保程序仍可正常运行
- 新增 8 个云函数并补全对应页面逻辑:getMessages/sendMessage(私信聊天)、
  getHotTopics/searchPosts/searchUsers(搜索)、getStory(故事,复用 posts
  集合)、getNotifications/markAllRead(基于点赞评论聚合的通知系统)
- 在 cloudbaserc.json 中注册全部新云函数,并通过 --deployMode zip 完成部署

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 16:27:28 +08:00

64 lines
2.1 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]) || '🐶'
}