MatchingPhotoHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 DownloadExt(List<CurRanMatchingPhotoWorksData> list)
  36. {
  37. for (int i = 0; i < list.Count; i++)
  38. {
  39. CurRanMatchingPhotoWorksData data = list[i];
  40. if (data == null || data.Ntexture != null || !data.IsUp) continue;
  41. int count = 0;
  42. yield return DownloadPictureExt(data, count);
  43. }
  44. MatchingCompetitionDataManager.Instance.IsWork = false;
  45. ET.Log.Debug("Download finish!!!");
  46. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  47. }
  48. public static IEnumerator Download(List<MatchingPhotoWorksData> list)
  49. {
  50. for (int i = 0; i < list.Count; i++)
  51. {
  52. MatchingPhotoWorksData data = list[i];
  53. if (data == null || data.Ntexture != null || !data.IsUp) continue;
  54. int count = 0;
  55. yield return DownloadPicture(data, count);
  56. }
  57. ET.Log.Debug("Download finish!!!");
  58. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  59. }
  60. public static IEnumerator Download(List<MatchingWorksData> list)
  61. {
  62. for (int i = 0; i < list.Count; i++)
  63. {
  64. MatchingWorksData data = list[i];
  65. if (data == null || data.Ntexture != null) continue;
  66. int count = 0;
  67. yield return DownloadPicture(data, count);
  68. }
  69. ET.Log.Debug("Download finish!!!");
  70. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  71. }
  72. public static IEnumerator Download(MatchingPhotoWorksData list)
  73. {
  74. MatchingPhotoWorksData data = list;
  75. yield return DownloadPicture(data, 1);
  76. ET.Log.Debug("Download finish!!!");
  77. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  78. }
  79. private static IEnumerator DownloadPictureExt(CurRanMatchingPhotoWorksData data, int count)
  80. {
  81. if (count >= 3)
  82. {
  83. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  84. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  85. data.Ntexture = null;
  86. ViewManager.Hide<ModalStatusView>();
  87. yield break;
  88. }
  89. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
  90. {
  91. yield return request.SendWebRequest();
  92. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  93. {
  94. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  95. count += 1;
  96. yield return DownloadPictureExt(data, count);
  97. }
  98. else
  99. {
  100. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  101. data.Ntexture = new NTexture(texture);
  102. data.Bytes = texture.EncodeToJPG();
  103. }
  104. }
  105. }
  106. private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count)
  107. {
  108. if (count >= 3)
  109. {
  110. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  111. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  112. data.Ntexture = null;
  113. ViewManager.Hide<ModalStatusView>();
  114. yield break;
  115. }
  116. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
  117. {
  118. yield return request.SendWebRequest();
  119. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  120. {
  121. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  122. count += 1;
  123. yield return DownloadPicture(data, count);
  124. }
  125. else
  126. {
  127. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  128. data.Ntexture = new NTexture(texture);
  129. data.Bytes = texture.EncodeToJPG();
  130. }
  131. }
  132. }
  133. private static IEnumerator DownloadPicture(MatchingWorksData data, int count)
  134. {
  135. if (count >= 3)
  136. {
  137. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  138. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  139. data.Ntexture = null;
  140. ViewManager.Hide<ModalStatusView>();
  141. yield break;
  142. }
  143. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl))
  144. {
  145. yield return request.SendWebRequest();
  146. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  147. {
  148. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  149. count += 1;
  150. yield return DownloadPicture(data, count);
  151. }
  152. else
  153. {
  154. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  155. data.Ntexture = new NTexture(texture);
  156. data.Bytes = texture.EncodeToJPG();
  157. }
  158. }
  159. }
  160. public static IEnumerator DownloadMyself(string nTextture)
  161. {
  162. yield return DownloadMyPicture(nTextture, 1);
  163. ET.Log.Debug("Download finish!!!");
  164. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  165. }
  166. private static IEnumerator DownloadMyPicture(string nTextture, int count)
  167. {
  168. if (count >= 3)
  169. {
  170. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  171. ViewManager.Hide<ModalStatusView>();
  172. yield break;
  173. }
  174. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(nTextture))
  175. {
  176. yield return request.SendWebRequest();
  177. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  178. {
  179. count += 1;
  180. yield return DownloadMyPicture(nTextture, count);
  181. }
  182. else
  183. {
  184. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  185. MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture);
  186. MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG();
  187. }
  188. }
  189. }
  190. }
  191. }