JS生成唯一标识

JS生成唯一标识

920 发布: 2023/11/17 15:10 本文总阅读量

方式一

 // 添加唯一编码
    function fn_Guid() {
      function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
      }
      return (s4() +s4() +"-" +s4() +"-" +s4() +"-" + s4() +"-" +s4() +s4() +s4()
      );
    }
 console.log( fn_Guid() );//5ab57ce8-8e90-65ef-1815-6a11102a2300

方式二 指定长度和基数

function fn_Guid(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [],
        i;
    radix = radix || chars.length;

    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        // rfc4122, version 4 form
        var r;
    // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}

console.log( fn_Guid() );     // B0A9158E-C37A-4226-B084-D313383A6E3B

方式三

function fn_Guid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}
console.log( fn_Guid() ) ;    // ab683eca-5616-41d7-8d73-307f26957339

方式四 标准id

function fn_Guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
console.log( fn_Guid() );    // 12c96135-d4b6-488f-ba1d-449b27851e0b

方式五 生成标准的uuid,且使用了随机种子,比上一个方法要好

function fn_Guid() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;    // d是随机种子
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}
console.log( fn_Guid() ) ;   //  826fe4be-631c-4a63-83e9-96a788e8a569

方式六 生成了不太规范的唯一标识,但是平时使用是最简洁的

var uuid = new Date().getTime() + Math.random().toString(36).substr(2);
console.log( uuid );   // 1590753224242oqgomgkv7tp

方法六的扩展 比方法六看起来规范些,但是需要一个格式化时间的函数.

function formatDateTime() {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return y + m + d + h + minute + second;
}
var uuid = formatDateTime() + Math.random().toString(36).substr(2);
console.log(uuid);   // 20200529195844692wbkvn5l