rpc.dart 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'dart:convert';
  4. import 'package:fis_common/extensions/type.dart';
  5. import 'package:fis_common/logger/logger.dart';
  6. import 'package:fis_jsonrpc/encrpyt.dart';
  7. import 'client_base.dart';
  8. import 'interceptor.dart';
  9. import 'services/index.dart';
  10. export 'services/index.dart';
  11. export 'request.dart';
  12. export 'exception.dart';
  13. export 'interceptor.dart';
  14. export 'notifications/index.dart';
  15. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  16. const C_SHELL_RPC_DEFAULT_HOST = 'platform.fis.plus';
  17. /// JSON-RPC 代理
  18. class JsonRpcProxy {
  19. JsonRpcProxy({
  20. String? host,
  21. this.platformHost = C_SHELL_RPC_DEFAULT_HOST,
  22. }) {
  23. _currentHost = host ?? "unknown";
  24. }
  25. /// 宿主平台代理地址
  26. final String platformHost;
  27. /// 服务主机地址
  28. late String _currentHost;
  29. /// 服务主机协议
  30. late String _currentProtocol;
  31. /// 当前服务主机地址
  32. String get currentHostAddress => "$_currentProtocol://$_currentHost";
  33. HashMap<Type, dynamic> _serviceCache = HashMap();
  34. static PlatformService? _platformService;
  35. /// 请求加密配置
  36. JsonRpcEncryptConfig get requestEncryptConfig =>
  37. JsonRpcClientBase.requestEncryptConfig;
  38. set requestEncryptConfig(JsonRpcEncryptConfig config) {
  39. JsonRpcClientBase.requestEncryptConfig = config;
  40. final cfgJson = jsonEncode(config.toJson());
  41. logger.i("JsonRpcProxy - set RequestEncryptConfig: $cfgJson.");
  42. }
  43. /// 响应加密配置
  44. JsonRpcEncryptConfig get responseEncryptConfig =>
  45. JsonRpcClientBase.responseEncryptConfig;
  46. set responseEncryptConfig(JsonRpcEncryptConfig config) {
  47. JsonRpcClientBase.responseEncryptConfig = config;
  48. final cfgJson = jsonEncode(config.toJson());
  49. logger.i("JsonRpcProxy - set ResponseEncryptConfig: $cfgJson.");
  50. }
  51. /* 服务代理设置 Start */
  52. /// 平台服务
  53. PlatformService get platform {
  54. if (_platformService == null)
  55. _platformService = PlatformService("http://$platformHost", timeout: 15000);
  56. return _platformService!;
  57. }
  58. LiveConsultationService get liveConsultation =>
  59. findService(() => new LiveConsultationService(currentHostAddress));
  60. AIDiagnosisService get aIDiagnosis =>
  61. findService(() => new AIDiagnosisService(currentHostAddress));
  62. AppletAPIService get appletAPI =>
  63. findService(() => new AppletAPIService(currentHostAddress));
  64. ASRService get aSR =>
  65. findService(() => new ASRService(currentHostAddress));
  66. AuthenticationService get authentication =>
  67. findService(() => new AuthenticationService(currentHostAddress));
  68. ChatMessageService get chatMessage =>
  69. findService(() => new ChatMessageService(currentHostAddress));
  70. ConnectService get connect =>
  71. findService(() => new ConnectService(currentHostAddress));
  72. DeployPlatformService get deployPlatform =>
  73. findService(() => new DeployPlatformService(currentHostAddress));
  74. DeviceService get device =>
  75. findService(() => new DeviceService(currentHostAddress));
  76. EducationService get education =>
  77. findService(() => new EducationService(currentHostAddress));
  78. EmailService get email =>
  79. findService(() => new EmailService(currentHostAddress));
  80. IdentityApplyService get identityApply =>
  81. findService(() => new IdentityApplyService(currentHostAddress));
  82. LabService get lab =>
  83. findService(() => new LabService(currentHostAddress));
  84. LoginService get login =>
  85. findService(() => new LoginService(currentHostAddress));
  86. OrganizationService get organization =>
  87. findService(() => new OrganizationService(currentHostAddress));
  88. PatientService get patient =>
  89. findService(() => new PatientService(currentHostAddress));
  90. PaymentService get payment =>
  91. findService(() => new PaymentService(currentHostAddress));
  92. PositionService get position =>
  93. findService(() => new PositionService(currentHostAddress));
  94. RankService get rank =>
  95. findService(() => new RankService(currentHostAddress));
  96. RecognitionService get recognition =>
  97. findService(() => new RecognitionService(currentHostAddress));
  98. RecordInfoService get recordInfo =>
  99. findService(() => new RecordInfoService(currentHostAddress));
  100. RegionService get region =>
  101. findService(() => new RegionService(currentHostAddress));
  102. RegisterService get register =>
  103. findService(() => new RegisterService(currentHostAddress));
  104. RemedicalService get remedical =>
  105. findService(() => new RemedicalService(currentHostAddress));
  106. RemoteUltrasoundService get remoteUltrasound =>
  107. findService(() => new RemoteUltrasoundService(currentHostAddress));
  108. ReportService get report =>
  109. findService(() => new ReportService(currentHostAddress));
  110. RoleService get role =>
  111. findService(() => new RoleService(currentHostAddress));
  112. SMSService get sMS =>
  113. findService(() => new SMSService(currentHostAddress));
  114. StorageService get storage =>
  115. findService(() => new StorageService(currentHostAddress));
  116. UltrasoundReportService get ultrasoundReport =>
  117. findService(() => new UltrasoundReportService(currentHostAddress));
  118. UpgradeService get upgrade =>
  119. findService(() => new UpgradeService(currentHostAddress));
  120. UserService get user =>
  121. findService(() => new UserService(currentHostAddress));
  122. VinnoIOTService get vinnoIOT =>
  123. findService(() => new VinnoIOTService(currentHostAddress));
  124. VinnoServerService get vinnoServer =>
  125. findService(() => new VinnoServerService(currentHostAddress));
  126. VitalHealthExamBookingService get vitalHealthExamBooking =>
  127. findService(() => new VitalHealthExamBookingService(currentHostAddress));
  128. VitalAnalyzeConfigService get vitalAnalyzeConfig =>
  129. findService(() => new VitalAnalyzeConfigService(currentHostAddress));
  130. VitalCompletionRecordService get vitalCompletionRecord =>
  131. findService(() => new VitalCompletionRecordService(currentHostAddress));
  132. VitalContractRecordService get vitalContractRecord =>
  133. findService(() => new VitalContractRecordService(currentHostAddress));
  134. VitalContractTemplateService get vitalContractTemplate =>
  135. findService(() => new VitalContractTemplateService(currentHostAddress));
  136. VitalDeviceService get vitalDevice =>
  137. findService(() => new VitalDeviceService(currentHostAddress));
  138. VitalDiagnosisService get vitalDiagnosis =>
  139. findService(() => new VitalDiagnosisService(currentHostAddress));
  140. VitalDictionaryService get vitalDictionary =>
  141. findService(() => new VitalDictionaryService(currentHostAddress));
  142. VitalDynamicTypeService get vitalDynamicType =>
  143. findService(() => new VitalDynamicTypeService(currentHostAddress));
  144. VitalExamService get vitalExam =>
  145. findService(() => new VitalExamService(currentHostAddress));
  146. VitalFollowUpService get vitalFollowUp =>
  147. findService(() => new VitalFollowUpService(currentHostAddress));
  148. VitalLabelService get vitalLabel =>
  149. findService(() => new VitalLabelService(currentHostAddress));
  150. VitalLoginService get vitalLogin =>
  151. findService(() => new VitalLoginService(currentHostAddress));
  152. VitalOperationLogService get vitalOperationLog =>
  153. findService(() => new VitalOperationLogService(currentHostAddress));
  154. VitalOrganizationService get vitalOrganization =>
  155. findService(() => new VitalOrganizationService(currentHostAddress));
  156. VitalPatientExtensionService get vitalPatientExtension =>
  157. findService(() => new VitalPatientExtensionService(currentHostAddress));
  158. VitalPatientService get vitalPatient =>
  159. findService(() => new VitalPatientService(currentHostAddress));
  160. VitalRegionService get vitalRegion =>
  161. findService(() => new VitalRegionService(currentHostAddress));
  162. VitalReportService get vitalReport =>
  163. findService(() => new VitalReportService(currentHostAddress));
  164. VitalResidenceService get vitalResidence =>
  165. findService(() => new VitalResidenceService(currentHostAddress));
  166. VitalRoleService get vitalRole =>
  167. findService(() => new VitalRoleService(currentHostAddress));
  168. VitalScheduleService get vitalSchedule =>
  169. findService(() => new VitalScheduleService(currentHostAddress));
  170. VitalServicePackService get vitalServicePack =>
  171. findService(() => new VitalServicePackService(currentHostAddress));
  172. VitalStatisticService get vitalStatistic =>
  173. findService(() => new VitalStatisticService(currentHostAddress));
  174. VitalSystemSettingService get vitalSystemSetting =>
  175. findService(() => new VitalSystemSettingService(currentHostAddress));
  176. VitalTeamRegionService get vitalTeamRegion =>
  177. findService(() => new VitalTeamRegionService(currentHostAddress));
  178. VitalTeamService get vitalTeam =>
  179. findService(() => new VitalTeamService(currentHostAddress));
  180. VitalTemplateService get vitalTemplate =>
  181. findService(() => new VitalTemplateService(currentHostAddress));
  182. VitalTownService get vitalTown =>
  183. findService(() => new VitalTownService(currentHostAddress));
  184. VitalUpgradeService get vitalUpgrade =>
  185. findService(() => new VitalUpgradeService(currentHostAddress));
  186. VitalUserFeatureService get vitalUserFeature =>
  187. findService(() => new VitalUserFeatureService(currentHostAddress));
  188. VitalUserPasswordService get vitalUserPassword =>
  189. findService(() => new VitalUserPasswordService(currentHostAddress));
  190. VitalUserService get vitalUser =>
  191. findService(() => new VitalUserService(currentHostAddress));
  192. /* 服务代理设置 End */
  193. /// 设置服务主机地址
  194. void setServerHost(String address, [bool useSSL = false]) {
  195. logger.i('JsonRpcProxy setServerHost :' + address);
  196. _currentProtocol = useSSL ? "https" : "http";
  197. _currentHost = address;
  198. }
  199. /// 添加拦截器
  200. void addInterceptor(JsonRpcInterceptor interceptor) =>
  201. jsonRpcInterceptHost.addInterceptor(interceptor);
  202. /// 清空缓存
  203. void clearCache() => _serviceCache.clear();
  204. /// 查找Service实例
  205. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  206. Type serviceType = typeOf<T>();
  207. if (!_serviceCache.containsKey(serviceType)) {
  208. _serviceCache[serviceType] = builder.call();
  209. }
  210. return _serviceCache[serviceType] as T;
  211. }
  212. }