AiQualityService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using RestSharp;
  2. using Fw.Utility.UnifyResponse;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using Hotline.Share.Dtos.Quality;
  6. using Hotline.Ai.Quality;
  7. namespace Hotline.Ai.XingTang
  8. {
  9. public class AiQualityService : IAiQualityService
  10. {
  11. private readonly RestClient _client;
  12. private readonly string _baseUrl;
  13. public AiQualityService(string baseUrl)
  14. {
  15. _client = new RestClient();
  16. _baseUrl = baseUrl;
  17. }
  18. public async Task CreateAiOrderQualityTask(
  19. string id,
  20. string audioFile,
  21. string fromNo,
  22. DateTime? callStartTime,
  23. string viteRecordPrefix,
  24. string ywlxString,
  25. CancellationToken cancellationToken)
  26. {
  27. }
  28. public async Task<string> CreateAiOrderQualityTask(string filename, CancellationToken cancellationToken)
  29. {
  30. var url = _baseUrl + "/offlinerecog?filename=" + filename;
  31. var baseUrl = new Uri(url);
  32. return await ExecuteAsync(baseUrl.ToString(), Method.Get, "", cancellationToken);
  33. }
  34. public async Task<ApiResponse<TResponse>> ExecuteAsync<TRequest, TResponse>(string path, Method httpMethod,
  35. TRequest request, CancellationToken cancellationToken)
  36. where TRequest : class
  37. {
  38. var req = new RestRequest(path, httpMethod);
  39. if (httpMethod is Method.Get)
  40. {
  41. req.AddObject(request);
  42. }
  43. else
  44. {
  45. req.AddJsonBody(request);
  46. }
  47. try
  48. {
  49. var response = await _client.ExecuteAsync<ApiResponse<TResponse>>(req, cancellationToken);
  50. return response.Data;
  51. }
  52. catch (Exception e)
  53. {
  54. throw new HttpRequestException($"智能质检平台错误,Error: {e.Message}");
  55. }
  56. }
  57. public async Task<string> ExecuteAsync<TRequest>(string path, Method httpMethod, TRequest request,
  58. CancellationToken cancellationToken)
  59. where TRequest : class
  60. {
  61. var req = new RestRequest(path, httpMethod);
  62. req.Timeout = new TimeSpan(0,30,0);
  63. try
  64. {
  65. var response = await _client.ExecuteAsync<ApiResponse>(req, cancellationToken);
  66. return response.Content;
  67. }
  68. catch (Exception e)
  69. {
  70. throw new HttpRequestException($"智能质检平台错误,Error: {e.Message}");
  71. }
  72. }
  73. /// <summary>
  74. /// MD5加密
  75. /// </summary>
  76. /// <param name="input">需要加密的字符串</param>
  77. /// <returns></returns>
  78. private static string MD5Encrypt(string? input)
  79. {
  80. using var md5 = MD5.Create();
  81. var t = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
  82. var sb = new StringBuilder(32);
  83. for (var i = 0; i < t.Length; i++)
  84. sb.Append(t[i].ToString("x").PadLeft(2, '0'));
  85. return sb.ToString();
  86. }
  87. private static string Base64En(string? model)
  88. {
  89. var bytes = Encoding.UTF8.GetBytes(model);
  90. return Convert.ToBase64String(bytes);
  91. }
  92. }
  93. }