第二次提交
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
|
||||
import '../../models/user_model.dart';
|
||||
import '../auth_http_datasource.dart';
|
||||
@@ -11,7 +12,7 @@ class AuthHttpDataSourceImpl implements AuthHttpDataSource {
|
||||
@override
|
||||
Future<UserModel> login(String username, String password, sourceType) async {
|
||||
final response = await dio.post(
|
||||
'/login',
|
||||
HttpApiConsts.loginUrl,
|
||||
data: {
|
||||
'username': username,
|
||||
'password': password,
|
||||
@@ -19,11 +20,21 @@ class AuthHttpDataSourceImpl implements AuthHttpDataSource {
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print('runtimeType = ${response.data.runtimeType}');
|
||||
return UserModel.fromJson(response.data);
|
||||
} else {
|
||||
throw Exception(response.data['message'] ?? 'Login failed');
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
if (responseData['code'] != 200) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
final loginUsername = responseData['username'];
|
||||
if (loginUsername == null || loginUsername == '') {
|
||||
throw Exception('服务器返回的数据结构不完整(缺少用户信息)');
|
||||
}
|
||||
var userModel = UserModel.fromJson(responseData);
|
||||
return userModel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:maibu_satabot_v2/core/data/base_model.dart';
|
||||
|
||||
import '../../../../core/domain/entities/user_entity.dart';
|
||||
|
||||
class UserModel extends UserEntity {
|
||||
class UserModel extends UserEntity implements BaseModel {
|
||||
UserModel({
|
||||
required super.userId,
|
||||
required super.username,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user