@peng_kai/kit 0.2.50 → 0.2.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.2.50",
4
+ "version": "0.2.51",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
@@ -25,6 +25,7 @@ export function getDeviceInfo(callback: (base: Record<string, any>, all: GetResu
25
25
  };
26
26
 
27
27
  req.headers.set('Device', callback(base, result));
28
+ req.headers.set('Device-Id', getDeviceID(base.visitor_id));
28
29
  req.headers.set('Accept-Date', getCurrentTimeZone());
29
30
 
30
31
  return req;
@@ -38,3 +39,24 @@ function getCurrentTimeZone() {
38
39
  const tzOffset = now.getTimezoneOffset() / 60;
39
40
  return toBase64([time, tz, tzOffset, describe].join('|'));
40
41
  }
42
+
43
+ function getDeviceID(visitorId: string) {
44
+ const key = 'APP_DEVICE_ID';
45
+ const oldId = localStorage.getItem(key);
46
+ const newId = oldId || visitorId || generateRandomStringSecure(32);
47
+ newId !== oldId && localStorage.setItem(key, newId);
48
+ return newId;
49
+ }
50
+
51
+ function generateRandomStringSecure(length: number) {
52
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
53
+ const randomValues = new Uint32Array(length);
54
+ window.crypto.getRandomValues(randomValues); // 浏览器内置加密 API
55
+
56
+ let result = '';
57
+ for (let i = 0; i < length; i++) {
58
+ result += chars[randomValues[i] % chars.length];
59
+ }
60
+
61
+ return result;
62
+ }