rpc.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'client_base.dart';
  5. import 'interceptor.dart';
  6. import 'services/index.dart';
  7. export 'services/index.dart';
  8. export 'request.dart';
  9. export 'exception.dart';
  10. export 'interceptor.dart';
  11. export 'listener/listener.dart';
  12. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  13. /// 默认地址标识
  14. const String _defaultAddressSign = "FLYINSONO";
  15. /// JSON-RPC 代理
  16. class JsonRpcProxy {
  17. JsonRpcProxy({
  18. this.defaultServerHost = "cloud.fis.plus",
  19. this.platformHost = "platform.fis.plus",
  20. }) {
  21. _currentHost = this.defaultServerHost;
  22. }
  23. /// 默认主机地址
  24. final String defaultServerHost;
  25. /// 宿主平台代理地址
  26. final String platformHost;
  27. late String _currentHost;
  28. /// 当前服务主机地址
  29. String get currentHostAddress => "http://$_currentHost";
  30. String get defaultAddressSign => _defaultAddressSign;
  31. HashMap<Type, dynamic> _serviceCache = HashMap();
  32. static PlatformService? _platformService;
  33. /* 服务代理设置 Start */
  34. /// 平台服务
  35. PlatformService get platform {
  36. if (_platformService == null)
  37. _platformService = PlatformService("http://$platformHost");
  38. return _platformService!;
  39. }
  40. AIDiagnosisService get aIDiagnosis =>
  41. findService(() => new AIDiagnosisService(currentHostAddress));
  42. AuthenticationService get authentication =>
  43. findService(() => new AuthenticationService(currentHostAddress));
  44. ConnectService get connect =>
  45. findService(() => new ConnectService(currentHostAddress));
  46. DeviceService get device =>
  47. findService(() => new DeviceService(currentHostAddress));
  48. EmailService get email =>
  49. findService(() => new EmailService(currentHostAddress));
  50. IdentityApplyService get identityApply =>
  51. findService(() => new IdentityApplyService(currentHostAddress));
  52. LoginService get login =>
  53. findService(() => new LoginService(currentHostAddress));
  54. NotificationService get notification =>
  55. findService(() => new NotificationService(currentHostAddress));
  56. OrganizationService get organization =>
  57. findService(() => new OrganizationService(currentHostAddress));
  58. PatientService get patient =>
  59. findService(() => new PatientService(currentHostAddress));
  60. PositionService get position =>
  61. findService(() => new PositionService(currentHostAddress));
  62. RankService get rank =>
  63. findService(() => new RankService(currentHostAddress));
  64. RecordInfoService get recordInfo =>
  65. findService(() => new RecordInfoService(currentHostAddress));
  66. RegionService get region =>
  67. findService(() => new RegionService(currentHostAddress));
  68. RemedicalService get remedical =>
  69. findService(() => new RemedicalService(currentHostAddress));
  70. ReportService get report =>
  71. findService(() => new ReportService(currentHostAddress));
  72. RoleService get role =>
  73. findService(() => new RoleService(currentHostAddress));
  74. SMSService get sMS =>
  75. findService(() => new SMSService(currentHostAddress));
  76. StorageService get storage =>
  77. findService(() => new StorageService(currentHostAddress));
  78. UserService get user =>
  79. findService(() => new UserService(currentHostAddress));
  80. DBLogService get dBLog =>
  81. findService(() => new DBLogService(currentHostAddress));
  82. /* 服务代理设置 End */
  83. /// 设置服务主机地址
  84. void setServerHost(String address) {
  85. if (address == _defaultAddressSign) {
  86. address = defaultServerHost;
  87. }
  88. _currentHost = address;
  89. }
  90. /// 添加拦截器
  91. void addInterceptor(JsonRpcInterceptor interceptor) =>
  92. jsonRpcInterceptHost.addInterceptor(interceptor);
  93. /// 清空缓存
  94. void clearCache() => _serviceCache.clear();
  95. /// 查找Service实例
  96. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  97. Type serviceType = typeOf<T>();
  98. if (!_serviceCache.containsKey(serviceType)) {
  99. _serviceCache[serviceType] = builder.call();
  100. }
  101. return _serviceCache[serviceType] as T;
  102. }
  103. }