返回 AiToEarn
index.ts
1 import dayjs from 'dayjs';
2 import { getFilePathNameCommon, sleep } from '../../commont/utils';
3 import { message } from 'antd';
4 /**
5 * 生成唯一ID
6 */
7 export function generateUUID(): string {
8 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
9 const r = (Math.random() * 16) | 0;
10 const v = c === 'x' ? r : (r & 0x3) | 0x8;
11 return v.toString(16);
12 });
13 }
14
15 // 获取文件路径中的文件名和后缀
16 export const getFilePathName = getFilePathNameCommon;
17
18 // 格式化时间
19 export function formatTime(
20 time: string | number | Date,
21 format: string = 'YYYY-MM-DD HH:mm:ss',
22 ) {
23 return dayjs(time).format(format);
24 }
25
26 /**
27 * 将数值转换为 hh:mm:ss 格式的字符串
28 * 7 -> 00:00:07
29 * @param seconds - 要转换的秒数
30 * @returns 格式化后的时间字符串
31 */
32 export function formatSeconds(seconds: number): string {
33 const hours = Math.floor(seconds / 3600);
34 const minutes = Math.floor((seconds % 3600) / 60);
35 const secs = seconds % 60;
36
37 const pad = (num: number) => num.toString().padStart(2, '0');
38
39 return `${pad(hours)}:${pad(minutes)}:${pad(secs)}`;
40 }
41
42 /**
43 * 根据输入数值返回中文描述
44 * @param value 输入的数值
45 * @returns 如果数值超过1000返回'n千',超过10000返回'n万'
46 */
47 export function describeNumber(value: number): string {
48 if (value > 10000) {
49 // 数值超过10000,返回'n万'
50 const wan = Math.floor(value / 10000);
51 return `${wan}万`;
52 } else if (value > 1000) {
53 // 数值超过1000,返回'n千'
54 const qian = Math.floor(value / 1000);
55 return `${qian}千`;
56 } else {
57 // 数值不超过1000,直接返回数值的字符串形式
58 return value.toString();
59 }
60 }
61
62 // 去除字符串中的话题
63 export function parseTopicString(input: string): {
64 topics: string[];
65 cleanedString: string;
66 } {
67 // 使用正则表达式提取字符串中的部分
68 const extractedParts = input.match(/#(\S+)/g) || [];
69
70 // 在原始输入中用空字符串替换提取的部分
71 let cleanedString = input;
72 extractedParts.forEach((part) => {
73 cleanedString = cleanedString.replace(part, '').trim();
74 });
75
76 // 创建提取的话题数组
77 const topics = extractedParts.map((part) => {
78 const match = part.match(/#(\S+)/);
79 return match ? match[1] : '';
80 });
81
82 return { topics, cleanedString };
83 }
84
85 // 敏感词检测-多余检测动画
86 export async function sensitivityLoading() {
87 const key = generateUUID();
88 const msgs = [
89 '敏感词检测中',
90 '色情检测中',
91 'abuse检测中',
92 '广告法检测中',
93 'Error检测中',
94 ];
95
96 for (const msg of msgs) {
97 message.open({
98 key,
99 type: 'loading',
100 content: `${msg} ...`,
101 });
102 await sleep(200);
103 }
104 message.destroy(key);
105 }
106
106 lines TYPESCRIPT