model.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'package:fis_common/index.dart';
  2. import 'package:flyinsono/architecture/app_parameters.dart';
  3. const String DefaultServerName = "FLYINSONO";
  4. ///配置数据模型
  5. class ConfigModel {
  6. ConfigModel({
  7. required this.common,
  8. required this.server,
  9. required this.flyinsonoServers,
  10. });
  11. _ServerModule server;
  12. List<String> flyinsonoServers;
  13. _CommonModule common;
  14. ///根据静态配置构造的词典创建配置数据模型实例
  15. factory ConfigModel.fromJson(Map<String, dynamic> map) {
  16. List<String> flyinsonoServers = [];
  17. if (map.containsKey('flyinsonoServers')) {
  18. final jsonList = map['flyinsonoServers'] as List<dynamic>;
  19. for (var f in jsonList) {
  20. flyinsonoServers.add(f);
  21. }
  22. } else {
  23. flyinsonoServers = AppParameters.data.flyinsonoServers;
  24. }
  25. return ConfigModel(
  26. common: _CommonModule.fromJson(map["common"] ?? {}),
  27. server: _ServerModule.fromJson(map['server'] ?? {}),
  28. flyinsonoServers: flyinsonoServers,
  29. );
  30. }
  31. ///将配置序列化成Json
  32. Map<String, dynamic> toJson() {
  33. Map<String, dynamic> map = {};
  34. map['common'] = common.toJson();
  35. map['server'] = server.toJson();
  36. map['flyinsonoServers'] = flyinsonoServers;
  37. return map;
  38. }
  39. }
  40. class _CommonModule {
  41. String dateTimeFormat;
  42. _CommonModule({
  43. required this.dateTimeFormat,
  44. });
  45. factory _CommonModule.fromJson(Map<String, dynamic> map) {
  46. return _CommonModule(
  47. dateTimeFormat: map['dateTimeFormat'] ?? "yyyy-MM-dd HH:mm",
  48. );
  49. }
  50. Map<String, dynamic> toJson() {
  51. Map<String, dynamic> map = {};
  52. map['dateTimeFormat'] = dateTimeFormat;
  53. return map;
  54. }
  55. }
  56. class _ServerModule {
  57. String? _current;
  58. Uri? _currentUri;
  59. /// 当前地址
  60. String? get current => _current;
  61. set current(String? value) {
  62. if (value != _current) {
  63. _currentUri = null;
  64. _current = value;
  65. }
  66. }
  67. /// 选项资源
  68. List<String> optionSource;
  69. _ServerModule({
  70. required String? currentUrl,
  71. required this.optionSource,
  72. }) {
  73. _current = currentUrl;
  74. }
  75. Uri? get currentUri {
  76. if (current.isNullOrEmpty || current == DefaultServerName) {
  77. return null;
  78. }
  79. if (_currentUri == null) {
  80. _currentUri = Uri.parse(current!);
  81. }
  82. return _currentUri!;
  83. }
  84. factory _ServerModule.fromJson(Map<String, dynamic> map) {
  85. List<String> options;
  86. if (map['optionSource'] != null) {
  87. options = (map['optionSource'] as List<dynamic>)
  88. .map((e) => e.toString())
  89. .toList();
  90. } else {
  91. options = [];
  92. }
  93. return _ServerModule(
  94. currentUrl: map['current'],
  95. optionSource: options,
  96. );
  97. }
  98. Map<String, dynamic> toJson() {
  99. Map<String, dynamic> map = {};
  100. if (current != null) {
  101. map['current'] = current;
  102. }
  103. map['optionSource'] = optionSource;
  104. return map;
  105. }
  106. }