89 lines
2.3 KiB
C#
89 lines
2.3 KiB
C#
using AllBase;
|
|
using AllBase.Model.Result.PDDResult;
|
|
using AllBase.Model.Result;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Work.Store.Api;
|
|
|
|
namespace Work.Services;
|
|
|
|
/// <summary>
|
|
/// 图片缓存服务
|
|
/// </summary>
|
|
public class ImageCacheService
|
|
{
|
|
/// <summary>
|
|
/// 获取图片缓存
|
|
/// </summary>
|
|
/// <param name="product"></param>
|
|
public static Dictionary<string, ImageCache> GetImageCaches(PDDProductInfo product)
|
|
{
|
|
Dictionary<string, ImageCache> wxImgMapDic = new Dictionary<string, ImageCache>(StringComparer.OrdinalIgnoreCase);
|
|
try
|
|
{
|
|
List<string> imageUrls = new List<string>();
|
|
imageUrls.AddRange(product.detail.galleries.Where(g => g.type == 1).Take(9).Select(a => a.url));
|
|
var temp_detailGalleries = product.detail.galleries.Where(g => g.type == 2).ToList();
|
|
if (temp_detailGalleries.Count == 0)
|
|
temp_detailGalleries.Add(new gallery_item() { type = 2, url = product.detail.galleries.FirstOrDefault(g => g.type == 1)?.url });
|
|
imageUrls.AddRange(temp_detailGalleries.Select(a => a.url));
|
|
|
|
|
|
if (product.detail.sku.FirstOrDefault()?.spec?.Count > 0)
|
|
{
|
|
imageUrls.AddRange(product.detail.sku.Select(a => a.thumb_url));
|
|
}
|
|
|
|
|
|
|
|
List<string> imageUrlMd5s = imageUrls.Select(a => BaseClass.GetMd5(a)).ToList();
|
|
|
|
var caches = StoreApiClient.Instance.ImageCache.GetImageCaches(imageUrlMd5s);
|
|
foreach (var item in caches)
|
|
{
|
|
item.IsCache = true;
|
|
wxImgMapDic[item.Md5] = item;
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
|
|
}
|
|
return wxImgMapDic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存缓存图片
|
|
/// </summary>
|
|
/// <param name="platform"></param>
|
|
/// <param name="images"></param>
|
|
public static void SaveImageCaches(PlatformType platform, IEnumerable<ImageCache> images)
|
|
{
|
|
try
|
|
{
|
|
var pending = images.Where(image => !image.IsCache).ToList();
|
|
var failed = StoreApiClient.Instance.ImageCache.SaveImageCaches((int)platform, pending);
|
|
foreach (var item in pending)
|
|
{
|
|
item.IsCache = !failed.Contains(item.Md5, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
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; }
|
|
}
|