diff --git a/backend/services/identity-service/src/api/dto/request/register-by-phone.dto.ts b/backend/services/identity-service/src/api/dto/request/register-by-phone.dto.ts index 1b149b3d..c1e57220 100644 --- a/backend/services/identity-service/src/api/dto/request/register-by-phone.dto.ts +++ b/backend/services/identity-service/src/api/dto/request/register-by-phone.dto.ts @@ -54,12 +54,12 @@ export class RegisterByPhoneDto { @IsObject() deviceName?: DeviceNameDto; - @ApiPropertyOptional({ - example: 'RWAABC1234', - description: '邀请人推荐码 (6-20位大写字母和数字)', + @ApiProperty({ + example: 'SEED01', + description: '邀请人推荐码 (6-20位大写字母和数字) - 必填', }) - @IsOptional() @IsString() + @IsNotEmpty({ message: '推荐码不能为空' }) @Matches(/^[A-Z0-9]{6,20}$/, { message: '推荐码格式错误' }) - inviterReferralCode?: string; + inviterReferralCode: string; } 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 71f24baf..f4c393a8 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 @@ -248,26 +248,31 @@ export class UserApplicationService { } this.logger.log(`[REGISTER] Phone number validated`); - // 3. 验证邀请码 - let inviterSequence: AccountSequence | null = null; - if (command.inviterReferralCode) { - const referralCode = ReferralCode.create(command.inviterReferralCode); - const referralValidation = - await this.validatorService.validateReferralCode(referralCode); - if (!referralValidation.isValid) { - this.logger.warn( - `[REGISTER] Referral code invalid: ${command.inviterReferralCode}`, - ); - throw new ApplicationError(referralValidation.errorMessage!); - } - const inviter = - await this.userRepository.findByReferralCode(referralCode); - inviterSequence = inviter!.accountSequence; - this.logger.log( - `[REGISTER] Inviter validated: ${inviterSequence.value}`, - ); + // 3. 验证邀请码 (必填) + if (!command.inviterReferralCode) { + this.logger.warn(`[REGISTER] Missing required referral code`); + throw new ApplicationError('推荐码不能为空'); } + const referralCode = ReferralCode.create(command.inviterReferralCode); + const referralValidation = + await this.validatorService.validateReferralCode(referralCode); + if (!referralValidation.isValid) { + this.logger.warn( + `[REGISTER] Referral code invalid: ${command.inviterReferralCode}`, + ); + throw new ApplicationError(referralValidation.errorMessage!); + } + const inviter = await this.userRepository.findByReferralCode(referralCode); + if (!inviter) { + this.logger.warn( + `[REGISTER] Inviter not found for code: ${command.inviterReferralCode}`, + ); + throw new ApplicationError('推荐码对应的用户不存在'); + } + const inviterSequence = inviter.accountSequence; + this.logger.log(`[REGISTER] Inviter validated: ${inviterSequence.value}`); + // 4. 生成用户序列号 const accountSequence = await this.sequenceGenerator.generateNextUserSequence(); diff --git a/backend/services/referral-service/prisma/seed.ts b/backend/services/referral-service/prisma/seed.ts index b4204ced..e9c80c8b 100644 --- a/backend/services/referral-service/prisma/seed.ts +++ b/backend/services/referral-service/prisma/seed.ts @@ -7,8 +7,8 @@ const prisma = new PrismaClient(); // 用于为未来的注册用户提供初始推荐码 // ============================================ const SEED_USER = { - userId: BigInt(5), - accountSequence: 'D25122100000', // 种子用户 (序号0,真实用户从1开始) + userId: BigInt(25129999999), // 从 accountSequence 去掉 D 前缀 + accountSequence: 'D25129999999', // 种子用户 (特殊序号,区别于真实用户) myReferralCode: 'SEED01', usedReferralCode: null, // 根节点无推荐人 referrerId: null, // 根节点