You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.1 KiB
92 lines
2.1 KiB
/// API响应模型基类
|
|
class BaseResponse<T> {
|
|
final int code;
|
|
final String message;
|
|
final T? data;
|
|
|
|
BaseResponse({
|
|
required this.code,
|
|
required this.message,
|
|
this.data,
|
|
});
|
|
|
|
factory BaseResponse.fromJson(Map<String, dynamic> 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<String, dynamic> toJson(Object? Function(T?)? toJsonT) {
|
|
final Map<String, dynamic> data = <String, dynamic>{};
|
|
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<T> {
|
|
final List<T> list;
|
|
final int total;
|
|
final int page;
|
|
final int pageSize;
|
|
final bool hasMore;
|
|
|
|
PaginatedResponse({
|
|
required this.list,
|
|
required this.total,
|
|
required this.page,
|
|
required this.pageSize,
|
|
this.hasMore = false,
|
|
});
|
|
|
|
factory PaginatedResponse.fromJson(
|
|
Map<String, dynamic> json,
|
|
T Function(dynamic) fromJsonT,
|
|
) {
|
|
final list = <T>[];
|
|
if (json['list'] != null && json['list'] is List) {
|
|
for (final item in json['list']) {
|
|
list.add(fromJsonT(item));
|
|
}
|
|
}
|
|
|
|
return PaginatedResponse(
|
|
list: list,
|
|
total: json['total'] ?? 0,
|
|
page: json['page'] ?? 1,
|
|
pageSize: json['pageSize'] ?? 10,
|
|
hasMore: json['hasMore'] ?? false,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson(Object Function(T) toJsonT) {
|
|
return {
|
|
'list': list.map((e) => toJsonT(e)).toList(),
|
|
'total': total,
|
|
'page': page,
|
|
'pageSize': pageSize,
|
|
'hasMore': hasMore,
|
|
};
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'PaginatedResponse{list: $list, total: $total, page: $page, pageSize: $pageSize, hasMore: $hasMore}';
|
|
}
|
|
}
|