Merge branch 'dev' into happi/dev/database-update
# Conflicts: # apps/im_app/lib/features/login/presentation/login_view_model.dart
This commit is contained in:
@@ -1,27 +1,34 @@
|
||||
import 'package:networks_sdk/networks_sdk.dart';
|
||||
|
||||
import '../../domain/entities/user.dart';
|
||||
import '../../domain/repositories/auth_repository.dart';
|
||||
import '../remote/login_request.dart';
|
||||
import '../remote/logout_request.dart';
|
||||
import 'package:im_app/domain/entities/user.dart';
|
||||
import 'package:im_app/domain/repositories/auth_repository.dart';
|
||||
import 'package:im_app/data/remote/login_request.dart';
|
||||
import 'package:im_app/data/remote/logout_request.dart';
|
||||
import 'package:im_app/data/remote/send_otp_request.dart';
|
||||
import 'package:im_app/data/remote/verify_otp_request.dart';
|
||||
|
||||
/// 认证 Repository 实现
|
||||
///
|
||||
/// implements [AuthRepository] 接口(domain/repositories/ 中定义)。
|
||||
/// 直接使用 [NetworksSdkApi] 发送请求,将 DTO 转为 Domain Entity。
|
||||
/// 后续可加 Local DataSource 实现离线缓存。
|
||||
///
|
||||
/// ## 数据流位置
|
||||
/// ## 登录流程
|
||||
///
|
||||
/// ```
|
||||
/// LoginUseCase.execute(email, password)
|
||||
/// → ★ AuthRepositoryImpl.login() ★ ← 你在这里
|
||||
/// LoginUseCase.sendOtp(countryCode, contact)
|
||||
/// → ★ AuthRepositoryImpl.sendOtp() ★ ← 你在这里(步骤 1)
|
||||
/// → NetworksSdkApi.executeRequest(SendOtpRequest)
|
||||
/// → 服务端 POST /app/api/auth/otp/send
|
||||
///
|
||||
/// LoginUseCase.verifyAndLogin(countryCode, contact, code)
|
||||
/// → ★ AuthRepositoryImpl.verifyOtp() ★ ← 你在这里(步骤 2)
|
||||
/// → NetworksSdkApi.executeRequest(VerifyOtpRequest)
|
||||
/// → 服务端 POST /app/api/auth/vcode/check
|
||||
/// ← VerifyOtpResponse.token = vcode_token
|
||||
/// → ★ AuthRepositoryImpl.login() ★ ← 你在这里(步骤 3)
|
||||
/// → NetworksSdkApi.executeRequest(LoginRequest)
|
||||
/// → 服务端 POST /auth/login
|
||||
/// ← LoginResponse(SDK 已拆包 { code, message, data } envelope)
|
||||
/// → _onTokenUpdate(accessToken) ← 回调写入 Token
|
||||
/// ← LoginResponse.toEntity() → User ← DTO → Entity 转换在这里
|
||||
/// ← User(Domain Entity)
|
||||
/// → 服务端 POST /app/api/auth/login-user
|
||||
/// ← LoginResponse → _onTokenUpdate(accessToken) → User
|
||||
/// ```
|
||||
class AuthRepositoryImpl implements AuthRepository {
|
||||
final NetworksSdkApi _client;
|
||||
@@ -34,29 +41,62 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
_onTokenUpdate = onTokenUpdate;
|
||||
|
||||
@override
|
||||
Future<User> login({required String email, required String password}) async {
|
||||
final LoginResponse? loginResponse = await _client.executeRequest(
|
||||
LoginRequest(email: email, password: password),
|
||||
Future<void> sendOtp({
|
||||
required String countryCode,
|
||||
required String contact,
|
||||
}) async {
|
||||
await _client.executeRequest(
|
||||
SendOtpRequest(countryCode: countryCode, contact: contact),
|
||||
);
|
||||
|
||||
if (loginResponse == null) {
|
||||
throw Exception('Login failed: empty response');
|
||||
}
|
||||
|
||||
_onTokenUpdate(loginResponse.accessToken);
|
||||
|
||||
return loginResponse.toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<User?> getCurrentUser() async {
|
||||
// TODO: 从本地存储获取用户信息
|
||||
return null;
|
||||
Future<String> verifyOtp({
|
||||
required String countryCode,
|
||||
required String contact,
|
||||
required String code,
|
||||
}) async {
|
||||
final response = await _client.executeRequest(
|
||||
VerifyOtpRequest(
|
||||
countryCode: countryCode,
|
||||
contact: contact,
|
||||
code: code,
|
||||
),
|
||||
);
|
||||
|
||||
if (response == null) {
|
||||
throw Exception('Verify OTP failed: empty response');
|
||||
}
|
||||
|
||||
return response.token;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<User> login({
|
||||
required String countryCode,
|
||||
required String contact,
|
||||
required String vcodeToken,
|
||||
}) async {
|
||||
final response = await _client.executeRequest(
|
||||
LoginRequest(
|
||||
countryCode: countryCode,
|
||||
contact: contact,
|
||||
vcodeToken: vcodeToken,
|
||||
),
|
||||
);
|
||||
|
||||
if (response == null) {
|
||||
throw Exception('Login failed: empty response');
|
||||
}
|
||||
|
||||
_onTokenUpdate(response.accessToken);
|
||||
|
||||
return response.toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> logout() async {
|
||||
await _client.executeRequest(LogoutRequest());
|
||||
_onTokenUpdate(null); // 回调清除 Token(内存 + 持久化由 Provider 层组合)
|
||||
_onTokenUpdate(null); // 清除 Token(内存 + 持久化由 Provider 层组合)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user