GUID(全局統(tǒng)一標識符)是指在一臺機器上生成的數(shù)字,它保證對在同一時空中的所有機器都是唯一的。通常平臺會提供生成GUID的API。生成算法很有意思,用到了以太網(wǎng)卡地址、納秒級時間、芯片ID碼和許多可能的數(shù)字。GUID的唯一缺陷在于生成的結(jié)果串會比較大。
GUID的格式為:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
大家都知道GUID在前端開發(fā)中用處不大,但如果需要插入某個ID,并且這個ID與后臺對應(yīng)等其它需要GUID的操作時,為了方便,我們還是可以生成一個GUID的。
一般在sql、java、C#等后臺或數(shù)據(jù)庫語言中生成GUID都很簡單,而前端沒有直接生成GUID的方法,只能自己手寫一個。但由于GUID需要獲取以太網(wǎng)卡的地址、以及納秒級的時間等數(shù)字。而前端獲取到這些信息比較困難(知道的童鞋請一定告訴我),而我們可以模擬實現(xiàn)生成GUID,代碼如下:
/*
* 功能:生成一個GUID碼,其中GUID以14個以下的日期時間及18個以上的16進制隨機數(shù)組成,GUID存在一定的重復(fù)概率,但重復(fù)概率極低,理論上重復(fù)概率為每10ms有1/(16^18),即16的18次方分之1,重復(fù)概率低至可忽略不計*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
function GUID() { this .date = new Date(); /* 判斷是否初始化過,如果初始化過以下代碼,則以下代碼將不再執(zhí)行,實際中只執(zhí)行一次 */ if ( typeof this .newGUID != 'function' ) { /* 生成GUID碼 */ GUID.prototype.newGUID = function () { this .date = new Date(); var guidStr = '' ; sexadecimalDate = this .hexadecimal( this .getGUIDDate(), 16); sexadecimalTime = this .hexadecimal( this .getGUIDTime(), 16); for ( var i = 0; i < 9; i++) { guidStr += Math.floor(Math.random()*16).toString(16); } guidStr += sexadecimalDate; guidStr += sexadecimalTime; while (guidStr.length < 32) { guidStr += Math.floor(Math.random()*16).toString(16); } return this .formatGUID(guidStr); } /* * 功能:獲取當前日期的GUID格式,即8位數(shù)的日期:19700101 * 返回值:返回GUID日期格式的字條串 */ GUID.prototype.getGUIDDate = function () { return this .date.getFullYear() + this .addZero( this .date.getMonth() + 1) + this .addZero( this .date.getDay()); } /* * 功能:獲取當前時間的GUID格式,即8位數(shù)的時間,包括毫秒,毫秒為2位數(shù):12300933 * 返回值:返回GUID日期格式的字條串 */ GUID.prototype.getGUIDTime = function () { return this .addZero( this .date.getHours()) + this .addZero( this .date.getMinutes()) + this .addZero( this .date.getSeconds()) + this .addZero( parseInt( this .date.getMilliseconds() / 10 )); } /* * 功能: 為一位數(shù)的正整數(shù)前面添加0,如果是可以轉(zhuǎn)成非NaN數(shù)字的字符串也可以實現(xiàn) * 參數(shù): 參數(shù)表示準備再前面添加0的數(shù)字或可以轉(zhuǎn)換成數(shù)字的字符串 * 返回值: 如果符合條件,返回添加0后的字條串類型,否則返回自身的字符串 */ GUID.prototype.addZero = function (num) { if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) { return '0' + Math.floor(num); } else { return num.toString(); } } /* * 功能:將y進制的數(shù)值,轉(zhuǎn)換為x進制的數(shù)值 * 參數(shù):第1個參數(shù)表示欲轉(zhuǎn)換的數(shù)值;第2個參數(shù)表示欲轉(zhuǎn)換的進制;第3個參數(shù)可選,表示當前的進制數(shù),如不寫則為10 * 返回值:返回轉(zhuǎn)換后的字符串 */ GUID.prototype.hexadecimal = function (num, x, y) { if (y != undefined) { return parseInt(num.toString(), y).toString(x); } else { return parseInt(num.toString()).toString(x); } } /* * 功能:格式化32位的字符串為GUID模式的字符串 * 參數(shù):第1個參數(shù)表示32位的字符串 * 返回值:標準GUID格式的字符串 */ GUID.prototype.formatGUID = function (guidStr) { var str1 = guidStr.slice(0, 8) + '-' , str2 = guidStr.slice(8, 12) + '-' , str3 = guidStr.slice(12, 16) + '-' , str4 = guidStr.slice(16, 20) + '-' , str5 = guidStr.slice(20); return str1 + str2 + str3 + str4 + str5; } } } |
GUID 對象
只需要將其保存在一個JS文件中并引用即可。
然后我們只需要
var guid = new GUID();
alert(guid.newGUID());
即可獲取GUID碼。
實現(xiàn)原理很簡單,這里只是采用了系統(tǒng)時間與18個以上的十六進制隨機數(shù)組成,并用系統(tǒng)時間轉(zhuǎn)換為十六進制,這樣雖然還是有可能重復(fù),但是重復(fù)的概率極低,可忽略不計。