rpc.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. ResearchEditionService get researchEdition =>
  59. findService(() => new ResearchEditionService(currentHostAddress));
  60. LiveConsultationService get liveConsultation =>
  61. findService(() => new LiveConsultationService(currentHostAddress));
  62. AIDiagnosisService get aIDiagnosis =>
  63. findService(() => new AIDiagnosisService(currentHostAddress));
  64. AppletAPIService get appletAPI =>
  65. findService(() => new AppletAPIService(currentHostAddress));
  66. ASRService get aSR =>
  67. findService(() => new ASRService(currentHostAddress));
  68. AuthenticationService get authentication =>
  69. findService(() => new AuthenticationService(currentHostAddress));
  70. ChatMessageService get chatMessage =>
  71. findService(() => new ChatMessageService(currentHostAddress));
  72. ConnectService get connect =>
  73. findService(() => new ConnectService(currentHostAddress));
  74. DeployPlatformService get deployPlatform =>
  75. findService(() => new DeployPlatformService(currentHostAddress));
  76. DeviceService get device =>
  77. findService(() => new DeviceService(currentHostAddress));
  78. DongleService get dongle =>
  79. findService(() => new DongleService(currentHostAddress));
  80. EducationService get education =>
  81. findService(() => new EducationService(currentHostAddress));
  82. EmailService get email =>
  83. findService(() => new EmailService(currentHostAddress));
  84. IdentityApplyService get identityApply =>
  85. findService(() => new IdentityApplyService(currentHostAddress));
  86. LabService get lab =>
  87. findService(() => new LabService(currentHostAddress));
  88. LoginService get login =>
  89. findService(() => new LoginService(currentHostAddress));
  90. OrganizationService get organization =>
  91. findService(() => new OrganizationService(currentHostAddress));
  92. PatientService get patient =>
  93. findService(() => new PatientService(currentHostAddress));
  94. PaymentService get payment =>
  95. findService(() => new PaymentService(currentHostAddress));
  96. PositionService get position =>
  97. findService(() => new PositionService(currentHostAddress));
  98. RankService get rank =>
  99. findService(() => new RankService(currentHostAddress));
  100. RecognitionService get recognition =>
  101. findService(() => new RecognitionService(currentHostAddress));
  102. RecordInfoService get recordInfo =>
  103. findService(() => new RecordInfoService(currentHostAddress));
  104. RegionService get region =>
  105. findService(() => new RegionService(currentHostAddress));
  106. RegisterService get register =>
  107. findService(() => new RegisterService(currentHostAddress));
  108. RemedicalService get remedical =>
  109. findService(() => new RemedicalService(currentHostAddress));
  110. RemoteUltrasoundService get remoteUltrasound =>
  111. findService(() => new RemoteUltrasoundService(currentHostAddress));
  112. ReportService get report =>
  113. findService(() => new ReportService(currentHostAddress));
  114. RoleService get role =>
  115. findService(() => new RoleService(currentHostAddress));
  116. SMSService get sMS =>
  117. findService(() => new SMSService(currentHostAddress));
  118. StorageService get storage =>
  119. findService(() => new StorageService(currentHostAddress));
  120. TaskService get task =>
  121. findService(() => new TaskService(currentHostAddress));
  122. UltrasoundReportService get ultrasoundReport =>
  123. findService(() => new UltrasoundReportService(currentHostAddress));
  124. UpgradeService get upgrade =>
  125. findService(() => new UpgradeService(currentHostAddress));
  126. UserService get user =>
  127. findService(() => new UserService(currentHostAddress));
  128. VinnoIOTService get vinnoIOT =>
  129. findService(() => new VinnoIOTService(currentHostAddress));
  130. VinnoServerService get vinnoServer =>
  131. findService(() => new VinnoServerService(currentHostAddress));
  132. VitalHealthExamBookingService get vitalHealthExamBooking =>
  133. findService(() => new VitalHealthExamBookingService(currentHostAddress));
  134. ExamService get exam =>
  135. findService(() => new ExamService(currentHostAddress));
  136. VitalAnalyzeConfigService get vitalAnalyzeConfig =>
  137. findService(() => new VitalAnalyzeConfigService(currentHostAddress));
  138. VitalCompletionRecordService get vitalCompletionRecord =>
  139. findService(() => new VitalCompletionRecordService(currentHostAddress));
  140. VitalContractRecordService get vitalContractRecord =>
  141. findService(() => new VitalContractRecordService(currentHostAddress));
  142. VitalContractTemplateService get vitalContractTemplate =>
  143. findService(() => new VitalContractTemplateService(currentHostAddress));
  144. VitalDeviceService get vitalDevice =>
  145. findService(() => new VitalDeviceService(currentHostAddress));
  146. VitalDiagnosisService get vitalDiagnosis =>
  147. findService(() => new VitalDiagnosisService(currentHostAddress));
  148. VitalDictionaryService get vitalDictionary =>
  149. findService(() => new VitalDictionaryService(currentHostAddress));
  150. VitalDynamicTypeService get vitalDynamicType =>
  151. findService(() => new VitalDynamicTypeService(currentHostAddress));
  152. VitalElectrocardiogramService get vitalElectrocardiogram =>
  153. findService(() => new VitalElectrocardiogramService(currentHostAddress));
  154. VitalExamService get vitalExam =>
  155. findService(() => new VitalExamService(currentHostAddress));
  156. VitalFacturyPostHistoryService get vitalFacturyPostHistory =>
  157. findService(() => new VitalFacturyPostHistoryService(currentHostAddress));
  158. VitalFacturyUserService get vitalFacturyUser =>
  159. findService(() => new VitalFacturyUserService(currentHostAddress));
  160. VitalFollowUpService get vitalFollowUp =>
  161. findService(() => new VitalFollowUpService(currentHostAddress));
  162. VitalLabelService get vitalLabel =>
  163. findService(() => new VitalLabelService(currentHostAddress));
  164. VitalLoginService get vitalLogin =>
  165. findService(() => new VitalLoginService(currentHostAddress));
  166. VitalOperationLogService get vitalOperationLog =>
  167. findService(() => new VitalOperationLogService(currentHostAddress));
  168. VitalOrganizationService get vitalOrganization =>
  169. findService(() => new VitalOrganizationService(currentHostAddress));
  170. VitalPatientExtensionService get vitalPatientExtension =>
  171. findService(() => new VitalPatientExtensionService(currentHostAddress));
  172. VitalPatientService get vitalPatient =>
  173. findService(() => new VitalPatientService(currentHostAddress));
  174. VitalPrescriptionService get vitalPrescription =>
  175. findService(() => new VitalPrescriptionService(currentHostAddress));
  176. VitalRegionService get vitalRegion =>
  177. findService(() => new VitalRegionService(currentHostAddress));
  178. VitalReportService get vitalReport =>
  179. findService(() => new VitalReportService(currentHostAddress));
  180. VitalResidenceService get vitalResidence =>
  181. findService(() => new VitalResidenceService(currentHostAddress));
  182. VitalRoleService get vitalRole =>
  183. findService(() => new VitalRoleService(currentHostAddress));
  184. VitalScheduleService get vitalSchedule =>
  185. findService(() => new VitalScheduleService(currentHostAddress));
  186. VitalServicePackService get vitalServicePack =>
  187. findService(() => new VitalServicePackService(currentHostAddress));
  188. VitalStatisticService get vitalStatistic =>
  189. findService(() => new VitalStatisticService(currentHostAddress));
  190. VitalSystemSettingService get vitalSystemSetting =>
  191. findService(() => new VitalSystemSettingService(currentHostAddress));
  192. VitalTeamRegionService get vitalTeamRegion =>
  193. findService(() => new VitalTeamRegionService(currentHostAddress));
  194. VitalTeamService get vitalTeam =>
  195. findService(() => new VitalTeamService(currentHostAddress));
  196. VitalTemplateService get vitalTemplate =>
  197. findService(() => new VitalTemplateService(currentHostAddress));
  198. VitalTownService get vitalTown =>
  199. findService(() => new VitalTownService(currentHostAddress));
  200. VitalUpgradeService get vitalUpgrade =>
  201. findService(() => new VitalUpgradeService(currentHostAddress));
  202. VitalUserFeatureService get vitalUserFeature =>
  203. findService(() => new VitalUserFeatureService(currentHostAddress));
  204. VitalUserPasswordService get vitalUserPassword =>
  205. findService(() => new VitalUserPasswordService(currentHostAddress));
  206. VitalUserService get vitalUser =>
  207. findService(() => new VitalUserService(currentHostAddress));
  208. /* 服务代理设置 End */
  209. /// 设置服务主机地址
  210. void setServerHost(String address, [bool useSSL = false]) {
  211. logger.i('JsonRpcProxy setServerHost :' + address);
  212. _currentProtocol = useSSL ? "https" : "http";
  213. _currentHost = address;
  214. }
  215. /// 添加拦截器
  216. void addInterceptor(JsonRpcInterceptor interceptor) =>
  217. jsonRpcInterceptHost.addInterceptor(interceptor);
  218. /// 清空缓存
  219. void clearCache() => _serviceCache.clear();
  220. /// 查找Service实例
  221. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  222. Type serviceType = typeOf<T>();
  223. if (!_serviceCache.containsKey(serviceType)) {
  224. _serviceCache[serviceType] = builder.call();
  225. }
  226. return _serviceCache[serviceType] as T;
  227. }
  228. }