resp.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:sport/bean/page.dart';
  2. import '../Converter.dart';
  3. class Resp {
  4. int code = -1;
  5. String msg;
  6. Resp(this.code, this.msg);
  7. factory Resp.fromJson(Map<String, dynamic> json) {
  8. return Resp(json['code'], json['msg'] ?? "");
  9. }
  10. }
  11. class RespData<T> extends Resp {
  12. T? data;
  13. RespData(code, msg, this.data) : super(code, msg);
  14. factory RespData.fromJson(Map<String, dynamic> json) {
  15. return RespData(json['code'], json['msg'], json['data'] != null ? Converter.fromJson<T>(json['data']) : null);
  16. }
  17. }
  18. class RespList<T> extends Resp {
  19. List<T> results;
  20. RespList(code, msg, this.results) : super(code, msg);
  21. factory RespList.fromJson(Map<String, dynamic> json) {
  22. List<T> results = [];
  23. if (json['data'] != null) {
  24. (json['data'] as List).forEach((element) {
  25. var item = Converter.fromJson<T>(element);
  26. if (item != null) results.add(item);
  27. });
  28. }
  29. return RespList(json['code'], json['msg'], results);
  30. }
  31. }
  32. class PageResult<T> {
  33. List<T>? results;
  34. Page? page;
  35. int count;
  36. PageResult({this.results, this.page, this.count = 0});
  37. }
  38. class RespPage<T> extends Resp {
  39. PageResult<T> pageResult;
  40. RespPage(code, msg, this.pageResult) : super(code, msg);
  41. factory RespPage.fromJson(Map<String, dynamic> json) {
  42. PageResult<T> pageResult = PageResult();
  43. List<T> results = [];
  44. if (json['data'] != null) {
  45. Map<String, dynamic> data = json['data'];
  46. if (data['list'] != null) {
  47. (data['list'] as List).forEach((element) {
  48. var item = Converter.fromJson<T>(element);
  49. if (item != null) results.add(item);
  50. });
  51. pageResult.results = results;
  52. } else if (data['data'] != null) {
  53. (data['data'] as List).forEach((element) {
  54. var item = Converter.fromJson<T>(element);
  55. if (item != null) results.add(item);
  56. });
  57. pageResult.results = results;
  58. }
  59. Page page = Page();
  60. if (data['pages'] != null) {
  61. page = Page.fromJson(data['pages'] as Map<String, dynamic>);
  62. pageResult.page = page;
  63. }
  64. if (data['count'] != null) {
  65. pageResult.count = Converter.toInt(data['count']);
  66. }
  67. }
  68. return RespPage(json['code'], json['msg'], pageResult);
  69. }
  70. }