RepositoryExtension.CheckContent.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Aliyun.Acs.Core;
  2. using Aliyun.Acs.Core.Http;
  3. using Aliyun.Acs.Core.Profile;
  4. using Elasticsearch.Net;
  5. using JiaZhiQuan.Common.Config;
  6. using JiaZhiQuan.Common.ElasticSearch;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using Senparc.Weixin.MP.Containers;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Diagnostics;
  14. using System.Dynamic;
  15. using System.Linq;
  16. using System.Net.Http;
  17. using System.Reflection;
  18. using System.Security.Cryptography;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using Wicture.DbRESTFul;
  22. using Wicture.DbRESTFul.Cache;
  23. using Wicture.DbRESTFul.Infrastructure.Repository;
  24. namespace JiaZhiQuan.Common
  25. {
  26. public class TextScanResult
  27. {
  28. public string RequestId { get; set; }
  29. /// <summary>
  30. /// 英文标签
  31. /// </summary>
  32. public string Label { get; set; }
  33. /// <summary>
  34. /// 标签中文名称
  35. /// </summary>
  36. public string LabelCN { get; set; }
  37. /// <summary>
  38. /// 原Suggestion内容,pass/block/preview
  39. /// </summary>
  40. public string Suggestion { get; set; }
  41. public string ResponseText { get; set; }
  42. /// <summary>
  43. /// -1 出错了 0 通过 1 阻止 2 人工复核
  44. /// </summary>
  45. public int State { get; set; }
  46. public static Dictionary<string, string> TextCheckLabelDic = new Dictionary<string, string>()
  47. {
  48. ["abuse"] = "辱骂",
  49. ["terrorism"] = "暴恐",
  50. ["porn"] = "鉴黄",
  51. ["contraband"] = "违禁",
  52. ["ad"] = "广告",
  53. ["politics"] = "敏感",
  54. };
  55. }
  56. public static partial class RepositoryExtension
  57. {
  58. /// <summary>
  59. /// 检查文本内容合法性
  60. /// </summary>
  61. public static async Task<TextScanResult> CheckText(this DbRESTFulRepository repository, string text, ConfigFromDb config)
  62. {
  63. TextScanResult lastResp = null;
  64. var left = text.Length;
  65. var rst = string.Empty;
  66. if (string.IsNullOrWhiteSpace(text))
  67. {
  68. lastResp = new TextScanResult
  69. {
  70. RequestId = string.Empty,
  71. Label = "normal",
  72. LabelCN = "正常",
  73. State = 0,
  74. Suggestion = string.Empty,
  75. ResponseText = string.Empty,
  76. };
  77. }
  78. while (left > 0)
  79. {
  80. var subText = text.Substring(text.Length - left, left > 10000 ? 10000 : left);
  81. IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", config.AliVisionAccessKeyId, config.AliVisionAccessSecret);
  82. DefaultAcsClient client = new DefaultAcsClient(profile);
  83. CommonRequest request = new CommonRequest();
  84. request.Method = MethodType.POST;
  85. request.Domain = "imageaudit.cn-shanghai.aliyuncs.com";
  86. request.Version = "2019-12-30";
  87. request.Action = "ScanText";
  88. request.AddQueryParameters("Tasks.1.Content", subText);
  89. var idx = 1;
  90. TextScanResult.TextCheckLabelDic.Keys.ForEach(e =>
  91. {
  92. request.AddQueryParameters($"Labels.{idx}.Label", e);
  93. idx++;
  94. });
  95. CommonResponse response = client.GetCommonResponse(request);
  96. var respTxt = Encoding.Default.GetString(response.HttpResponse.Content);
  97. JToken token = JToken.Parse(respTxt);
  98. var reqId = token.Value<string>("RequestId") ?? string.Empty;
  99. var checkRst = token.Value<JObject>("Data")?.Value<JArray>("Elements")?.FirstOrDefault()?.Value<JArray>("Results")?.FirstOrDefault();
  100. var label = checkRst?.Value<string>("Label");
  101. var suggestion = checkRst?.Value<string>("Suggestion");
  102. var labelCN = label == null ? "" : TextScanResult.TextCheckLabelDic.ContainsKey(label) ? TextScanResult.TextCheckLabelDic[label] : "正常";
  103. var state = suggestion == "block" ? 1 : suggestion == "review" ? 2 : suggestion == "pass" ? 0 : -1;
  104. lastResp = new TextScanResult
  105. {
  106. RequestId = reqId,
  107. Label = label,
  108. LabelCN = labelCN,
  109. State = state,
  110. Suggestion = suggestion ?? string.Empty,
  111. ResponseText = respTxt
  112. };
  113. if (lastResp.State == -1 || lastResp.State == 1) break;
  114. left -= 10000;
  115. }
  116. if (!string.IsNullOrEmpty(lastResp?.ResponseText))
  117. {
  118. // 保存最后一次的结果到数据库
  119. await repository.QuerySingleOrDefaultAsync<dynamic>("insert ignore into s_yundun_text_records(id, resp, createAt) values(@id, @resp, @createAt)", new { id = lastResp.RequestId, resp = lastResp.ResponseText, createAt = DateTime.Now }, null, null, false);
  120. }
  121. return await Task.FromResult(lastResp);
  122. }
  123. }
  124. }