zhangwencui
7 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { formatDateTime } from '#/packages/utils/src';
 
/** 展示用户名称 */
export function formatUserLabel(
  nickname: string | undefined,
  userId: number | undefined,
) {
  if (!userId) {
    return '-';
  }
  return `${nickname || '-'} (${userId})`;
}
 
/** 展示群名称 */
export function formatGroupLabel(
  name: string | undefined,
  groupId: number | undefined,
) {
  if (!groupId) {
    return '-';
  }
  return `${name || '-'} (${groupId})`;
}
 
/** 展示日期时间 */
export function formatDateTimeText(value?: Date | number | string) {
  return value ? (formatDateTime(value) as string) : '-';
}
 
/** 格式化 JSON */
export function formatJsonText(content?: string) {
  if (!content) {
    return '';
  }
  try {
    return JSON.stringify(JSON.parse(content), null, 2);
  } catch {
    return content;
  }
}
 
/** 探测图片尺寸 */
export function probeImageSize(url: string) {
  return new Promise<{ height: number; width: number }>((resolve, reject) => {
    const img = new Image();
    img.addEventListener('load', () => {
      resolve({
        width: img.naturalWidth || img.width,
        height: img.naturalHeight || img.height,
      });
    });
    img.addEventListener('error', reject);
    img.src = url;
  });
}