From c2ddb67b3e15f48585262928db97b2d76fbed599 Mon Sep 17 00:00:00 2001 From: moyin <244344649@qq.com> Date: Wed, 17 Dec 2025 20:22:10 +0800 Subject: [PATCH] =?UTF-8?q?service=EF=BC=9A=E6=9B=B4=E6=96=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E4=B8=9A=E5=8A=A1=E6=9C=8D=E5=8A=A1=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=82=AE=E7=AE=B1=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加发送邮箱验证码服务方法 - 添加验证邮箱验证码服务方法 - 添加重新发送邮箱验证码服务方法 - 集成验证码服务和邮件服务 - 更新相关的单元测试 --- src/business/login/login.service.spec.ts | 1 + src/business/login/login.service.ts | 102 +++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/business/login/login.service.spec.ts b/src/business/login/login.service.spec.ts index 59e1e6c..f3ac346 100644 --- a/src/business/login/login.service.spec.ts +++ b/src/business/login/login.service.spec.ts @@ -20,6 +20,7 @@ describe('LoginService', () => { github_id: null as string | null, avatar_url: null as string | null, role: 1, + email_verified: false, created_at: new Date(), updated_at: new Date() }; diff --git a/src/business/login/login.service.ts b/src/business/login/login.service.ts index 19fb510..dcc1ad0 100644 --- a/src/business/login/login.service.ts +++ b/src/business/login/login.service.ts @@ -287,6 +287,108 @@ export class LoginService { } } + /** + * 发送邮箱验证码 + * + * @param email 邮箱地址 + * @returns 响应结果 + */ + async sendEmailVerification(email: string): Promise> { + try { + this.logger.log(`发送邮箱验证码: ${email}`); + + // 调用核心服务发送验证码 + const verificationCode = await this.loginCoreService.sendEmailVerification(email); + + this.logger.log(`邮箱验证码已发送: ${email}`); + + // 实际应用中不应返回验证码,这里仅用于演示 + return { + success: true, + data: { verification_code: verificationCode }, + message: '验证码已发送,请查收邮件' + }; + } catch (error) { + this.logger.error(`发送邮箱验证码失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '发送验证码失败', + error_code: 'SEND_EMAIL_VERIFICATION_FAILED' + }; + } + } + + /** + * 验证邮箱验证码 + * + * @param email 邮箱地址 + * @param code 验证码 + * @returns 响应结果 + */ + async verifyEmailCode(email: string, code: string): Promise { + try { + this.logger.log(`验证邮箱验证码: ${email}`); + + // 调用核心服务验证验证码 + const isValid = await this.loginCoreService.verifyEmailCode(email, code); + + if (isValid) { + this.logger.log(`邮箱验证成功: ${email}`); + return { + success: true, + message: '邮箱验证成功' + }; + } else { + return { + success: false, + message: '验证码错误', + error_code: 'INVALID_VERIFICATION_CODE' + }; + } + } catch (error) { + this.logger.error(`邮箱验证失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '邮箱验证失败', + error_code: 'EMAIL_VERIFICATION_FAILED' + }; + } + } + + /** + * 重新发送邮箱验证码 + * + * @param email 邮箱地址 + * @returns 响应结果 + */ + async resendEmailVerification(email: string): Promise> { + try { + this.logger.log(`重新发送邮箱验证码: ${email}`); + + // 调用核心服务重新发送验证码 + const verificationCode = await this.loginCoreService.resendEmailVerification(email); + + this.logger.log(`邮箱验证码已重新发送: ${email}`); + + // 实际应用中不应返回验证码,这里仅用于演示 + return { + success: true, + data: { verification_code: verificationCode }, + message: '验证码已重新发送,请查收邮件' + }; + } catch (error) { + this.logger.error(`重新发送邮箱验证码失败: ${email}`, error instanceof Error ? error.stack : String(error)); + + return { + success: false, + message: error instanceof Error ? error.message : '重新发送验证码失败', + error_code: 'RESEND_EMAIL_VERIFICATION_FAILED' + }; + } + } + /** * 格式化用户信息 *