1 | if (!(typeof module !== "undefined" && module.exports)){ |
||
2 | var UIDvalue = getUID(); |
||
3 | } |
||
4 | |||
5 | function currentUID() { |
||
6 | return UIDvalue; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
7 | } |
||
8 | |||
9 | |||
10 | /** |
||
11 | * Get uid saved in browser |
||
12 | */ |
||
13 | function getUID() { |
||
14 | return localStorage.getItem('uid'); |
||
15 | } |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Signing the uid |
||
20 | * @param {String} UID |
||
21 | */ |
||
22 | function sign_uid(UID) { |
||
23 | var url = location.protocol + '//' + location.host + location.pathname; |
||
24 | //console.log(url); |
||
25 | if (typeof jQuery != 'undefined') { |
||
26 | $.ajax({ |
||
27 | url: url, |
||
28 | dataType: 'jsonp', |
||
29 | method: 'post', |
||
30 | headers: { |
||
31 | 'Uid-Sign': guid() |
||
32 | }, |
||
33 | data: { |
||
34 | 'uid_jsp': 1, |
||
35 | 'callback': 'saveUID', |
||
36 | 'uid': UID |
||
37 | }, |
||
38 | silent: true, |
||
39 | success: function(resdata) { |
||
40 | console.log(resdata); |
||
0 ignored issues
–
show
|
|||
41 | if (resdata.hasOwnProperty('uid')) { |
||
42 | localStorage.setItem('uid', resdata.uid); |
||
43 | } |
||
44 | } |
||
45 | }); |
||
46 | } else { |
||
47 | ajax().post(url, { |
||
48 | 'uid_jsp': genUID(), |
||
49 | 'callback': 'saveUID', |
||
50 | 'uid': UID |
||
51 | }, function(res) { |
||
52 | console.log(res); |
||
0 ignored issues
–
show
|
|||
53 | eval(res); |
||
0 ignored issues
–
show
|
|||
54 | }); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | var UIDcalled = false; |
||
59 | |||
60 | /** |
||
61 | * Check UID |
||
62 | * @return {string} uid |
||
63 | * @param {Function|any} callback |
||
64 | */ |
||
65 | function checkUID(callback) { |
||
66 | UIDvalue = getUID(); |
||
67 | if (isExpireUID()) { |
||
68 | UIDvalue = genUID(); |
||
69 | sign_uid(UIDvalue); |
||
70 | } |
||
71 | if (!UIDcalled) { |
||
72 | setTimeout(() => { |
||
73 | checkUID(); |
||
74 | }, 60000); |
||
75 | UIDcalled = true; |
||
76 | } |
||
77 | UIDvalue = getUID(); |
||
78 | if (typeof callback == 'function') { |
||
79 | return callback(UIDvalue); |
||
80 | } else { |
||
81 | return UIDvalue; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | |||
86 | function isExpireUID() { |
||
87 | |||
88 | if (typeof UIDForce == 'boolean' && UIDForce) { |
||
0 ignored issues
–
show
|
|||
89 | console.log("UID FORCED"); |
||
0 ignored issues
–
show
|
|||
90 | |||
91 | delete UIDForce; |
||
0 ignored issues
–
show
|
|||
92 | return true; |
||
93 | } else { |
||
94 | var timeLeft = framework().gc('signature-timeleft'); |
||
95 | |||
96 | timeLeft = new Date(timeLeft).getTime(); |
||
97 | var date = new Date().getTime(); |
||
98 | |||
99 | var isExpired = timeLeft < date; |
||
100 | //console.log('uid is expired ' + isExpired); |
||
101 | if (isExpired) { |
||
102 | return true; |
||
103 | } |
||
104 | return !localStorage.getItem('uid'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | function AddMinutesToDate(date, minutes) { |
||
109 | return new Date(date.getTime() + minutes * 60000); |
||
110 | } |
||
111 | |||
112 | function genUID() { |
||
113 | return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); |
||
114 | } |
||
115 | /** |
||
116 | * Save uid |
||
117 | * @param {Object} data |
||
118 | */ |
||
119 | function saveUID(data) { |
||
120 | console.log(data); |
||
0 ignored issues
–
show
|
|||
121 | if (typeof data == 'object') { |
||
122 | if (data.hasOwnProperty('uid')) { |
||
123 | console.log(`${data.uid} was saved`); |
||
124 | localStorage.setItem('uid', data.uid); |
||
125 | var date = new Date(); |
||
126 | framework().sc('signature-timeleft', AddMinutesToDate(date, 5)); // 5 mins |
||
127 | //location.reload(); |
||
128 | } |
||
129 | } |
||
130 | } |