File size: 2,707 Bytes
9dd9a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
class DeviceService {
  getDeviceFingerprint(): string {
    // 生成设备指纹
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    ctx?.fillText('NovelForge', 10, 10);
    const canvasFingerprint = canvas.toDataURL();

    const fingerprint = {
      userAgent: navigator.userAgent,
      language: navigator.language,
      platform: navigator.platform,
      screen: `${screen.width}x${screen.height}`,
      timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      canvas: canvasFingerprint.slice(-20),
      plugins: Array.from(navigator.plugins).map(p => p.name).join(','),
    };

    // 生成哈希
    return this.simpleHash(JSON.stringify(fingerprint));
  }

  getDeviceInfo() {
    return {
      platform: navigator.platform,
      userAgent: navigator.userAgent,
      language: navigator.language,
      screen: {
        width: screen.width,
        height: screen.height,
        colorDepth: screen.colorDepth,
      },
      timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
      timestamp: new Date().toISOString(),
    };
  }

  private simpleHash(str: string): string {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      const char = str.charCodeAt(i);
      hash = ((hash << 5) - hash) + char;
      hash = hash & hash; // Convert to 32bit integer
    }
    return Math.abs(hash).toString(16);
  }

  async verifyDevice(userId: string, deviceFingerprint: string): Promise<boolean> {
    // 模拟设备验证
    const savedDevices = localStorage.getItem(`devices_${userId}`);
    if (!savedDevices) {
      // 第一次登录,保存设备信息
      localStorage.setItem(`devices_${userId}`, JSON.stringify([deviceFingerprint]));
      return true;
    }

    const devices = JSON.parse(savedDevices);
    return devices.includes(deviceFingerprint);
  }

  async bindDevice(userId: string, deviceFingerprint: string): Promise<void> {
    const savedDevices = localStorage.getItem(`devices_${userId}`);
    const devices = savedDevices ? JSON.parse(savedDevices) : [];
    
    if (!devices.includes(deviceFingerprint)) {
      devices.push(deviceFingerprint);
      localStorage.setItem(`devices_${userId}`, JSON.stringify(devices));
    }
  }

  async unbindDevice(userId: string, deviceFingerprint: string): Promise<void> {
    const savedDevices = localStorage.getItem(`devices_${userId}`);
    if (savedDevices) {
      const devices = JSON.parse(savedDevices);
      const filteredDevices = devices.filter((d: string) => d !== deviceFingerprint);
      localStorage.setItem(`devices_${userId}`, JSON.stringify(filteredDevices));
    }
  }
}

export const deviceService = new DeviceService();