91 lines
2.7 KiB
Dart
91 lines
2.7 KiB
Dart
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:networks_sdk/networks_sdk.dart';
|
||
|
||
import '../../../core/foundation/api_paths.dart';
|
||
import '../../../domain/entities/user.dart';
|
||
|
||
part 'login_request.g.dart';
|
||
|
||
/// # /auth/login — 登录接口
|
||
///
|
||
/// 一个端点 = 一个文件,Response DTO + Request 放在同一文件中。
|
||
///
|
||
/// ## 数据流位置
|
||
///
|
||
/// ```
|
||
/// AuthRepositoryImpl.login(email, password)
|
||
/// → _client.executeRequest( ★ LoginRequest ★ ) ← 你在这里
|
||
/// → 服务端 POST /auth/login
|
||
/// → 响应 JSON → ★ LoginData ★ ← 也在这里
|
||
/// → LoginData.toEntity() → User
|
||
/// ```
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Response DTO
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 登录响应 DTO
|
||
///
|
||
/// 服务端返回的登录数据,包含 token 和用户信息。
|
||
/// 通过 [toEntity] 转换为 Domain Entity [User]。
|
||
@JsonSerializable()
|
||
class LoginData {
|
||
final String token;
|
||
@JsonKey(name: 'user_id')
|
||
final String userId;
|
||
final String email;
|
||
final String? nickname;
|
||
final String? avatar;
|
||
|
||
const LoginData({
|
||
required this.token,
|
||
required this.userId,
|
||
required this.email,
|
||
this.nickname,
|
||
this.avatar,
|
||
});
|
||
|
||
factory LoginData.fromJson(Map<String, dynamic> json) =>
|
||
_$LoginDataFromJson(json);
|
||
|
||
Map<String, dynamic> toJson() => _$LoginDataToJson(this);
|
||
|
||
/// DTO → Domain Entity
|
||
User toEntity() {
|
||
return User(
|
||
id: userId,
|
||
email: email,
|
||
nickname: nickname,
|
||
avatar: avatar,
|
||
);
|
||
}
|
||
}
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Request
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 登录请求
|
||
///
|
||
/// `@ApiRequest` 自动生成 `_$LoginRequestApi` mixin,
|
||
/// 提供 path / method / requestType / includeToken / fromJson 自动注册。
|
||
@ApiRequest(
|
||
path: ApiPaths.authLogin,
|
||
method: HttpMethod.post,
|
||
responseType: LoginData,
|
||
requestType: ApiRequestType.login,
|
||
)
|
||
@JsonSerializable()
|
||
class LoginRequest extends ApiRequestable<LoginData> with _$LoginRequestApi {
|
||
final String email;
|
||
final String password;
|
||
|
||
LoginRequest({required this.email, required this.password});
|
||
|
||
factory LoginRequest.fromJson(Map<String, dynamic> json) =>
|
||
_$LoginRequestFromJson(json);
|
||
|
||
@override
|
||
Map<String, dynamic> toJson() => _$LoginRequestToJson(this);
|
||
}
|