Files
gao-customer-h5/src/utils/avatar.ts
T

19 lines
745 B
TypeScript

/**
* 构建用户备用头像。
*
* @param name 头像中显示的用户名
* @param backgroundColor 头像背景色
*/
export function buildFallbackAvatar(name?: string, backgroundColor = "#2563eb"): string {
const text = encodeURIComponent((name?.trim() || "用户").slice(0, 1));
const fill = encodeURIComponent(backgroundColor);
return [
"data:image/svg+xml;charset=UTF-8,",
"%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120' viewBox='0 0 120 120'%3E",
`%3Crect width='120' height='120' rx='24' fill='${fill}'/%3E`,
"%3Ctext x='50%25' y='54%25' fill='white' font-size='36' font-family='Arial,sans-serif' text-anchor='middle' dominant-baseline='middle'%3E",
text,
"%3C/text%3E%3C/svg%3E"
].join("");
}