intimacy_analyze_repository.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'dart:io';
  2. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/app_base_request.dart';
  5. import '../../di/get_it.dart';
  6. import '../../utils/async_util.dart';
  7. import '../../utils/atmob_log.dart';
  8. import '../../utils/http_handler.dart';
  9. import '../api/atmob_api.dart';
  10. import '../api/request/intimacy_analyze_request.dart';
  11. import '../api/request/intimacy_reply_analyze_request.dart';
  12. import '../api/request/intimacy_reply_chat_request.dart';
  13. import '../api/response/intimacy_analyze_chat_config_response.dart';
  14. import '../api/response/intimacy_analyze_config_response.dart';
  15. import '../api/response/intimacy_analyze_reply_config_response.dart';
  16. import '../api/response/intimacy_analyze_response.dart';
  17. import '../api/response/intimacy_chat_analyze_response.dart';
  18. import '../api/response/intimacy_reply_analyze_response.dart';
  19. /// 亲密度分析Repository层
  20. @LazySingleton()
  21. class IntimacyAnalyzeRepository {
  22. final String tag = "IntimacyAnalyzeRepository";
  23. final AtmobApi atmobApi;
  24. /// 亲密度配置
  25. Rxn<IntimacyAnalyzeConfigResponse> intimacyAnalyzeConfig =
  26. Rxn<IntimacyAnalyzeConfigResponse>();
  27. /// 对话分析配置
  28. Rxn<IntimacyAnalyzeChatConfigResponse> intimacyAnalyzeChatConfig =
  29. Rxn<IntimacyAnalyzeChatConfigResponse>();
  30. /// 识图回复配置
  31. Rxn<IntimacyAnalyzeReplyConfigResponse> intimacyAnalyzeReplyConfig =
  32. Rxn<IntimacyAnalyzeReplyConfigResponse>();
  33. IntimacyAnalyzeRepository(this.atmobApi) {
  34. AtmobLog.d(tag, '$tag...init');
  35. // 初始化时,刷新配置
  36. _refreshIntimacyAnalyzeConfig();
  37. _refreshIntimacyAnalyzeChatConfig();
  38. _refreshIntimacyAnalyzeReplyConfig();
  39. }
  40. /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用
  41. IntimacyAnalyzeRepository getInstance() {
  42. return getIt.get<IntimacyAnalyzeRepository>();
  43. }
  44. /// 刷新亲密度配置
  45. void _refreshIntimacyAnalyzeConfig() {
  46. AsyncUtil.retry(
  47. () => requestIntimacyAnalyzeConfig(),
  48. Duration(seconds: 3),
  49. maxRetry: 100,
  50. ).then((IntimacyAnalyzeConfigResponse configResponse) {
  51. AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}");
  52. intimacyAnalyzeConfig.value = configResponse;
  53. });
  54. }
  55. /// 刷新对话分析配置
  56. void _refreshIntimacyAnalyzeChatConfig() {
  57. AsyncUtil.retry(
  58. () => requestIntimacyAnalyzeChatConfig(),
  59. Duration(seconds: 3),
  60. maxRetry: 100,
  61. ).then((IntimacyAnalyzeChatConfigResponse configResponse) {
  62. AtmobLog.d(tag, "获取对话分析配置成功: ${configResponse.toJson()}");
  63. intimacyAnalyzeChatConfig.value = configResponse;
  64. });
  65. }
  66. /// 刷新识图回复配置
  67. void _refreshIntimacyAnalyzeReplyConfig() {
  68. AsyncUtil.retry(
  69. () => requestIntimacyAnalyzeReplyConfig(),
  70. Duration(seconds: 3),
  71. maxRetry: 100,
  72. ).then((IntimacyAnalyzeReplyConfigResponse configResponse) {
  73. AtmobLog.d(tag, "获取识图回复配置成功: ${configResponse.toJson()}");
  74. intimacyAnalyzeReplyConfig.value = configResponse;
  75. });
  76. }
  77. /// 获取亲密度配置
  78. Future<IntimacyAnalyzeConfigResponse> requestIntimacyAnalyzeConfig() {
  79. return atmobApi
  80. .getIntimacyAnalyzeConfig(AppBaseRequest())
  81. .then(HttpHandler.handle(true));
  82. }
  83. /// 获取对话分析配置
  84. Future<IntimacyAnalyzeChatConfigResponse> requestIntimacyAnalyzeChatConfig() {
  85. return atmobApi
  86. .getIntimacyAnalyzeChatConfig(AppBaseRequest())
  87. .then(HttpHandler.handle(true));
  88. }
  89. /// 对话分析
  90. Future<IntimacyChatAnalyzeResponse> intimacyChatAnalyze(
  91. IntimacyChatAnalyzeRequest request,
  92. ) {
  93. return atmobApi.intimacyChatAnalyze(request).then(HttpHandler.handle(true));
  94. }
  95. /// 获取识图回复配置
  96. Future<IntimacyAnalyzeReplyConfigResponse>
  97. requestIntimacyAnalyzeReplyConfig() {
  98. return atmobApi
  99. .getIntimacyAnalyzeReplyConfig(AppBaseRequest())
  100. .then(HttpHandler.handle(true));
  101. }
  102. /// 分析亲密度
  103. Future<IntimacyAnalyzeResponse> getIntimacyAnalyze(
  104. IntimacyAnalyzeRequest request,
  105. ) {
  106. return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true));
  107. }
  108. /// 识图回复
  109. Future<IntimacyReplyAnalyzeResponse> intimacyReplyAnalyze(
  110. IntimacyReplyAnalyzeRequest request,
  111. ) {
  112. return atmobApi
  113. .intimacyReplyAnalyze(request)
  114. .then(HttpHandler.handle(true));
  115. }
  116. }