- 移除 @riverpod/@freezed 注解依赖,全部改为手写 Provider(无需 build_runner) · LoginState 改为纯 Dart,LoginViewModel/ThemeViewModel/ChatViewModel 改为 Notifier · UserNotifier 改为 FamilyAsyncNotifier<User?,int>,mini_app_provider 改为手写 Provider · 15 个 StreamProvider/StreamProvider.family 从 @riverpod 迁移至手写 - 发送消息(#25) · SendMessageRequest/SendMessageResponse DTO · SendMessageUseCase:乐观写入 DB → HTTP POST → 更新 Chat 摘要 - 接收消息 WS(#26) · WsMessageService:监听 mode2 WS 帧 → HTTP 补拉 → DB 写入 → Chat 更新 · FetchHistoryRequest/FetchHistoryResponse DTO(GET /app/api/chat/history) · FetchHistoryUseCase:拉取 → insertOrReplaceAll - DI 装配(chat_service_providers.dart) · wsMessageServiceProvider、sendMessageUseCaseProvider、fetchHistoryUseCaseProvider - 聊天列表页(#27) · ChatListViewModel(Notifier<void>)+ chat_page.dart 真实会话列表 UI · ListTile:头像首字母、最新消息摘要、未读角标、时间格式化 - 聊天详情页(#28) · ChatDetailViewModel(FamilyNotifier<ChatDetailState,int>)+ chat_detail_page.dart · 消息气泡(自己/他人分左右)、底部输入框、发送状态与错误提示 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
191 lines
6.4 KiB
Dart
191 lines
6.4 KiB
Dart
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:networks_sdk/networks_sdk.dart';
|
||
|
||
import 'package:im_app/core/foundation/api_paths.dart';
|
||
import 'package:im_app/domain/entities/user.dart';
|
||
|
||
part 'login_request.g.dart';
|
||
|
||
/// # /app/api/auth/login-user — 使用 vcode_token 完成登录
|
||
///
|
||
/// 流程:发送验证码([SendOtpRequest])→ 校验验证码([VerifyOtpRequest])
|
||
/// → ★ 用 vcode_token 登录(本请求)★ → 获得 access_token
|
||
///
|
||
/// ## 数据流位置
|
||
///
|
||
/// ```
|
||
/// AuthRepositoryImpl.login(countryCode, contact, vcodeToken)
|
||
/// → _client.executeRequest( ★ LoginRequest ★ ) ← 你在这里
|
||
/// → 服务端 POST /app/api/auth/login-user
|
||
/// → SDK 拆包 {code, message, data} envelope
|
||
/// → ★ LoginResponse ★ ← 也在这里
|
||
/// → LoginResponse.toEntity() → User
|
||
/// ```
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Response DTO
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 登录响应中的用户档案,嵌套在 [LoginResponse.profile] 中。
|
||
///
|
||
/// 纯 Dart 类,无需任何注解。`_$LoginProfileFromJson` 由生成器从 `@ApiRequest` 声明中自动推导生成。
|
||
class LoginProfile {
|
||
final int uid;
|
||
final String uuid;
|
||
@JsonKey(name: 'last_online')
|
||
final int lastOnline;
|
||
@JsonKey(name: 'profile_pic')
|
||
final String profilePic;
|
||
@JsonKey(name: 'profile_pic_gaussian')
|
||
final String profilePicGaussian;
|
||
final String nickname;
|
||
final String contact;
|
||
@JsonKey(name: 'country_code')
|
||
final String countryCode;
|
||
final String email;
|
||
@JsonKey(name: 'recovery_email')
|
||
final String recoveryEmail;
|
||
final String username;
|
||
final String bio;
|
||
final int relationship;
|
||
@JsonKey(name: 'user_alias')
|
||
final String? userAlias;
|
||
@JsonKey(name: 'channel_id')
|
||
final int channelId;
|
||
@JsonKey(name: 'channel_group_id')
|
||
final int channelGroupId;
|
||
final String hint;
|
||
|
||
const LoginProfile({
|
||
required this.uid,
|
||
required this.uuid,
|
||
required this.lastOnline,
|
||
required this.profilePic,
|
||
required this.profilePicGaussian,
|
||
required this.nickname,
|
||
required this.contact,
|
||
required this.countryCode,
|
||
required this.email,
|
||
required this.recoveryEmail,
|
||
required this.username,
|
||
required this.bio,
|
||
required this.relationship,
|
||
this.userAlias,
|
||
required this.channelId,
|
||
required this.channelGroupId,
|
||
required this.hint,
|
||
});
|
||
|
||
factory LoginProfile.fromJson(Map<String, dynamic> json) => LoginProfile(
|
||
uid: (json['uid'] as num?)?.toInt() ?? 0,
|
||
uuid: json['uuid'] as String? ?? '',
|
||
lastOnline: (json['last_online'] as num?)?.toInt() ?? 0,
|
||
profilePic: json['profile_pic'] as String? ?? '',
|
||
profilePicGaussian: json['profile_pic_gaussian'] as String? ?? '',
|
||
nickname: json['nickname'] as String? ?? '',
|
||
contact: json['contact'] as String? ?? '',
|
||
countryCode: json['country_code'] as String? ?? '',
|
||
email: json['email'] as String? ?? '',
|
||
recoveryEmail: json['recovery_email'] as String? ?? '',
|
||
username: json['username'] as String? ?? '',
|
||
bio: json['bio'] as String? ?? '',
|
||
relationship: (json['relationship'] as num?)?.toInt() ?? 0,
|
||
userAlias: json['user_alias'] as String?,
|
||
channelId: (json['channel_id'] as num?)?.toInt() ?? 0,
|
||
channelGroupId: (json['channel_group_id'] as num?)?.toInt() ?? 0,
|
||
hint: json['hint'] as String? ?? '',
|
||
);
|
||
|
||
User toEntity() => User(
|
||
uid: uid,
|
||
uuid: uuid,
|
||
lastOnline: lastOnline,
|
||
profilePic: profilePic,
|
||
profilePicGaussian: profilePicGaussian,
|
||
nickname: nickname,
|
||
contact: contact,
|
||
countryCode: countryCode,
|
||
email: email,
|
||
recoveryEmail: recoveryEmail,
|
||
username: username,
|
||
bio: bio,
|
||
relationship: relationship,
|
||
userAlias: userAlias,
|
||
hint: hint,
|
||
);
|
||
}
|
||
|
||
/// 登录接口的业务响应数据(对应服务端 `data` 字段,即 T in `APIResponseWrapper<T>`)。
|
||
///
|
||
/// `{ code, message }` 由 SDK 内部的 `ApiResponseWrapper` 统一处理,
|
||
/// App 层只接触此类,不感知 envelope 结构。纯 Dart 类,无需任何注解。
|
||
class LoginResponse {
|
||
@JsonKey(name: 'account_id')
|
||
final String accountId;
|
||
final LoginProfile profile;
|
||
@JsonKey(name: 'access_token')
|
||
final String accessToken;
|
||
@JsonKey(name: 'refresh_token')
|
||
final String refreshToken;
|
||
@JsonKey(name: 'device_id')
|
||
final String deviceId;
|
||
final String nonce;
|
||
@JsonKey(name: 'login_data')
|
||
final String loginData;
|
||
@JsonKey(name: 'is_verified')
|
||
final bool? isVerified;
|
||
|
||
const LoginResponse({
|
||
required this.accountId,
|
||
required this.profile,
|
||
required this.accessToken,
|
||
required this.refreshToken,
|
||
required this.deviceId,
|
||
this.nonce = '',
|
||
this.loginData = '',
|
||
this.isVerified,
|
||
});
|
||
|
||
factory LoginResponse.fromJson(Map<String, dynamic> json) => LoginResponse(
|
||
accountId: json['account_id'] as String? ?? '',
|
||
profile: LoginProfile.fromJson(json['profile'] as Map<String, dynamic>),
|
||
accessToken: json['access_token'] as String? ?? '',
|
||
refreshToken: json['refresh_token'] as String? ?? '',
|
||
deviceId: json['device_id'] as String? ?? '',
|
||
nonce: json['nonce'] as String? ?? '',
|
||
loginData: json['login_data'] as String? ?? '',
|
||
isVerified: json['is_verified'] as bool?,
|
||
);
|
||
|
||
User toEntity() => profile.toEntity();
|
||
}
|
||
|
||
// ─────────────────────────────────────────────
|
||
// Request
|
||
// ─────────────────────────────────────────────
|
||
|
||
/// 使用 vcode_token 完成登录的请求
|
||
///
|
||
/// 上游:[VerifyOtpRequest] 返回的 `token` 即 vcodeToken。
|
||
/// 成功后 [LoginResponse.accessToken] 写入 ApiConfig,后续请求自动携带。
|
||
@ApiRequest(
|
||
path: ApiPaths.authLogin,
|
||
method: HttpMethod.post,
|
||
responseType: LoginResponse,
|
||
requestType: ApiRequestType.login,
|
||
)
|
||
class LoginRequest extends ApiRequestable<LoginResponse>
|
||
with _$LoginRequestApi {
|
||
@JsonKey(name: 'country_code')
|
||
final String countryCode;
|
||
final String contact;
|
||
@JsonKey(name: 'vcode_token')
|
||
final String vcodeToken;
|
||
|
||
LoginRequest({
|
||
required this.countryCode,
|
||
required this.contact,
|
||
required this.vcodeToken,
|
||
});
|
||
}
|