using CsharpHttpHelper; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using Work.Services; namespace Work.Store.Api; /// 图片缓存接口,返回批量保存失败的 MD5 列表。 public sealed class ImageCacheApi { private readonly ApiTransport _transport; public ImageCacheApi(ApiTransport transport) => _transport = transport ?? throw new ArgumentNullException(nameof(transport)); public List GetImageCaches(List md5s) => _transport.GetRequired>("ImageCache/GetImageCache", MethodType.Post, new { md5s }); public List SaveImageCaches(int platform, IReadOnlyList images) { if (images == null) throw new ArgumentNullException(nameof(images)); if (images.Count == 0) return new List(); try { _transport.Send("ImageCache/AddOrUpdate", MethodType.Post, images.Select(image => new { md5 = image.Md5, Platform = platform, PlatformUrl = image.PlatformUrl }).ToArray()); return new List(); } catch (ApiException ex) when (ex.Code == 500) { // 兼容旧服务的部分失败响应;没有明确失败项时保持整体失败。 if (ex.ResponseData is JArray array && array.Count > 0 && array.All(item => item.Type == JTokenType.String)) return array.ToObject>(); if (ex.ResponseData?.Type == JTokenType.String && !string.IsNullOrWhiteSpace((string)ex.ResponseData)) return new List { (string)ex.ResponseData }; throw; } } }