File size: 3,264 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { User } from '../types';
import { deviceService } from './deviceService';

class AuthService {

  // 模拟用户数据
  private mockUsers: User[] = [
    {
      id: 'user_1',
      email: 'admin@novelforge.com',
      username: 'admin',
      full_name: '管理员',
      role: 'admin',
      created_at: new Date().toISOString(),
      last_login_at: new Date().toISOString(),
    },
    {
      id: 'user_2', 
      email: 'demo@novelforge.com',
      username: 'demo_user',
      full_name: '演示用户',
      role: 'user',
      created_at: new Date().toISOString(),
      last_login_at: new Date().toISOString(),
    }
  ];

  async signIn(email: string, password: string): Promise<User> {
    // 模拟登录验证
    await this.delay(1000); // 模拟网络延迟

    const user = this.mockUsers.find(u => u.email === email);
    
    if (!user) {
      throw new Error('用户不存在');
    }

    if (password.length < 6) {
      throw new Error('密码错误');
    }

    // 检查设备绑定
    const deviceFingerprint = deviceService.getDeviceFingerprint();
    console.log('设备指纹:', deviceFingerprint);

    // 更新最后登录时间
    user.last_login_at = new Date().toISOString();

    return user;
  }

  async signUp(email: string, password: string, userData?: Partial<User>): Promise<User> {
    await this.delay(1500);

    // 检查用户是否已存在
    const existingUser = this.mockUsers.find(u => u.email === email);
    if (existingUser) {
      throw new Error('该邮箱已被注册');
    }

    if (password.length < 6) {
      throw new Error('密码至少需6位字符');
    }

    const newUser: User = {
      id: `user_${Date.now()}`,
      email,
      username: userData?.username || email.split('@')[0],
      full_name: userData?.full_name || '新用户',
      role: 'user',
      created_at: new Date().toISOString(),
      last_login_at: new Date().toISOString(),
    };

    this.mockUsers.push(newUser);
    return newUser;
  }

  async signOut(): Promise<void> {
    await this.delay(500);
    // 清理本地存储
    localStorage.removeItem('novel_forge_user');
  }

  async updateProfile(userId: string, data: Partial<User>): Promise<User> {
    await this.delay(800);

    const userIndex = this.mockUsers.findIndex(u => u.id === userId);
    if (userIndex === -1) {
      throw new Error('用户不存在');
    }

    this.mockUsers[userIndex] = {
      ...this.mockUsers[userIndex],
      ...data,
    };

    return this.mockUsers[userIndex];
  }

  async getCurrentUser(): Promise<User | null> {
    try {
      const savedUser = localStorage.getItem('novel_forge_user');
      if (savedUser) {
        return JSON.parse(savedUser);
      }
      return null;
    } catch {
      return null;
    }
  }

  async resetPassword(email: string): Promise<void> {
    await this.delay(1000);
    
    const user = this.mockUsers.find(u => u.email === email);
    if (!user) {
      throw new Error('用户不存在');
    }

    // 模拟发送重置邮件
    console.log(`密码重置邮件已发送至: ${email}`);
  }

  private delay(ms: number): Promise<void> {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
}

export const authService = new AuthService();