vue-vben-admin/src/utils/uuid.ts

29 lines
710 B
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
const hexList: string[] = [];
for (let i = 0; i <= 15; i++) {
hexList[i] = i.toString(16);
}
2021-02-09 23:06:00 +08:00
2020-09-28 20:19:10 +08:00
export function buildUUID(): string {
let uuid = '';
for (let i = 1; i <= 36; i++) {
if (i === 9 || i === 14 || i === 19 || i === 24) {
uuid += '-';
} else if (i === 15) {
uuid += 4;
} else if (i === 20) {
2020-11-12 22:20:15 +08:00
uuid += hexList[(Math.random() * 4) | 8];
2020-09-28 20:19:10 +08:00
} else {
uuid += hexList[(Math.random() * 16) | 0];
}
}
return uuid.replace(/-/g, '');
}
2020-10-22 23:56:33 +08:00
let unique = 0;
2021-02-26 20:09:24 +08:00
export function buildShortUUID(prefix = ''): string {
2020-10-22 23:56:33 +08:00
const time = Date.now();
const random = Math.floor(Math.random() * 1000000000);
unique++;
return prefix + '_' + random + unique + String(time);
}