_dict.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:chicken_farm/core/api/api_service.dart';
  2. import 'package:chicken_farm/modes/system/dict.dart';
  3. class DictApi {
  4. static final DictApi _instance = DictApi._internal();
  5. factory DictApi() => _instance;
  6. DictApi._internal();
  7. Future<dynamic> getTypes() async {
  8. return await ApiService().get(
  9. '/system/dict/type/list',
  10. queryParameters: {'pageSize': 10000},
  11. );
  12. }
  13. Future<dynamic> getType(String id) async {
  14. return await ApiService().get('/system/dict/type/$id');
  15. }
  16. // 修改此方法以正确处理返回的字典数据列表
  17. Future<List<DictDataModel>?> getDicts(String type) async {
  18. try {
  19. final response = await ApiService().get('/system/dict/data/type/$type');
  20. if (response != null) {
  21. // 根据RuoYi的标准API格式处理响应
  22. if (response is List) {
  23. // 直接是列表的情况
  24. return response
  25. .map(
  26. (item) => item is Map<String, dynamic>
  27. ? DictDataModel.fromJson(item)
  28. : DictDataModel(
  29. dictCode: 0,
  30. dictSort: 0,
  31. dictLabel: '',
  32. dictValue: '',
  33. dictType: type,
  34. ),
  35. )
  36. .toList();
  37. } else if (response is Map<String, dynamic>) {
  38. // 包含在data字段中的情况
  39. if (response['data'] is List) {
  40. return (response['data'] as List)
  41. .map(
  42. (item) => item is Map<String, dynamic>
  43. ? DictDataModel.fromJson(item)
  44. : DictDataModel(
  45. dictCode: 0,
  46. dictSort: 0,
  47. dictLabel: '',
  48. dictValue: '',
  49. dictType: type,
  50. ),
  51. )
  52. .toList();
  53. } else if (response['rows'] is List) {
  54. // 包含在rows字段中的情况
  55. return (response['rows'] as List)
  56. .map(
  57. (item) => item is Map<String, dynamic>
  58. ? DictDataModel.fromJson(item)
  59. : DictDataModel(
  60. dictCode: 0,
  61. dictSort: 0,
  62. dictLabel: '',
  63. dictValue: '',
  64. dictType: type,
  65. ),
  66. )
  67. .toList();
  68. } else {
  69. // 单个对象的情况
  70. return [DictDataModel.fromJson(response)];
  71. }
  72. }
  73. }
  74. return null;
  75. } catch (e) {
  76. // 出现异常时返回null
  77. return null;
  78. }
  79. }
  80. Future<dynamic> refreshCache() async {
  81. return await ApiService().delete('/system/dict/type/refreshCache');
  82. }
  83. }