MatchingPhotoHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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)
  36. {
  37. for (int i = 0; i < list.Count; i++)
  38. {
  39. MatchingPhotoWorksData data = list[i];
  40. if (data == null || data.Ntexture != null) continue;
  41. int count = 0;
  42. yield return DownloadPicture(data, count);
  43. }
  44. ET.Log.Debug("Download finish!!!");
  45. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  46. }
  47. public static IEnumerator Download(List<MatchingWorksData> list)
  48. {
  49. for (int i = 0; i < list.Count; i++)
  50. {
  51. MatchingWorksData data = list[i];
  52. if (data == null || data.Ntexture != null) continue;
  53. int count = 0;
  54. yield return DownloadPicture(data, count);
  55. }
  56. ET.Log.Debug("Download finish!!!");
  57. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  58. }
  59. public static IEnumerator Download(MatchingPhotoWorksData list)
  60. {
  61. MatchingPhotoWorksData data = list;
  62. yield return DownloadPicture(data, 1);
  63. ET.Log.Debug("Download finish!!!");
  64. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  65. }
  66. private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count)
  67. {
  68. if (count >= 3)
  69. {
  70. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  71. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  72. data.Ntexture = null;
  73. ViewManager.Hide<ModalStatusView>();
  74. yield break;
  75. }
  76. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
  77. {
  78. yield return request.SendWebRequest();
  79. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  80. {
  81. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  82. count += 1;
  83. yield return DownloadPicture(data, count);
  84. }
  85. else
  86. {
  87. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  88. data.Ntexture = new NTexture(texture);
  89. data.Bytes = texture.EncodeToJPG();
  90. }
  91. }
  92. }
  93. private static IEnumerator DownloadPicture(MatchingWorksData data, int count)
  94. {
  95. if (count >= 3)
  96. {
  97. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  98. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  99. data.Ntexture = null;
  100. ViewManager.Hide<ModalStatusView>();
  101. yield break;
  102. }
  103. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl))
  104. {
  105. yield return request.SendWebRequest();
  106. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  107. {
  108. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  109. count += 1;
  110. yield return DownloadPicture(data, count);
  111. }
  112. else
  113. {
  114. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  115. data.Ntexture = new NTexture(texture);
  116. data.Bytes = texture.EncodeToJPG();
  117. }
  118. }
  119. }
  120. }
  121. }