Upload2ZCManager.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using ToneTuneToolkit.Common;
  6. using UnityEngine.Events;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. /// <summary>
  10. /// 对志城法宝
  11. /// </summary>
  12. public class Upload2ZCManager : SingletonMaster<Upload2ZCManager>
  13. {
  14. private const string uploadURL = @"https://vw-aud.studiocapsule.cn/api/device/uploadWall";
  15. public UnityAction<string> OnUploadFinished;
  16. // ==================================================
  17. #region 上传文件流
  18. public void UploadData(byte[] fileBytes) => StartCoroutine(nameof(UploadDataAction), fileBytes);
  19. private IEnumerator UploadDataAction(byte[] fileBytes)
  20. {
  21. WWWForm wwwForm = new WWWForm();
  22. wwwForm.AddBinaryData("file", fileBytes);
  23. using (UnityWebRequest www = UnityWebRequest.Post(uploadURL, wwwForm))
  24. {
  25. // www.SetRequestHeader("Content-Type", "multipart/form-data"); // wwwForm不要手动设置避免boundary消失
  26. www.downloadHandler = new DownloadHandlerBuffer();
  27. yield return www.SendWebRequest();
  28. if (www.result != UnityWebRequest.Result.Success)
  29. {
  30. Debug.Log($"[U2ZCM] {www.error}");
  31. yield break;
  32. }
  33. Debug.Log($"[U2ZCM] {www.downloadHandler.text}");
  34. ResponData responData = JsonConvert.DeserializeObject<ResponData>(www.downloadHandler.text);
  35. // // 解析方案A 动态类型
  36. // dynamic data = responData.data;
  37. // string qr = data.qr_url;
  38. // 解析方案B
  39. JObject data = JObject.FromObject(responData.data);
  40. string qr_url = data["qr_url"].ToString();
  41. // Debug.Log(qr_url);
  42. DownloadQRCode(qr_url);
  43. }
  44. yield break;
  45. }
  46. #endregion
  47. // ==================================================
  48. #region 获取QR图片
  49. public static UnityAction<Texture2D> OnDownloadQRCodeFinished;
  50. [SerializeField] private Texture2D debug_peekQRCode;
  51. public void DownloadQRCode(string url) => StartCoroutine(nameof(DownloadQRCodeAction), url);
  52. private IEnumerator DownloadQRCodeAction(string url)
  53. {
  54. using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))
  55. {
  56. yield return www.SendWebRequest();
  57. if (www.result != UnityWebRequest.Result.Success)
  58. {
  59. Debug.Log($"[U2ZCM] {www.error}");
  60. yield break;
  61. }
  62. debug_peekQRCode = DownloadHandlerTexture.GetContent(www); // DEBUG
  63. if (OnDownloadQRCodeFinished != null)
  64. {
  65. OnDownloadQRCodeFinished(DownloadHandlerTexture.GetContent(www));
  66. }
  67. }
  68. yield break;
  69. }
  70. #endregion
  71. // ==================================================
  72. public class ResponData
  73. {
  74. public int code;
  75. public string message;
  76. public object data;
  77. }
  78. }