rpc.dart 3.4 KB

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