1.方法1
UUID(): string {
// crypto.randomUUID() 仅在 HTTPS 或 localhost 下可用
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
return crypto.randomUUID();
} else {
// 降级方案:手动生成 UUID v4
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
2.方法2
public static UUID(): string {
if (typeof (window) !== "undefined" && typeof (window.crypto) !== "undefined" && typeof (window.crypto.getRandomValues) !== "undefined") {
// If we have a cryptographically secure PRNG, use that
// http://stackoverflow.com/questions/6906916/collisions-when-generating-uuids-in-javascript
let buf: Uint16Array = new Uint16Array(8);
window.crypto.getRandomValues(buf);
return (this.pad4(buf[0]) + this.pad4(buf[1]) + "-" + this.pad4(buf[2]) + "-" + this.pad4(buf[3]) + "-" + this.pad4(buf[4]) + "-" + this.pad4(buf[5]) + this.pad4(buf[6]) + this.pad4(buf[7]));
} else {
// Otherwise, just use Math.random
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
// https://stackoverflow.com/questions/11605068/why-does-jshint-argue-against-bitwise-operators-how-should-i-express-this-code
return this.random4() + this.random4() + "-" + this.random4() + "-" + this.random4() + "-" +
this.random4() + "-" + this.random4() + this.random4() + this.random4();
}
}
private static pad4(num: number): string {
let ret: string = num.toString(16);
while (ret.length < 4) {
ret = "0" + ret;
}
return ret;
}
private static random4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
2073

被折叠的 条评论
为什么被折叠?



