MatchingPhotoHelper.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using ET;
  9. using FairyGUI;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. namespace GFGGame
  13. {
  14. public static class MatchingPhotoHelper
  15. {
  16. //将图片上传到华为云
  17. public static async Task<bool> PushToHWCloud(string signUrl, byte[] buffer)
  18. {
  19. HttpContent content = new ByteArrayContent(buffer);
  20. HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseCookies = false });
  21. HttpResponseMessage response = await httpClient.PutAsync(signUrl, content);
  22. response.EnsureSuccessStatusCode();
  23. await response.Content.ReadAsStreamAsync();
  24. if (response.StatusCode != HttpStatusCode.OK)
  25. {
  26. ViewManager.Hide<ModalStatusView>();
  27. PromptController.Instance.ShowFloatTextPrompt("上传资源失败");
  28. return false;
  29. }
  30. else
  31. {
  32. return true;
  33. }
  34. }
  35. public static IEnumerator Download(List<MatchingPhotoWorksData> list,string targetPath)
  36. {
  37. List<string> imageList = MatchingCompetitionDataManager.Instance.GetImageFileNames(targetPath);
  38. for (int i = 0; i < list.Count; i++)
  39. {
  40. string ImageName = list[i].JudgingInfo.WorksId.ToString() + ".jpg";
  41. //排行榜内图片是否存在本地,否就下载,是就赋值
  42. if (!MatchingCompetitionDataManager.Instance.RankIdisInLocal(ImageName, imageList))
  43. {
  44. MatchingPhotoWorksData data = list[i];
  45. if (data == null || data.Ntexture != null) continue;
  46. int count = 0;
  47. yield return DownloadPicture(data, count, targetPath);
  48. }
  49. else
  50. {
  51. //这个变量没用处
  52. //list[i].Bytes = File.ReadAllBytes((targetPath+ "/" + ImageName));
  53. if(!MatchingCompetitionDataManager.Instance._currentImageDic.ContainsKey(list[i].JudgingInfo.WorksId.ToString()))
  54. {
  55. MatchingCompetitionDataManager.Instance._currentImageDic.Add(list[i].JudgingInfo.WorksId.ToString(), MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName, targetPath));
  56. }
  57. //list[i].Ntexture = MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName,targetPath);
  58. yield return null;
  59. }
  60. //本地图片是否存在排行榜,否就删除
  61. foreach (string item in imageList)
  62. {
  63. string getImagePath = targetPath +"/"+ list[i].JudgingInfo.WorksId.ToString()+".jpg";
  64. if (!MatchingCompetitionDataManager.Instance.ImageIdIsInRank(item))
  65. {
  66. MatchingCompetitionDataManager.Instance.DeleteImage(getImagePath);
  67. }
  68. }
  69. }
  70. ET.Log.Debug("Download finish!!!");
  71. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  72. }
  73. public static IEnumerator Download(List<MatchingWorksData> list)
  74. {
  75. for (int i = 0; i < list.Count; i++)
  76. {
  77. MatchingWorksData data = list[i];
  78. if (data == null || data.Ntexture != null) continue;
  79. int count = 0;
  80. yield return DownloadPicture(data, count);
  81. }
  82. ET.Log.Debug("Download finish!!!");
  83. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  84. }
  85. public static IEnumerator Download(MatchingPhotoWorksData list, string targetPath)
  86. {
  87. MatchingPhotoWorksData data = list;
  88. yield return DownloadPicture(data, 1,targetPath);
  89. ET.Log.Debug("Download finish!!!");
  90. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  91. }
  92. private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count,string targetPath)
  93. {
  94. if (count >= 3)
  95. {
  96. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  97. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  98. data.Ntexture = null;
  99. ViewManager.Hide<ModalStatusView>();
  100. yield break;
  101. }
  102. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
  103. {
  104. yield return request.SendWebRequest();
  105. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  106. {
  107. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  108. count += 1;
  109. yield return DownloadPicture(data, count, targetPath);
  110. }
  111. else
  112. {
  113. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  114. data.Ntexture = new NTexture(texture);
  115. data.Bytes = texture.EncodeToJPG();
  116. if (!MatchingCompetitionDataManager.Instance._currentImageDic.ContainsKey(data.JudgingInfo.WorksId.ToString()))
  117. {
  118. MatchingCompetitionDataManager.Instance._currentImageDic.Add(data.JudgingInfo.WorksId.ToString(), data.Ntexture);
  119. }
  120. MatchingCompetitionDataManager.Instance.DownloadImageToLocal(data.Bytes,data.JudgingInfo.WorksId,targetPath);
  121. }
  122. }
  123. }
  124. private static IEnumerator DownloadPicture(MatchingWorksData data, int count)
  125. {
  126. if (count >= 3)
  127. {
  128. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  129. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  130. data.Ntexture = null;
  131. ViewManager.Hide<ModalStatusView>();
  132. yield break;
  133. }
  134. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl))
  135. {
  136. yield return request.SendWebRequest();
  137. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  138. {
  139. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  140. count += 1;
  141. yield return DownloadPicture(data, count);
  142. }
  143. else
  144. {
  145. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  146. data.Ntexture = new NTexture(texture);
  147. data.Bytes = texture.EncodeToJPG();
  148. }
  149. }
  150. }
  151. public static IEnumerator DownloadMyself(string nTextture,string targetPath)
  152. {
  153. yield return DownloadMyPicture(nTextture, 1,targetPath);
  154. ET.Log.Debug("Download finish!!!");
  155. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  156. }
  157. private static IEnumerator DownloadMyPicture(string nTextture, int count,string targetPath)
  158. {
  159. if (count >= 3)
  160. {
  161. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  162. ViewManager.Hide<ModalStatusView>();
  163. yield break;
  164. }
  165. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(nTextture))
  166. {
  167. yield return request.SendWebRequest();
  168. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  169. {
  170. count += 1;
  171. yield return DownloadMyPicture(nTextture, count,targetPath);
  172. }
  173. else
  174. {
  175. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  176. MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture);
  177. MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG();
  178. //MatchingCompetitionDataManager.Instance.DownloadImageToLocal(MatchingCompetitionDataManager.Instance.MyBytes, MatchingCompetitionDataManager.Instance.WorksID,targetPath);
  179. }
  180. }
  181. }
  182. }
  183. }