增加上传违规商品

This commit is contained in:
chfei committed 2026-10-30 11:30:26 +08:00
1 parent d51dbf2bb7
commit a319af0e91
3 files changed
+347 -156

No files matched your search

+186
View File
@@ -2,6 +2,7 @@
using AllBase.Helper;
using AllBase.Model.Result;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -46,6 +47,155 @@ namespace Work.Services
return ret.Data;
}
/// <summary>
/// 在后台解析微信处罚 JSON 并上报;无商品或处理失败时返回空列表,异常在内部记录。
/// </summary>
public static void ReportViolationsFromJson(string json)
{
try
{
Task.Run(() => ReportViolationsFromJsonCore(json)).ConfigureAwait(false);
}
catch (Exception ex)
{
try
{
LogSave.AddFile("解析并上报违规商品失败:" + ex.Message);
}
catch
{
// 日志失败也不能影响调用方。
}
}
}
private static async Task<List<ReportGoodsViolationResult>> ReportViolationsFromJsonCore(string json)
{
if (string.IsNullOrWhiteSpace(json))
{
throw new ArgumentException("处罚 JSON 文本不能为空。", nameof(json));
}
JObject root;
try
{
root = JObject.Parse(json);
}
catch (JsonException ex)
{
throw new ArgumentException("处罚 JSON 格式不正确。", nameof(json), ex);
}
var record = root["record"] as JObject;
if (root["code"]?.ToString() != "0" || record?["code"]?.ToString() != "0")
{
throw new ArgumentException("处罚数据的 code 或 record.code 不为 0,或缺少 record。", nameof(json));
}
if (!(record["items"] is JArray records))
{
throw new ArgumentException("处罚数据缺少 record.items 数组。", nameof(json));
}
var items = new List<ReportGoodsViolationItem>();
foreach (var token in records)
{
if (!(token is JObject punishment))
{
throw new ArgumentException("record.items 中的处罚记录必须是对象。", nameof(json));
}
var objects = punishment["punishObject"];
if (objects == null || objects.Type == JTokenType.Null)
{
continue;
}
if (!(objects is JArray products))
{
throw new ArgumentException("punishObject 必须是数组。", nameof(json));
}
foreach (var product in products)
{
if (!(product is JObject target))
{
throw new ArgumentException("punishObject 中的处罚对象必须是对象。", nameof(json));
}
var id = target["productId"];
// 订单等非商品处罚对象不包含 productId。
if (id == null)
{
continue;
}
if ((id.Type != JTokenType.String && id.Type != JTokenType.Integer)
|| !long.TryParse(id.ToString(), out var goodsId) || goodsId <= 0)
{
throw new ArgumentException("商品 productId 必须是有效的正整数编号。", nameof(json));
}
var reason = punishment["punishReason"];
if (reason?.Type != JTokenType.String || string.IsNullOrWhiteSpace((string)reason))
{
throw new ArgumentException($"商品 {goodsId} 缺少有效的 punishReason。", nameof(json));
}
items.Add(new ReportGoodsViolationItem { GoodsId = goodsId, Reasons = ((string)reason).Trim() });
}
}
var reports = items.GroupBy(item => item.GoodsId).Select(group => new ReportGoodsViolationItem
{
GoodsId = group.Key,
Reasons = string.Join(";", group.Select(item => item.Reasons).Distinct())
}).ToList();
return reports.Count == 0 ? new List<ReportGoodsViolationResult>() : await ReportViolations(reports);
}
/// <summary>
/// 批量上报违规商品。
/// </summary>
public static async Task<List<ReportGoodsViolationResult>> ReportViolations(List<ReportGoodsViolationItem> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (items.Any(item => item == null))
{
throw new ArgumentException("违规商品列表不能包含空项。", nameof(items));
}
var html = await GetHtml.PostJsonAsync(ServerUrl + "/api/CloudGoods/ReportViolations", items);
var ret = JsonConvert.DeserializeObject<Result<List<ReportGoodsViolationResult>>>(html);
if (ret == null)
{
throw new Exception("批量上报违规商品返回为空。");
}
if (ret.Code != 200)
{
throw new Exception(ret.Msg);
}
return ret.Data ?? throw new Exception("批量上报违规商品返回数据为空。");
}
/// <summary>
/// 批量查询商品违规状态。
/// </summary>
public static async Task<List<GoodsViolationStatus>> GetViolationStatus(List<long> goodsIds)
{
if (goodsIds == null)
{
throw new ArgumentNullException(nameof(goodsIds));
}
var html = await GetHtml.PostJsonAsync(ServerUrl + "/api/CloudGoods/GetViolationStatus", goodsIds);
var ret = JsonConvert.DeserializeObject<Result<List<GoodsViolationStatus>>>(html);
if (ret == null)
{
throw new Exception("批量查询商品违规状态返回为空。");
}
if (ret.Code != 200)
{
throw new Exception(ret.Msg);
}
return ret.Data ?? throw new Exception("批量查询商品违规状态返回数据为空。");
}
#region user
/// <summary>
/// 搜索收藏
@@ -117,6 +267,42 @@ namespace Work.Services
return string.IsNullOrEmpty(BaseClass.ParentUserName) ? BaseClass.UserName : BaseClass.ParentUserName;
}
}
/// <summary>
/// 违规商品上报项。
/// </summary>
public class ReportGoodsViolationItem
{
[JsonProperty("goodsId")]
public long GoodsId { get; set; }
[JsonProperty("reasons")]
public string Reasons { get; set; } = string.Empty;
}
/// <summary>
/// 单个商品的违规上报结果。
/// </summary>
public class ReportGoodsViolationResult
{
[JsonProperty("goodsId")]
public long GoodsId { get; set; }
[JsonProperty("isOffShelf")]
public bool IsOffShelf { get; set; }
}
/// <summary>
/// 商品违规状态。
/// </summary>
public class GoodsViolationStatus
{
[JsonProperty("goodsId")]
public long GoodsId { get; set; }
[JsonProperty("isViolation")]
public bool IsViolation { get; set; }
}
/// <summary>
/// 商品搜索请求
/// </summary>
+11 -9
View File
@@ -12,15 +12,9 @@ using System.Threading.Tasks;
namespace Work.Services;
public class ImageCache
{
public string Md5 { get; set; }
public int Platform { get; set; }
[JsonProperty("platform_url")]
public string PlatformUrl { get; set; }
public bool IsCache { get; set; }
}
/// <summary>
/// 图片缓存服务
/// </summary>
public class ImageCacheService
{
private static string ServerUrl = "http://103.90.155.78:9001";
@@ -136,3 +130,11 @@ public class ImageCacheService
}
}
public class ImageCache
{
public string Md5 { get; set; }
public int Platform { get; set; }
[JsonProperty("platform_url")]
public string PlatformUrl { get; set; }
public bool IsCache { get; set; }
}