using Aliyun.Acs.Core; using Aliyun.Acs.Core.Http; using Aliyun.Acs.Core.Profile; using Elasticsearch.Net; using JiaZhiQuan.Common.Config; using JiaZhiQuan.Common.ElasticSearch; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Senparc.Weixin.MP.Containers; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Dynamic; using System.Linq; using System.Net.Http; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Wicture.DbRESTFul; using Wicture.DbRESTFul.Cache; using Wicture.DbRESTFul.Infrastructure.Repository; namespace JiaZhiQuan.Common { public class TextScanResult { public string RequestId { get; set; } /// /// 英文标签 /// public string Label { get; set; } /// /// 标签中文名称 /// public string LabelCN { get; set; } /// /// 原Suggestion内容,pass/block/preview /// public string Suggestion { get; set; } public string ResponseText { get; set; } /// /// -1 出错了 0 通过 1 阻止 2 人工复核 /// public int State { get; set; } public static Dictionary TextCheckLabelDic = new Dictionary() { ["abuse"] = "辱骂", ["terrorism"] = "暴恐", ["porn"] = "鉴黄", ["contraband"] = "违禁", ["ad"] = "广告", ["politics"] = "敏感", }; } public static partial class RepositoryExtension { /// /// 检查文本内容合法性 /// public static async Task CheckText(this DbRESTFulRepository repository, string text, ConfigFromDb config) { TextScanResult lastResp = null; var left = text.Length; var rst = string.Empty; if (string.IsNullOrWhiteSpace(text)) { lastResp = new TextScanResult { RequestId = string.Empty, Label = "normal", LabelCN = "正常", State = 0, Suggestion = string.Empty, ResponseText = string.Empty, }; } while (left > 0) { var subText = text.Substring(text.Length - left, left > 10000 ? 10000 : left); IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", config.AliVisionAccessKeyId, config.AliVisionAccessSecret); DefaultAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.Method = MethodType.POST; request.Domain = "imageaudit.cn-shanghai.aliyuncs.com"; request.Version = "2019-12-30"; request.Action = "ScanText"; request.AddQueryParameters("Tasks.1.Content", subText); var idx = 1; TextScanResult.TextCheckLabelDic.Keys.ForEach(e => { request.AddQueryParameters($"Labels.{idx}.Label", e); idx++; }); CommonResponse response = client.GetCommonResponse(request); var respTxt = Encoding.Default.GetString(response.HttpResponse.Content); JToken token = JToken.Parse(respTxt); var reqId = token.Value("RequestId") ?? string.Empty; var checkRst = token.Value("Data")?.Value("Elements")?.FirstOrDefault()?.Value("Results")?.FirstOrDefault(); var label = checkRst?.Value("Label"); var suggestion = checkRst?.Value("Suggestion"); var labelCN = label == null ? "" : TextScanResult.TextCheckLabelDic.ContainsKey(label) ? TextScanResult.TextCheckLabelDic[label] : "正常"; var state = suggestion == "block" ? 1 : suggestion == "review" ? 2 : suggestion == "pass" ? 0 : -1; lastResp = new TextScanResult { RequestId = reqId, Label = label, LabelCN = labelCN, State = state, Suggestion = suggestion ?? string.Empty, ResponseText = respTxt }; if (lastResp.State == -1 || lastResp.State == 1) break; left -= 10000; } if (!string.IsNullOrEmpty(lastResp?.ResponseText)) { // 保存最后一次的结果到数据库 await repository.QuerySingleOrDefaultAsync("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); } return await Task.FromResult(lastResp); } } }