Files
monkey-ai-web/src/helpers/avatar.js
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

294 lines
11 KiB
JavaScript

import { encodeToBase64 } from './base64';
import { stringToColor } from './render';
export const USER_AVATAR_TYPES = {
PRESET: 'preset',
INITIAL: 'initial',
CUSTOM: 'custom',
};
export const CARTOON_AVATAR_PRESETS = [
{
id: 'fox',
label: '灵感小狐',
bg: ['#ffedd5', '#fed7aa'],
face: '#fb923c',
faceAlt: '#fff7ed',
accent: '#ea580c',
},
{
id: 'cat',
label: '蓝莓小猫',
bg: ['#dbeafe', '#bfdbfe'],
face: '#60a5fa',
faceAlt: '#eff6ff',
accent: '#2563eb',
},
{
id: 'panda',
label: '云朵熊猫',
bg: ['#f8fafc', '#cbd5e1'],
face: '#ffffff',
faceAlt: '#111827',
accent: '#475569',
},
{
id: 'bunny',
label: '樱桃小兔',
bg: ['#fce7f3', '#fbcfe8'],
face: '#ffffff',
faceAlt: '#f9a8d4',
accent: '#db2777',
},
{
id: 'robot',
label: '像素机器人',
bg: ['#ccfbf1', '#99f6e4'],
face: '#2dd4bf',
faceAlt: '#0f766e',
accent: '#14b8a6',
},
{
id: 'koala',
label: '雾灰考拉',
bg: ['#e5e7eb', '#d1d5db'],
face: '#9ca3af',
faceAlt: '#f3f4f6',
accent: '#4b5563',
},
{
id: 'tiger',
label: '蜜糖小虎',
bg: ['#fef3c7', '#fde68a'],
face: '#f59e0b',
faceAlt: '#fff7ed',
accent: '#b45309',
},
{
id: 'owl',
label: '夜航猫头鹰',
bg: ['#ede9fe', '#ddd6fe'],
face: '#8b5cf6',
faceAlt: '#f5f3ff',
accent: '#6d28d9',
},
];
const DEFAULT_AVATAR_FALLBACK =
'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/docs-icon.png';
const escapeSvgText = (value) =>
String(value ?? '')
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
const svgToDataUrl = (svg) =>
`data:image/svg+xml;base64,${encodeToBase64(svg)}`;
const hashString = (value) => {
const text = String(value || '');
let hash = 0;
for (let i = 0; i < text.length; i += 1) {
hash = (hash * 31 + text.charCodeAt(i)) >>> 0;
}
return hash;
};
export const getAvatarPresetById = (id) =>
CARTOON_AVATAR_PRESETS.find((item) => item.id === id) ||
CARTOON_AVATAR_PRESETS[0];
export const getDefaultAvatarPresetId = (user) => {
if (!user) return CARTOON_AVATAR_PRESETS[0].id;
const seed = user.id || user.username || user.display_name || 'user';
return CARTOON_AVATAR_PRESETS[
hashString(seed) % CARTOON_AVATAR_PRESETS.length
].id;
};
export const parseUserSetting = (setting) => {
if (!setting) return {};
if (typeof setting === 'object') return setting;
try {
const parsed = JSON.parse(setting);
return parsed && typeof parsed === 'object' ? parsed : {};
} catch (_) {
return {};
}
};
export const getInitialAvatarDataUrl = (username, size = 96) => {
if (!username) return DEFAULT_AVATAR_FALLBACK;
const firstLetter = escapeSvgText(username[0].toUpperCase());
const bgColor = stringToColor(username);
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 96 96">
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="${bgColor}" stop-opacity="0.92"/>
<stop offset="1" stop-color="#111827" stop-opacity="0.88"/>
</linearGradient>
</defs>
<rect width="96" height="96" rx="28" fill="url(#bg)" />
<circle cx="72" cy="22" r="18" fill="#ffffff" opacity="0.16" />
<text x="50%" y="52%" dominant-baseline="central" text-anchor="middle" font-size="42" font-weight="700" fill="#ffffff" font-family="Lato, Arial, sans-serif">${firstLetter}</text>
</svg>
`;
return svgToDataUrl(svg);
};
export const getCartoonAvatarDataUrl = (presetId, size = 96) => {
const preset = getAvatarPresetById(presetId);
const { id, bg, face, faceAlt, accent } = preset;
const details = {
fox: `
<path d="M19 33 L31 14 L41 34 Z" fill="${face}" />
<path d="M77 33 L65 14 L55 34 Z" fill="${face}" />
<path d="M27 29 L31 22 L35 31 Z" fill="${faceAlt}" opacity="0.9" />
<path d="M69 29 L65 22 L61 31 Z" fill="${faceAlt}" opacity="0.9" />
<circle cx="48" cy="52" r="30" fill="${face}" />
<path d="M26 52 C34 44 39 42 48 42 C57 42 62 44 70 52 C66 68 58 78 48 80 C38 78 30 68 26 52Z" fill="${faceAlt}" />
<circle cx="38" cy="50" r="3.5" fill="#111827" />
<circle cx="58" cy="50" r="3.5" fill="#111827" />
<path d="M43 61 Q48 65 53 61" stroke="#7c2d12" stroke-width="3" stroke-linecap="round" fill="none" />
<path d="M45 56 Q48 58 51 56 Q48 62 45 56Z" fill="#111827" />
`,
cat: `
<path d="M20 35 L31 18 L40 38 Z" fill="${face}" />
<path d="M76 35 L65 18 L56 38 Z" fill="${face}" />
<path d="M28 33 L32 26 L36 36 Z" fill="${faceAlt}" />
<path d="M68 33 L64 26 L60 36 Z" fill="${faceAlt}" />
<circle cx="48" cy="54" r="30" fill="${face}" />
<circle cx="37" cy="51" r="4" fill="#0f172a" />
<circle cx="59" cy="51" r="4" fill="#0f172a" />
<path d="M45 61 Q48 64 51 61" stroke="#0f172a" stroke-width="2.8" stroke-linecap="round" fill="none" />
<path d="M30 60 L18 56 M30 65 L18 66 M66 60 L78 56 M66 65 L78 66" stroke="${accent}" stroke-width="2.5" stroke-linecap="round" opacity="0.75" />
`,
panda: `
<circle cx="26" cy="34" r="13" fill="${faceAlt}" />
<circle cx="70" cy="34" r="13" fill="${faceAlt}" />
<circle cx="48" cy="54" r="31" fill="${face}" />
<ellipse cx="36" cy="51" rx="10" ry="12" fill="${faceAlt}" />
<ellipse cx="60" cy="51" rx="10" ry="12" fill="${faceAlt}" />
<circle cx="38" cy="50" r="3" fill="#ffffff" />
<circle cx="58" cy="50" r="3" fill="#ffffff" />
<ellipse cx="48" cy="62" rx="6" ry="4" fill="${faceAlt}" />
<path d="M42 69 Q48 73 54 69" stroke="${faceAlt}" stroke-width="3" stroke-linecap="round" fill="none" />
`,
bunny: `
<rect x="29" y="9" width="13" height="38" rx="7" fill="${face}" transform="rotate(-10 35.5 28)" />
<rect x="54" y="9" width="13" height="38" rx="7" fill="${face}" transform="rotate(10 60.5 28)" />
<rect x="33" y="16" width="6" height="25" rx="3" fill="${faceAlt}" transform="rotate(-10 36 28)" />
<rect x="57" y="16" width="6" height="25" rx="3" fill="${faceAlt}" transform="rotate(10 60 28)" />
<circle cx="48" cy="57" r="29" fill="${face}" />
<circle cx="38" cy="55" r="3.4" fill="#1f2937" />
<circle cx="58" cy="55" r="3.4" fill="#1f2937" />
<circle cx="32" cy="63" r="5" fill="${faceAlt}" opacity="0.75" />
<circle cx="64" cy="63" r="5" fill="${faceAlt}" opacity="0.75" />
<path d="M45 62 Q48 66 51 62" stroke="#1f2937" stroke-width="2.6" stroke-linecap="round" fill="none" />
`,
robot: `
<rect x="27" y="29" width="42" height="42" rx="12" fill="${face}" />
<rect x="35" y="38" width="26" height="20" rx="6" fill="#ecfeff" />
<circle cx="41" cy="48" r="4" fill="${faceAlt}" />
<circle cx="55" cy="48" r="4" fill="${faceAlt}" />
<path d="M42 62 H54" stroke="${faceAlt}" stroke-width="3" stroke-linecap="round" />
<path d="M48 21 V29" stroke="${accent}" stroke-width="4" stroke-linecap="round" />
<circle cx="48" cy="18" r="5" fill="${accent}" />
<rect x="20" y="43" width="7" height="16" rx="3.5" fill="${accent}" />
<rect x="69" y="43" width="7" height="16" rx="3.5" fill="${accent}" />
`,
koala: `
<circle cx="25" cy="44" r="16" fill="${accent}" />
<circle cx="71" cy="44" r="16" fill="${accent}" />
<circle cx="48" cy="55" r="30" fill="${face}" />
<circle cx="36" cy="51" r="4" fill="#111827" />
<circle cx="60" cy="51" r="4" fill="#111827" />
<ellipse cx="48" cy="60" rx="7" ry="9" fill="#111827" />
<path d="M41 72 Q48 76 55 72" stroke="#111827" stroke-width="3" stroke-linecap="round" fill="none" />
<circle cx="30" cy="45" r="8" fill="${faceAlt}" opacity="0.7" />
<circle cx="66" cy="45" r="8" fill="${faceAlt}" opacity="0.7" />
`,
tiger: `
<circle cx="26" cy="34" r="12" fill="${face}" />
<circle cx="70" cy="34" r="12" fill="${face}" />
<circle cx="48" cy="54" r="30" fill="${face}" />
<path d="M35 30 L40 42 M48 25 V39 M61 30 L56 42" stroke="${accent}" stroke-width="4" stroke-linecap="round" />
<circle cx="38" cy="52" r="3.5" fill="#111827" />
<circle cx="58" cy="52" r="3.5" fill="#111827" />
<ellipse cx="48" cy="63" rx="12" ry="10" fill="${faceAlt}" />
<path d="M45 60 Q48 63 51 60" fill="#111827" />
<path d="M43 68 Q48 72 53 68" stroke="#111827" stroke-width="2.5" stroke-linecap="round" fill="none" />
`,
owl: `
<path d="M23 39 C24 22 36 17 48 28 C60 17 72 22 73 39 C77 62 65 80 48 80 C31 80 19 62 23 39Z" fill="${face}" />
<circle cx="38" cy="49" r="12" fill="${faceAlt}" />
<circle cx="58" cy="49" r="12" fill="${faceAlt}" />
<circle cx="38" cy="49" r="4" fill="#111827" />
<circle cx="58" cy="49" r="4" fill="#111827" />
<path d="M45 58 L51 58 L48 65Z" fill="#f59e0b" />
<path d="M31 34 Q38 28 45 34 M51 34 Q58 28 65 34" stroke="${accent}" stroke-width="3" stroke-linecap="round" fill="none" />
`,
};
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 96 96">
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="${bg[0]}"/>
<stop offset="1" stop-color="${bg[1]}"/>
</linearGradient>
</defs>
<rect width="96" height="96" rx="28" fill="url(#bg)" />
<circle cx="72" cy="23" r="19" fill="#ffffff" opacity="0.24" />
<circle cx="18" cy="79" r="20" fill="${accent}" opacity="0.12" />
${details[id] || details.fox}
</svg>
`;
return svgToDataUrl(svg);
};
export const getUserAvatarSetting = (user) => {
const settings = parseUserSetting(user?.setting);
const avatarType = Object.values(USER_AVATAR_TYPES).includes(
settings.avatar_type,
)
? settings.avatar_type
: USER_AVATAR_TYPES.PRESET;
const preset = getAvatarPresetById(
settings.avatar_preset || getDefaultAvatarPresetId(user),
);
return {
avatarType,
avatarPreset: preset.id,
customAvatarUrl:
typeof settings.custom_avatar_url === 'string'
? settings.custom_avatar_url
: '',
};
};
export const getUserAvatarUrl = (user, options = {}) => {
const { size = 96 } = options;
const username = user?.display_name || user?.username || 'User';
const setting = getUserAvatarSetting(user);
if (
setting.avatarType === USER_AVATAR_TYPES.CUSTOM &&
setting.customAvatarUrl
) {
return setting.customAvatarUrl;
}
if (setting.avatarType === USER_AVATAR_TYPES.INITIAL) {
return getInitialAvatarDataUrl(username, size);
}
return getCartoonAvatarDataUrl(setting.avatarPreset, size);
};