rpc.dart 3.7 KB

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