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 { // 模拟登录验证 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): Promise { 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 { await this.delay(500); // 清理本地存储 localStorage.removeItem('novel_forge_user'); } async updateProfile(userId: string, data: Partial): Promise { 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 { try { const savedUser = localStorage.getItem('novel_forge_user'); if (savedUser) { return JSON.parse(savedUser); } return null; } catch { return null; } } async resetPassword(email: string): Promise { 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 { return new Promise(resolve => setTimeout(resolve, ms)); } } export const authService = new AuthService();