From ceee3167cbd6c7ad0836f5623574d42dd17016d5 Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 20 Dec 2025 19:22:27 -0800 Subject: [PATCH] =?UTF-8?q?feat(identity-service):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=89=8B=E5=8A=A8=E9=92=B1=E5=8C=85=E9=87=8D=E8=AF=95=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 功能: - 新增 POST /user/wallet/retry 接口 - 用户可主动触发钱包生成重试 - 自动检查钱包是否已完成,避免重复生成 - 幂等操作:重新发布 UserAccountCreatedEvent 实现: - UserAccountController: 添加 wallet/retry 端点 - UserApplicationService: 实现 retryWalletGeneration 方法 - 重用现有的 createWalletGenerationEvent 方法 - 更新 Redis 状态为 pending 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- .../controllers/user-account.controller.ts | 9 ++++ .../services/user-application.service.ts | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/backend/services/identity-service/src/api/controllers/user-account.controller.ts b/backend/services/identity-service/src/api/controllers/user-account.controller.ts index fead7418..d37d8eef 100644 --- a/backend/services/identity-service/src/api/controllers/user-account.controller.ts +++ b/backend/services/identity-service/src/api/controllers/user-account.controller.ts @@ -212,6 +212,15 @@ export class UserAccountController { ); } + @Post('wallet/retry') + @ApiBearerAuth() + @ApiOperation({ summary: '手动重试钱包生成', description: '当钱包生成失败或超时时,用户可手动触发重试' }) + @ApiResponse({ status: 200, description: '重试请求已提交' }) + async retryWalletGeneration(@CurrentUser() user: CurrentUserData) { + await this.userService.retryWalletGeneration(user.userId); + return { message: '钱包生成重试已触发,请稍后查询钱包状态' }; + } + @Put('mnemonic/backup') @ApiBearerAuth() @ApiOperation({ summary: '标记助记词已备份' }) diff --git a/backend/services/identity-service/src/application/services/user-application.service.ts b/backend/services/identity-service/src/application/services/user-application.service.ts index 1c417bec..fed6c3e7 100644 --- a/backend/services/identity-service/src/application/services/user-application.service.ts +++ b/backend/services/identity-service/src/application/services/user-application.service.ts @@ -807,6 +807,59 @@ export class UserApplicationService { }; } + /** + * 手动重试钱包生成 + * + * 用户可以通过此方法手动触发钱包生成重试 + * 幂等操作:重新发布 UserAccountCreatedEvent + */ + async retryWalletGeneration(userId: string): Promise { + this.logger.log(`[WALLET-RETRY] Manual retry requested for user: ${userId}`); + + try { + // 1. 获取用户账号信息 + const userIdObj = UserId.create(BigInt(userId)); + const account = await this.userRepository.findById(userIdObj); + + if (!account) { + throw new ApplicationError('用户不存在'); + } + + // 2. 检查钱包是否已经生成完成 + const wallets = account.getAllWalletAddresses(); + const kavaWallet = wallets.find(w => w.chainType === ChainType.KAVA); + const dstWallet = wallets.find(w => w.chainType === ChainType.DST); + const bscWallet = wallets.find(w => w.chainType === ChainType.BSC); + + if (kavaWallet && dstWallet && bscWallet) { + this.logger.log(`[WALLET-RETRY] Wallet already complete for user: ${userId}`); + return; // 钱包已完成,无需重试 + } + + // 3. 重新触发钱包生成流程 + const event = account.createWalletGenerationEvent(); + await this.eventPublisher.publish(event); + + this.logger.log(`[WALLET-RETRY] Wallet generation retry triggered for user: ${userId}`); + + // 4. 更新 Redis 状态为 pending(等待重新生成) + const statusData = { + status: 'pending', + userId, + updatedAt: new Date().toISOString(), + }; + + await this.redisService.set( + `keygen:status:${userId}`, + JSON.stringify(statusData), + 60 * 60 * 24, // 24 小时 + ); + } catch (error) { + this.logger.error(`[WALLET-RETRY] Failed to retry wallet generation for user ${userId}`, error); + throw new ApplicationError('钱包生成重试失败,请稍后再试'); + } + } + /** * 获取用户的恢复助记词 *