| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import 'package:chicken_farm/core/api/api_service.dart';
- import 'package:chicken_farm/modes/system/dict.dart';
- class DictApi {
- static final DictApi _instance = DictApi._internal();
- factory DictApi() => _instance;
- DictApi._internal();
- Future<dynamic> getTypes() async {
- return await ApiService().get(
- '/system/dict/type/list',
- queryParameters: {'pageSize': 10000},
- );
- }
- Future<dynamic> getType(String id) async {
- return await ApiService().get('/system/dict/type/$id');
- }
- // 修改此方法以正确处理返回的字典数据列表
- Future<List<DictDataModel>?> getDicts(String type) async {
- try {
- final response = await ApiService().get('/system/dict/data/type/$type');
- if (response != null) {
- // 根据RuoYi的标准API格式处理响应
- if (response is List) {
- // 直接是列表的情况
- return response
- .map(
- (item) => item is Map<String, dynamic>
- ? DictDataModel.fromJson(item)
- : DictDataModel(
- dictCode: 0,
- dictSort: 0,
- dictLabel: '',
- dictValue: '',
- dictType: type,
- ),
- )
- .toList();
- } else if (response is Map<String, dynamic>) {
- // 包含在data字段中的情况
- if (response['data'] is List) {
- return (response['data'] as List)
- .map(
- (item) => item is Map<String, dynamic>
- ? DictDataModel.fromJson(item)
- : DictDataModel(
- dictCode: 0,
- dictSort: 0,
- dictLabel: '',
- dictValue: '',
- dictType: type,
- ),
- )
- .toList();
- } else if (response['rows'] is List) {
- // 包含在rows字段中的情况
- return (response['rows'] as List)
- .map(
- (item) => item is Map<String, dynamic>
- ? DictDataModel.fromJson(item)
- : DictDataModel(
- dictCode: 0,
- dictSort: 0,
- dictLabel: '',
- dictValue: '',
- dictType: type,
- ),
- )
- .toList();
- } else {
- // 单个对象的情况
- return [DictDataModel.fromJson(response)];
- }
- }
- }
- return null;
- } catch (e) {
- // 出现异常时返回null
- return null;
- }
- }
- Future<dynamic> refreshCache() async {
- return await ApiService().delete('/system/dict/type/refreshCache');
- }
- }
|