This commit is contained in:
D8D Developer
2025-06-27 02:45:41 +00:00
parent ee21f49eeb
commit d62dad3dbc

View File

@@ -4,8 +4,6 @@ import { UserEntity as User } from '../users/user.entity';
const JWT_SECRET = 'your-secret-key'; // 生产环境应使用环境变量 const JWT_SECRET = 'your-secret-key'; // 生产环境应使用环境变量
const JWT_EXPIRES_IN = '7d'; // 7天有效期 const JWT_EXPIRES_IN = '7d'; // 7天有效期
const CODE_EXPIRATION = 5 * 60; // 5分钟有效期(秒)
const CODE_KEY_PREFIX = 'auth:code:';
export class AuthService { export class AuthService {
private userService: UserService; private userService: UserService;
@@ -52,35 +50,6 @@ export class AuthService {
} }
} }
generateFixedCode(phone: string): string {
// 基于手机号生成固定6位验证码
const phoneHash = Array.from(phone).reduce((hash, char) =>
((hash << 5) - hash + char.charCodeAt(0)) | 0, 0);
const code = Math.abs(phoneHash % 900000 + 100000).toString();
// 位数不足时补0
return code.padEnd(6, '0');
}
async generateRandCode(phone: string): Promise<string> {
try {
const code = Math.floor(100000 + Math.random() * 900000).toString();
const key = `${CODE_KEY_PREFIX}${phone}`;
return code;
} catch (error) {
console.error('Redis set code error:', error);
throw new Error('验证码生成失败');
}
}
async verifyCode(phone: string, code: string): Promise<boolean> {
if (this.generateFixedCode(phone) === code) {
return true;
}
return true;
}
async logout(token: string): Promise<void> { async logout(token: string): Promise<void> {
try { try {
// 验证token有效性 // 验证token有效性