时间戳 + 随机数
createId ( ) {
return Math. random ( ) . toString ( 36 ) . substr ( 2 ) + Date. now ( ) . toString ( 36 )
}
UUID格式
generateUUID ( ) {
var d = new Date ( ) . getTime ( )
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( /[xy]/g , function ( c) {
var r = ( d + Math. random ( ) * 16 ) % 16 | 0
d = Math. floor ( d/ 16 )
return ( c== 'x' ? r : ( r& 0x3 | 0x8 ) ) . toString ( 36 )
} )
return uuid
} ,
GUID格式
getMathRandom ( ) {
return ( ( ( 1 + Math. random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 )
} ,
generateGUID ( ) {
return ( this . getMathRandom ( ) + this . getMathRandom ( ) + '-' + this . getMathRandom ( ) + '-' + this . getMathRandom ( ) + '-' + this . getMathRandom ( ) + '-' + this . getMathRandom ( ) + this . getMathRandom ( ) + this . getMathRandom ( ) )
}
可以指定长度和进制的UUID和GUID
Uuid ( len, radix) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . split ( '' )
var uuid = [ ] , i
radix = radix || chars. length
if ( len) {
for ( i = 0 ; i < len; i++ ) uuid[ i] = chars[ 0 | Math. random ( ) * radix]
} else {
var r
uuid[ 8 ] = uuid[ 13 ] = uuid[ 18 ] = uuid[ 23 ] = '-'
uuid[ 14 ] = '4'
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 ( '' )
}