/// API响应模型基类 class BaseResponse { final int code; final String message; final T? data; BaseResponse({ required this.code, required this.message, this.data, }); factory BaseResponse.fromJson(Map json, T Function(dynamic)? fromJsonT) { return BaseResponse( code: json['code'] ?? 0, message: json['message'] ?? '', data: fromJsonT != null && json.containsKey('data') && json['data'] != null ? fromJsonT(json['data']) : null, ); } Map toJson(Object? Function(T?)? toJsonT) { final Map data = {}; data['code'] = code; data['message'] = message; if (this.data != null && toJsonT != null) { data['data'] = toJsonT(this.data); } return data; } /// 是否请求成功 bool get isSuccess => code == 0; @override String toString() { return 'BaseResponse{code: $code, message: $message, data: $data}'; } } /// 分页响应模型 class PaginatedResponse { final List records; // 数据列表 final int total; // 总记录数 final int size; // 每页大小 final int current; // 当前页码 final int pages; // 总页数 PaginatedResponse({ required this.records, required this.total, required this.size, required this.current, required this.pages, }); /// 是否还有更多数据 bool get hasMore => current < pages; factory PaginatedResponse.fromJson( Map json, T Function(dynamic) fromJsonT, ) { final records = []; // 支持 records 或 list 字段名 final listData = json['records'] ?? json['list']; if (listData != null && listData is List) { for (final item in listData) { records.add(fromJsonT(item)); } } return PaginatedResponse( records: records, total: json['total'] ?? 0, size: json['size'] ?? json['pageSize'] ?? 10, current: json['current'] ?? json['page'] ?? 1, pages: json['pages'] ?? 1, ); } Map toJson(Object Function(T) toJsonT) { return { 'records': records.map((e) => toJsonT(e)).toList(), 'total': total, 'size': size, 'current': current, 'pages': pages, }; } @override String toString() { return 'PaginatedResponse{records: ${records.length} items, total: $total, current: $current, pages: $pages, size: $size}'; } }