第二次提交

This commit is contained in:
cc
2026-01-18 20:21:14 +08:00
parent 4346601481
commit 289e706337
129 changed files with 4615 additions and 135 deletions

View File

@@ -1,37 +1,40 @@
import 'package:dio/dio.dart';
import 'package:fpdart/fpdart.dart';
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
import '../../../../core/domain/entities/user_entity.dart';
import '../../domain/errors/auth_failure.dart';
import '../../domain/repositories/auth_repository.dart';
import '../datasources/auth_http_datasource.dart';
class AuthRepositoryImpl implements AuthRepository {
final AuthHttpDataSource remote;
final AuthHttpDataSource httpRemote;
final UserStorage localTokenStorage;
AuthRepositoryImpl({required this.remote, required this.localTokenStorage});
AuthRepositoryImpl({
required this.httpRemote,
required this.localTokenStorage,
});
@override
// 注意 1:返回类型应该是 Domain 层的 UserEntity,而不是 UserModel
Future<UserEntity> login(
Future<Either<AuthFailure, UserEntity>> login(
String username,
String password,
int sourceType,
) async {
try {
// 1. 从数据源获取 Data Model
final userModel = await remote.login(username, password, sourceType);
final userModel = await httpRemote.login(username, password, sourceType);
// 2. 职责核心:将 Model 转换为 Entity (解耦业务与 JSON 结构)
if (userModel != null) {
await localTokenStorage.saveUser(userModel);
}
await localTokenStorage.saveUser(userModel);
// 2. 只返回纯粹的用户信息实体
return userModel.toEntity();
return Right(userModel.toEntity());
} on DioException catch (e) {
final String serverMessage = e.response?.data['msg'] ?? "网络连接异常";
return Left(AuthFailure(serverMessage));
} catch (e) {
// 3. 职责核心:异常翻译
// 不直接抛出 Dio 异常,而是抛出业务层定义的自定义异常或返回 Failure
throw Exception(e.toString());
final cleanMessage = e.toString().replaceFirst('Exception: ', '');
return Left(AuthFailure(cleanMessage));
}
}
}