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 22cb92ed..dade703c 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 @@ -779,7 +779,9 @@ export class UserApplicationService { const account = await this.userRepository.findByReferralCode( ReferralCode.create(query.referralCode), ); - if (!account) return null; + if (!account) { + throw new ApplicationError('推荐码不存在或无效'); + } return { userId: account.userId.toString(), diff --git a/frontend/mobile-app/lib/core/services/account_service.dart b/frontend/mobile-app/lib/core/services/account_service.dart index 1aa382f4..32cd91bc 100644 --- a/frontend/mobile-app/lib/core/services/account_service.dart +++ b/frontend/mobile-app/lib/core/services/account_service.dart @@ -1422,13 +1422,27 @@ class AccountService { final response = await _apiClient.get( '/user/by-referral-code/$referralCode', ); - debugPrint('$_tag verifyReferralCode() - 推荐码有效'); - final responseData = response.data as Map; - return responseData['data'] as Map? ?? responseData; + debugPrint('$_tag verifyReferralCode() - API 响应: ${response.data}'); + + // 检查响应数据 + final responseData = response.data as Map?; + if (responseData == null) { + debugPrint('$_tag verifyReferralCode() - 响应数据为 null'); + throw ApiException('推荐码不存在或无效'); + } + + final data = responseData['data']; + if (data == null) { + debugPrint('$_tag verifyReferralCode() - data 字段为 null'); + throw ApiException('推荐码不存在或无效'); + } + + debugPrint('$_tag verifyReferralCode() - 推荐码有效'); + return data as Map; } on ApiException catch (e) { - debugPrint('$_tag verifyReferralCode() - 推荐码无效: $e'); - throw ApiException('推荐码不存在或无效'); + debugPrint('$_tag verifyReferralCode() - API 异常: $e'); + rethrow; } catch (e, stackTrace) { debugPrint('$_tag verifyReferralCode() - 未知异常: $e'); debugPrint('$_tag verifyReferralCode() - 堆栈: $stackTrace');