139 lines
3.3 KiB
C#
139 lines
3.3 KiB
C#
using AllBase;
|
|
using AllBase.Helper;
|
|
using AllBase.Model.Result;
|
|
using AllBase.Model.Result.PDDResult;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
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; }
|
|
}
|
|
|
|
public class ImageCacheService
|
|
{
|
|
private static string ServerUrl = "http://103.90.155.78:9001";
|
|
static ImageCacheService()
|
|
{
|
|
if (EnvironmentHelper.IsDevelopment)
|
|
{
|
|
ServerUrl = "http://localhost:14312";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <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
|
|
{
|
|
//if (EnvironmentHelper.IsProduction)
|
|
//{
|
|
// return wxImgMapDic;
|
|
//}
|
|
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 url = ServerUrl + "/api/ImageCache/GetImageCache";//获取缓存
|
|
var html = GetHtml.PostJson(url, new { md5s = imageUrlMd5s });
|
|
var ret = JsonConvert.DeserializeObject<Result<List<ImageCache>>>(html);
|
|
if (ret.Code == 200)
|
|
{
|
|
foreach (var item in ret.Data)
|
|
{
|
|
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
|
|
{
|
|
//if (EnvironmentHelper.IsProduction)
|
|
//{
|
|
// return;
|
|
//}
|
|
var data = "";
|
|
data = JsonHelper.SerializeObject(images.Where(a => a.IsCache == false).Select(a =>
|
|
new
|
|
{
|
|
md5 = a.Md5,
|
|
Platform = platform,
|
|
PlatformUrl = a.PlatformUrl
|
|
}
|
|
));
|
|
|
|
var url = ServerUrl + "/api/ImageCache/AddOrUpdate";
|
|
var html = GetHtml.PostJson(url, data);
|
|
|
|
|
|
var ret = JsonConvert.DeserializeObject<Result<JToken>>(html);
|
|
List<string> failed = new List<string>();
|
|
if (ret.Code == 500)
|
|
{
|
|
if (ret.Data is JArray jArray)
|
|
{
|
|
failed = jArray.ToObject<List<string>>() ?? new List<string>();
|
|
}
|
|
else if (ret.Data is JValue jValue && jValue.Value is string str)
|
|
{
|
|
failed.Add(str);
|
|
}
|
|
}
|
|
foreach (ImageCache item in images)
|
|
{
|
|
item.IsCache = !failed.Contains(item.Md5, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|