MatchingPhotoHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. for (int i = 0; i < list.Count; i++)
  38. {
  39. List<string> imageList = MatchingCompetitionDataManager.Instance.GetImageFileNames(targetPath);
  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. list[i].Bytes = File.ReadAllBytes((targetPath+ "/" + ImageName));
  52. list[i].Ntexture = MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName,targetPath);
  53. yield return null;
  54. }
  55. //本地图片是否存在排行榜,否就删除
  56. foreach (string item in imageList)
  57. {
  58. string getImagePath = targetPath +"/"+ list[i].JudgingInfo.WorksId.ToString()+".jpg";
  59. if (!MatchingCompetitionDataManager.Instance.ImageIdIsInRank(item))
  60. {
  61. MatchingCompetitionDataManager.Instance.DeleteImage(getImagePath);
  62. }
  63. }
  64. }
  65. ET.Log.Debug("Download finish!!!");
  66. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  67. }
  68. public static IEnumerator Download(List<MatchingWorksData> list)
  69. {
  70. for (int i = 0; i < list.Count; i++)
  71. {
  72. MatchingWorksData data = list[i];
  73. if (data == null || data.Ntexture != null) continue;
  74. int count = 0;
  75. yield return DownloadPicture(data, count);
  76. }
  77. ET.Log.Debug("Download finish!!!");
  78. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  79. }
  80. public static IEnumerator Download(MatchingPhotoWorksData list, string targetPath)
  81. {
  82. MatchingPhotoWorksData data = list;
  83. yield return DownloadPicture(data, 1,targetPath);
  84. ET.Log.Debug("Download finish!!!");
  85. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  86. }
  87. private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count,string targetPath)
  88. {
  89. if (count >= 3)
  90. {
  91. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  92. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  93. data.Ntexture = null;
  94. ViewManager.Hide<ModalStatusView>();
  95. yield break;
  96. }
  97. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
  98. {
  99. yield return request.SendWebRequest();
  100. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  101. {
  102. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  103. count += 1;
  104. yield return DownloadPicture(data, count, targetPath);
  105. }
  106. else
  107. {
  108. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  109. data.Ntexture = new NTexture(texture);
  110. data.Bytes = texture.EncodeToJPG();
  111. MatchingCompetitionDataManager.Instance.DownloadImageToLocal(data.Bytes,data.JudgingInfo.WorksId,targetPath);
  112. }
  113. }
  114. }
  115. private static IEnumerator DownloadPicture(MatchingWorksData data, int count)
  116. {
  117. if (count >= 3)
  118. {
  119. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  120. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  121. data.Ntexture = null;
  122. ViewManager.Hide<ModalStatusView>();
  123. yield break;
  124. }
  125. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl))
  126. {
  127. yield return request.SendWebRequest();
  128. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  129. {
  130. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  131. count += 1;
  132. yield return DownloadPicture(data, count);
  133. }
  134. else
  135. {
  136. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  137. data.Ntexture = new NTexture(texture);
  138. data.Bytes = texture.EncodeToJPG();
  139. }
  140. }
  141. }
  142. public static IEnumerator DownloadMyself(string nTextture,string targetPath)
  143. {
  144. yield return DownloadMyPicture(nTextture, 1,targetPath);
  145. ET.Log.Debug("Download finish!!!");
  146. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  147. }
  148. private static IEnumerator DownloadMyPicture(string nTextture, int count,string targetPath)
  149. {
  150. if (count >= 3)
  151. {
  152. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  153. ViewManager.Hide<ModalStatusView>();
  154. yield break;
  155. }
  156. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(nTextture))
  157. {
  158. yield return request.SendWebRequest();
  159. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  160. {
  161. count += 1;
  162. yield return DownloadMyPicture(nTextture, count,targetPath);
  163. }
  164. else
  165. {
  166. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  167. MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture);
  168. MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG();
  169. //MatchingCompetitionDataManager.Instance.DownloadImageToLocal(MatchingCompetitionDataManager.Instance.MyBytes, MatchingCompetitionDataManager.Instance.WorksID,targetPath);
  170. }
  171. }
  172. }
  173. }
  174. }