File size: 1,601 Bytes
075f1a3
 
 
 
 
b349768
 
075f1a3
 
 
 
 
 
b349768
075f1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 设备ID管理工具
 * 为每个访问设备生成并存储唯一的设备ID
 */

import { makeDeviceId } from './ids'

const DEVICE_ID_KEY = 'metarec_device_id'

/**
 * 生成唯一的设备ID
 */
function generateDeviceId(): string {
  return makeDeviceId()
}

/**
 * 获取或创建设备ID
 * 如果localStorage中已有设备ID,则返回它
 * 否则生成新的设备ID并存储
 */
export function getDeviceId(): string {
  try {
    // 尝试从localStorage获取
    let deviceId = localStorage.getItem(DEVICE_ID_KEY)
    
    if (!deviceId) {
      // 如果没有,生成新的
      deviceId = generateDeviceId()
      localStorage.setItem(DEVICE_ID_KEY, deviceId)
    }
    
    return deviceId
  } catch (error) {
    // 如果localStorage不可用(如隐私模式),使用sessionStorage
    console.warn('localStorage not available, using sessionStorage:', error)
    try {
      let deviceId = sessionStorage.getItem(DEVICE_ID_KEY)
      
      if (!deviceId) {
        deviceId = generateDeviceId()
        sessionStorage.setItem(DEVICE_ID_KEY, deviceId)
      }
      
      return deviceId
    } catch (e) {
      // 如果都不可用,生成临时ID(会话期间有效)
      console.warn('sessionStorage not available, using temporary ID:', e)
      return generateDeviceId()
    }
  }
}

/**
 * 清除设备ID(用于测试或重置)
 */
export function clearDeviceId(): void {
  try {
    localStorage.removeItem(DEVICE_ID_KEY)
    sessionStorage.removeItem(DEVICE_ID_KEY)
  } catch (error) {
    console.warn('Failed to clear device ID:', error)
  }
}