intimacy_analyze_repository.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/response/intimacy_analyze_chat_config_response.dart';
  12. import '../api/response/intimacy_analyze_config_response.dart';
  13. import '../api/response/intimacy_analyze_reply_config_response.dart';
  14. import '../api/response/intimacy_analyze_response.dart';
  15. /// 亲密度分析Repository层
  16. @LazySingleton()
  17. class IntimacyAnalyzeRepository {
  18. final String tag = "IntimacyAnalyzeRepository";
  19. final AtmobApi atmobApi;
  20. /// 亲密度配置
  21. Rxn<IntimacyAnalyzeConfigResponse> intimacyAnalyzeConfig =
  22. Rxn<IntimacyAnalyzeConfigResponse>();
  23. /// 对话分析配置
  24. Rxn<IntimacyAnalyzeChatConfigResponse> intimacyAnalyzeChatConfig =
  25. Rxn<IntimacyAnalyzeChatConfigResponse>();
  26. /// 识图回复配置
  27. Rxn<IntimacyAnalyzeReplyConfigResponse> intimacyAnalyzeReplyConfig =
  28. Rxn<IntimacyAnalyzeReplyConfigResponse>();
  29. IntimacyAnalyzeRepository(this.atmobApi) {
  30. AtmobLog.d(tag, '$tag...init');
  31. // 初始化时,刷新配置
  32. _refreshIntimacyAnalyzeConfig();
  33. _refreshIntimacyAnalyzeChatConfig();
  34. _refreshIntimacyAnalyzeReplyConfig();
  35. }
  36. /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用
  37. IntimacyAnalyzeRepository getInstance() {
  38. return getIt.get<IntimacyAnalyzeRepository>();
  39. }
  40. /// 刷新亲密度配置
  41. void _refreshIntimacyAnalyzeConfig() {
  42. AsyncUtil.retry(
  43. () => requestIntimacyAnalyzeConfig(),
  44. Duration(seconds: 3),
  45. maxRetry: 100,
  46. ).then((IntimacyAnalyzeConfigResponse configResponse) {
  47. AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}");
  48. intimacyAnalyzeConfig.value = configResponse;
  49. });
  50. }
  51. /// 刷新对话分析配置
  52. void _refreshIntimacyAnalyzeChatConfig() {
  53. AsyncUtil.retry(
  54. () => requestIntimacyAnalyzeChatConfig(),
  55. Duration(seconds: 3),
  56. maxRetry: 100,
  57. ).then((IntimacyAnalyzeChatConfigResponse configResponse) {
  58. AtmobLog.d(tag, "获取对话分析配置成功: ${configResponse.toJson()}");
  59. intimacyAnalyzeChatConfig.value = configResponse;
  60. });
  61. }
  62. /// 刷新识图回复配置
  63. void _refreshIntimacyAnalyzeReplyConfig() {
  64. AsyncUtil.retry(
  65. () => requestIntimacyAnalyzeReplyConfig(),
  66. Duration(seconds: 3),
  67. maxRetry: 100,
  68. ).then((IntimacyAnalyzeReplyConfigResponse configResponse) {
  69. AtmobLog.d(tag, "获取识图回复配置成功: ${configResponse.toJson()}");
  70. intimacyAnalyzeReplyConfig.value = configResponse;
  71. });
  72. }
  73. /// 获取亲密度配置
  74. Future<IntimacyAnalyzeConfigResponse> requestIntimacyAnalyzeConfig() {
  75. return atmobApi
  76. .getIntimacyAnalyzeConfig(AppBaseRequest())
  77. .then(HttpHandler.handle(true));
  78. }
  79. /// 获取对话分析配置
  80. Future<IntimacyAnalyzeChatConfigResponse> requestIntimacyAnalyzeChatConfig() {
  81. return atmobApi
  82. .getIntimacyAnalyzeChatConfig(AppBaseRequest())
  83. .then(HttpHandler.handle(true));
  84. }
  85. /// 获取识图回复配置
  86. Future<IntimacyAnalyzeReplyConfigResponse>
  87. requestIntimacyAnalyzeReplyConfig() {
  88. return atmobApi
  89. .getIntimacyAnalyzeReplyConfig(AppBaseRequest())
  90. .then(HttpHandler.handle(true));
  91. }
  92. /// 分析亲密度
  93. Future<IntimacyAnalyzeResponse> getIntimacyAnalyze(
  94. IntimacyAnalyzeRequest request,
  95. ) {
  96. return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true));
  97. }
  98. }