teamAvatar.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import foxAvatar from '@/assets/team-avatars/fox.png'
  2. import whaleAvatar from '@/assets/team-avatars/whale.png'
  3. import owlAvatar from '@/assets/team-avatars/owl.png'
  4. import rabbitAvatar from '@/assets/team-avatars/rabbit.png'
  5. import bearAvatar from '@/assets/team-avatars/bear.png'
  6. export const TEAM_ANIMAL_AVATAR_PREFIX = 'builtin:team-animal:'
  7. export const TEAM_ANIMAL_AVATARS = [
  8. { id: 'fox', label: '狐狸', value: `${TEAM_ANIMAL_AVATAR_PREFIX}fox`, src: foxAvatar },
  9. { id: 'whale', label: '鲸鱼', value: `${TEAM_ANIMAL_AVATAR_PREFIX}whale`, src: whaleAvatar },
  10. { id: 'owl', label: '猫头鹰', value: `${TEAM_ANIMAL_AVATAR_PREFIX}owl`, src: owlAvatar },
  11. { id: 'rabbit', label: '兔子', value: `${TEAM_ANIMAL_AVATAR_PREFIX}rabbit`, src: rabbitAvatar },
  12. { id: 'bear', label: '小熊', value: `${TEAM_ANIMAL_AVATAR_PREFIX}bear`, src: bearAvatar },
  13. ] as const
  14. export function teamAnimalAvatar(value?: string) {
  15. return TEAM_ANIMAL_AVATARS.find(avatar => avatar.value === value)
  16. }
  17. export function randomTeamAnimalAvatar(): typeof TEAM_ANIMAL_AVATARS[number] {
  18. return TEAM_ANIMAL_AVATARS[Math.floor(Math.random() * TEAM_ANIMAL_AVATARS.length)]!
  19. }
  20. export function fallbackTeamAnimalAvatar(key: string): typeof TEAM_ANIMAL_AVATARS[number] {
  21. let hash = 2166136261
  22. for (const byte of new TextEncoder().encode(key || 'team')) {
  23. hash ^= byte
  24. hash = Math.imul(hash, 16777619) >>> 0
  25. }
  26. return TEAM_ANIMAL_AVATARS[hash % TEAM_ANIMAL_AVATARS.length]!
  27. }
  28. export function resolvedTeamAvatarSource(value: string | undefined, fallbackKey: string): string {
  29. if (value && /^data:image\/(png|jpeg|webp);base64,/i.test(value)) return value
  30. return (teamAnimalAvatar(value) ?? fallbackTeamAnimalAvatar(fallbackKey)).src
  31. }
  32. const SUPPORTED_TEAM_AVATAR_TYPES = new Set([
  33. 'image/png',
  34. 'image/jpeg',
  35. 'image/webp',
  36. ])
  37. function readImage(file: File): Promise<string> {
  38. return new Promise((resolve, reject) => {
  39. const reader = new FileReader()
  40. reader.onload = () => typeof reader.result === 'string'
  41. ? resolve(reader.result)
  42. : reject(new Error('无法读取图片'))
  43. reader.onerror = () => reject(new Error('无法读取图片'))
  44. reader.readAsDataURL(file)
  45. })
  46. }
  47. function resizeAvatar(source: string): Promise<string> {
  48. return new Promise((resolve, reject) => {
  49. const image = new Image()
  50. image.onload = () => {
  51. const canvas = document.createElement('canvas')
  52. canvas.width = 256
  53. canvas.height = 256
  54. const context = canvas.getContext('2d')
  55. if (!context) return reject(new Error('当前浏览器不支持图片处理'))
  56. const edge = Math.min(image.width, image.height)
  57. context.drawImage(image, (image.width - edge) / 2, (image.height - edge) / 2, edge, edge, 0, 0, 256, 256)
  58. resolve(canvas.toDataURL('image/png'))
  59. }
  60. image.onerror = () => reject(new Error('请选择有效的 PNG、JPEG 或 WebP 图片'))
  61. image.src = source
  62. })
  63. }
  64. export async function processTeamAvatarFile(file: File): Promise<string> {
  65. if (!SUPPORTED_TEAM_AVATAR_TYPES.has(file.type)) {
  66. throw new Error('请选择 PNG、JPEG 或 WebP 图片')
  67. }
  68. if (file.size > 10 * 1024 * 1024) throw new Error('图片不能超过 10 MB')
  69. return resizeAvatar(await readImage(file))
  70. }