dict_stroe.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:chicken_farm/apis/system/_dict.dart';
  2. import 'package:chicken_farm/core/utils/storage.dart';
  3. import 'package:chicken_farm/modes/system/dict.dart';
  4. class DictStore {
  5. static final DictStore _instance = DictStore._internal();
  6. static const String _localStorgeKey = 'vb_dict_';
  7. final Map<String, List<DictDataModel>> _cache = {};
  8. factory DictStore() => _instance;
  9. DictStore._internal();
  10. /// 根据字典类型获取字典数据列表
  11. /// 先从缓存中查找,如果缓存中没有则调用接口查询
  12. Future<List<DictDataModel>?> getDictByType(String dictType) async {
  13. // 先从内存缓存中查找
  14. if (_cache.containsKey(dictType)) {
  15. return _cache[dictType];
  16. }
  17. // 再从本地存储中查找
  18. // final cachedDict = await StorageUtils.getObject<List<dynamic>>(
  19. // '$_localStorgeKey$dictType',
  20. // );
  21. // if (cachedDict != null) {
  22. // try {
  23. // final dictList = cachedDict
  24. // .map(
  25. // (item) => item is Map<String, dynamic>
  26. // ? DictDataModel.fromJson(item)
  27. // : null,
  28. // )
  29. // .where((item) => item != null)
  30. // .cast<DictDataModel>()
  31. // .toList();
  32. // _cache[dictType] = dictList;
  33. // return dictList;
  34. // } catch (e) {
  35. // // 解析失败,继续从网络获取
  36. // }
  37. // }
  38. // 如果本地都没有,则调用接口查询
  39. final dictData = await DictApi().getDicts(dictType);
  40. if (dictData != null) {
  41. // 存入缓存
  42. _cache[dictType] = dictData;
  43. await StorageUtils.setObject(
  44. '$_localStorgeKey$dictType',
  45. dictData.map((e) => e.toJson()).toList(),
  46. );
  47. return dictData;
  48. }
  49. return null;
  50. }
  51. /// 根据字典类型和值查询标签
  52. Future<String?> getLabelByTypeAndValue(String dictType, String value) async {
  53. final dict = await getDictByTypeAndValue(dictType, value);
  54. return dict?.dictLabel.isNotEmpty == true ? dict?.dictLabel : null;
  55. }
  56. /// 根据字典类型和值查询字典项
  57. Future<DictDataModel?> getDictByTypeAndValue(
  58. String dictType,
  59. String value,
  60. ) async {
  61. final dictList = await getDictByType(dictType);
  62. if (dictList != null) {
  63. final dict = dictList.firstWhere(
  64. (element) => element.dictValue == value,
  65. orElse: () => DictDataModel(
  66. dictCode: 0,
  67. dictSort: 0,
  68. dictLabel: '',
  69. dictValue: '',
  70. dictType: dictType,
  71. ),
  72. );
  73. // 检查是否找到了匹配的字典项
  74. if (dict.dictCode != 0 ||
  75. dict.dictLabel.isNotEmpty ||
  76. dict.dictValue.isNotEmpty) {
  77. return dict;
  78. }
  79. }
  80. return null;
  81. }
  82. /// 清除指定字典类型的缓存
  83. void clearDictCache(String dictType) {
  84. _cache.remove(dictType);
  85. //StorageUtils.remove('$_localStorgeKey$dictType');
  86. }
  87. /// 清除所有字典缓存
  88. void clearAll() {
  89. _cache.clear();
  90. //StorageUtils.removeWithPrefix(_localStorgeKey);
  91. }
  92. }