diff --git a/Work/Services/GoodsComplianceService.cs b/Work/Services/GoodsComplianceService.cs index 09dad1f..8f0b14b 100644 --- a/Work/Services/GoodsComplianceService.cs +++ b/Work/Services/GoodsComplianceService.cs @@ -37,7 +37,8 @@ public sealed class ComplianceResult public string Error { get; internal set; } public string Status => Error != null ? "失败,可重试" : Hits.Length == 0 ? "安全" : "有风险"; public string Summary => Error != null ? "失败:" + Error : - Hits.Length == 0 ? Status : $"有风险:第 {HitImageNumber} 张图片命中关键词:{string.Join(",", Hits)}"; + Hits.Length == 0 ? Status : $"有风险:{HitSource}命中关键词:{string.Join(",", Hits)}"; + private string HitSource => HitImageNumber == 0 ? "标题" : $"第 {HitImageNumber} 张图片"; } public sealed class ComplianceBatchResult @@ -217,6 +218,12 @@ public static class GoodsComplianceService { cancellationToken.ThrowIfCancellationRequested(); if (string.IsNullOrWhiteSpace(product?.ProductId)) throw new InvalidDataException("缺少商品 ID。"); + result.Hits = Matches(product.Title ?? string.Empty, words); + if (result.Hits.Length > 0) + { + result.Text = product.Title; + return result; + } var urls = SelectImages(product.MainImages, options.MainCount) .Concat(SelectImages(product.DetailImages, options.DetailCount)); if (options.IncludeSku) urls = urls.Concat(SelectImages(product.Skus?.Select(s => s?.ImageUrl), null)); diff --git a/test/GoodsComplianceSmoke.cs b/test/GoodsComplianceSmoke.cs index f837b38..c36ef47 100644 --- a/test/GoodsComplianceSmoke.cs +++ b/test/GoodsComplianceSmoke.cs @@ -50,6 +50,15 @@ internal static class GoodsComplianceSmoke Check(words.Length == 5); Check(GoodsComplianceService.Matches("ABC广告", words).SequenceEqual(new[] { "广告", "abc" })); Check(GoodsComplianceService.Matches("普通商品", words).Length == 0); + // Exercise detection without calling the reporting API. + var detect = typeof(GoodsComplianceService).GetMethod("DetectProductAsync", + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); + var titleHit = ((System.Threading.Tasks.Task)detect.Invoke(null, new object[] + { + new Product { ProductId = "title", Title = "ABC广告" }, new ComplianceOptions(), words, null, + System.Threading.CancellationToken.None + })).GetAwaiter().GetResult(); + Check(titleHit.Status == "有风险" && titleHit.Summary == "有风险:标题命中关键词:广告,abc"); var invalidImage = new Product { ProductId = "bad-image", MainImages = new List { "file:///image.jpg" } }; var statuses = new List(); GoodsComplianceService.DetectSelectedAsync(new[] { cloud }, item => invalidImage,