using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Threading.Tasks; using ET; using FairyGUI; using UnityEngine; using UnityEngine.Networking; namespace GFGGame { public static class MatchingPhotoHelper { //将图片上传到华为云 public static async Task PushToHWCloud(string signUrl, byte[] buffer) { HttpContent content = new ByteArrayContent(buffer); HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseCookies = false }); HttpResponseMessage response = await httpClient.PutAsync(signUrl, content); response.EnsureSuccessStatusCode(); await response.Content.ReadAsStreamAsync(); if (response.StatusCode != HttpStatusCode.OK) { ViewManager.Hide(); PromptController.Instance.ShowFloatTextPrompt("上传资源失败"); return false; } else { return true; } } public static IEnumerator Download(List list,string targetPath) { for (int i = 0; i < list.Count; i++) { List imageList = MatchingCompetitionDataManager.Instance.GetImageFileNames(targetPath); string ImageName = list[i].JudgingInfo.WorksId.ToString() + ".jpg"; //排行榜内图片是否存在本地,否就下载,是就赋值 if (!MatchingCompetitionDataManager.Instance.RankIdisInLocal(ImageName, imageList)) { MatchingPhotoWorksData data = list[i]; if (data == null || data.Ntexture != null) continue; int count = 0; yield return DownloadPicture(data, count, targetPath); } else { list[i].Bytes = File.ReadAllBytes((targetPath+ "/" + ImageName)); list[i].Ntexture = MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName,targetPath); yield return null; } //本地图片是否存在排行榜,否就删除 foreach (string item in imageList) { string getImagePath = targetPath +"/"+ list[i].JudgingInfo.WorksId.ToString()+".jpg"; if (!MatchingCompetitionDataManager.Instance.ImageIdIsInRank(item)) { MatchingCompetitionDataManager.Instance.DeleteImage(getImagePath); } } } ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } public static IEnumerator Download(List list) { for (int i = 0; i < list.Count; i++) { MatchingWorksData data = list[i]; if (data == null || data.Ntexture != null) continue; int count = 0; yield return DownloadPicture(data, count); } ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } public static IEnumerator Download(MatchingPhotoWorksData list, string targetPath) { MatchingPhotoWorksData data = list; yield return DownloadPicture(data, 1,targetPath); ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count,string targetPath) { if (count >= 3) { PromptController.Instance.ShowFloatTextPrompt("下载失败"); ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data)); data.Ntexture = null; ViewManager.Hide(); yield break; } using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data)); count += 1; yield return DownloadPicture(data, count, targetPath); } else { Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture; data.Ntexture = new NTexture(texture); data.Bytes = texture.EncodeToJPG(); MatchingCompetitionDataManager.Instance.DownloadImageToLocal(data.Bytes,data.JudgingInfo.WorksId,targetPath); } } } private static IEnumerator DownloadPicture(MatchingWorksData data, int count) { if (count >= 3) { PromptController.Instance.ShowFloatTextPrompt("下载失败"); ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data)); data.Ntexture = null; ViewManager.Hide(); yield break; } using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data)); count += 1; yield return DownloadPicture(data, count); } else { Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture; data.Ntexture = new NTexture(texture); data.Bytes = texture.EncodeToJPG(); } } } public static IEnumerator DownloadMyself(string nTextture,string targetPath) { yield return DownloadMyPicture(nTextture, 1,targetPath); ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } private static IEnumerator DownloadMyPicture(string nTextture, int count,string targetPath) { if (count >= 3) { PromptController.Instance.ShowFloatTextPrompt("下载失败"); ViewManager.Hide(); yield break; } using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(nTextture)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { count += 1; yield return DownloadMyPicture(nTextture, count,targetPath); } else { Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture; MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture); MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG(); //MatchingCompetitionDataManager.Instance.DownloadImageToLocal(MatchingCompetitionDataManager.Instance.MyBytes, MatchingCompetitionDataManager.Instance.WorksID,targetPath); } } } } }