uuid.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * @Author: tengmingxue 1473375109@qq.com
  3. * @Date: 2023-08-15 11:20:48
  4. * @LastEditors: tengmingxue 1473375109@qq.com
  5. * @LastEditTime: 2023-09-19 08:48:16
  6. * @FilePath: \xld-gis-admin\src\utils\uuid.ts
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. const hexList: string[] = [];
  10. for (let i = 0; i <= 15; i++) {
  11. hexList[i] = i.toString(16);
  12. }
  13. export function buildUUID(): string {
  14. let uuid = '';
  15. for (let i = 1; i <= 36; i++) {
  16. if (i === 9 || i === 14 || i === 19 || i === 24) {
  17. uuid += '-';
  18. } else if (i === 15) {
  19. uuid += 4;
  20. } else if (i === 20) {
  21. uuid += hexList[(Math.random() * 4) | 8];
  22. } else {
  23. uuid += hexList[(Math.random() * 16) | 0];
  24. }
  25. }
  26. return uuid.replace(/-/g, '');
  27. }
  28. export function getBuildUUID(): string {
  29. let uuid = '';
  30. for (let i = 1; i <= 36; i++) {
  31. if (i === 9 || i === 14 || i === 19 || i === 24) {
  32. uuid += '-';
  33. } else if (i === 15) {
  34. uuid += 4;
  35. } else if (i === 20) {
  36. uuid += hexList[(Math.random() * 4) | 8];
  37. } else {
  38. uuid += hexList[(Math.random() * 16) | 0];
  39. }
  40. }
  41. return uuid;
  42. }
  43. let unique = 0;
  44. export function buildShortUUID(prefix = ''): string {
  45. const time = Date.now();
  46. const random = Math.floor(Math.random() * 1000000000);
  47. unique++;
  48. return prefix + '_' + random + unique + String(time);
  49. }
  50. export function getlowerCaseChars() {
  51. return Array.from({ length: 26 }).map((_, index) => String.fromCharCode(index + 97));
  52. }
  53. export function getUpperCaseChars() {
  54. return Array.from({ length: 26 }).map((_, index) => String.fromCharCode(index + 65));
  55. }
  56. export function randomString(length = 20) {
  57. const upperCaseChars = getUpperCaseChars();
  58. const lowerCaseChars = getlowerCaseChars();
  59. const numberChars = Array.from({ length: 10 }).map((_, index) => index);
  60. const allChars = [...numberChars, ...upperCaseChars, ...lowerCaseChars].sort(
  61. () => 0.5 - Math.random()
  62. );
  63. const rangeFn = () => (Math.random() * 63) | 0;
  64. return Array.from({ length })
  65. .map(() => allChars[rangeFn()])
  66. .join('');
  67. }