resp.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:sport/bean/page.dart';
  2. import '../Converter.dart';
  3. class Resp {
  4. int code;
  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'],
  16. json['data'] != null ? Converter.fromJson<T>(json['data']) : null);
  17. }
  18. }
  19. class RespList<T> extends Resp {
  20. List<T> results;
  21. RespList(code, msg, this.results) : super(code, msg);
  22. factory RespList.fromJson(Map<String, dynamic> json) {
  23. List<T> results = List<T>();
  24. if (json['data'] != null) {
  25. (json['data'] as List).forEach((element) {
  26. results.add(Converter.fromJson<T>(element));
  27. });
  28. }
  29. return RespList(json['code'], json['msg'], results);
  30. }
  31. }
  32. class PageResult<T> {
  33. List<T> results;
  34. Page page;
  35. PageResult({this.results, this.page});
  36. }
  37. class RespPage<T> extends Resp {
  38. PageResult<T> pageResult;
  39. RespPage(code, msg, this.pageResult) : super(code, msg);
  40. factory RespPage.fromJson(Map<String, dynamic> json) {
  41. PageResult<T> pageResult = PageResult();
  42. List<T> results = List();
  43. if (json['data'] != null) {
  44. Map<String, dynamic> data = json['data'];
  45. if (data['list'] != null) {
  46. (data['list'] as List).forEach((element) {
  47. results.add(Converter.fromJson<T>(element));
  48. });
  49. pageResult.results = results;
  50. } else if (data['data'] != null) {
  51. (data['data'] as List).forEach((element) {
  52. results.add(Converter.fromJson<T>(element));
  53. });
  54. pageResult.results = results;
  55. }
  56. Page page = Page();
  57. if (data['pages'] != null) {
  58. page = Page.fromJson(data['pages'] as Map<String, dynamic>);
  59. pageResult.page = page;
  60. }
  61. }
  62. return RespPage(json['code'], json['msg'], pageResult);
  63. }
  64. }
  65. class ResultsTest<T> {
  66. List<T> list;
  67. Map map;
  68. }
  69. class RespListTest<T> extends Resp {
  70. ResultsTest<T> results;
  71. RespListTest(code, msg, this.results) : super(code, msg);
  72. factory RespListTest.fromJson(Map<String, dynamic> json) {
  73. ResultsTest<T> results = ResultsTest<T>();
  74. Map<String, List<int>> _map = new Map();
  75. if (json['data'] != null) {
  76. results.list = [];
  77. (json['data']["list"] as List).forEach((element) {
  78. results.list.add(Converter.fromJson<T>(element));
  79. });
  80. if (json['data']["map"] != null) {
  81. results.map = json['data']["map"];
  82. }
  83. }
  84. print("[results]:${results.map}--------------------------");
  85. return RespListTest(json['code'], json['msg'], results);
  86. }
  87. }