Upload2ZCManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. using System;
  10. /// <summary>
  11. /// 对志城综合法宝
  12. /// </summary>
  13. public class Upload2ZCManager : SingletonMaster<Upload2ZCManager>
  14. {
  15. // ==================================================
  16. #region 2025.04 Nike ADT
  17. private const string USERINFOREQUESTURL = @"https://nike-adt.studiocapsule.cn/api/device/scanQr";
  18. public static UnityAction<UserInfo> OnUserInfoDownloaded;
  19. /// <summary>
  20. /// 获取用户信息
  21. /// </summary>
  22. /// <param name="url"></param>
  23. public void GetUserInfo(string url) => StartCoroutine(nameof(GetUserInfoAction), url);
  24. private IEnumerator GetUserInfoAction(string url)
  25. {
  26. WWWForm wwwForm = new WWWForm();
  27. wwwForm.AddField("qr_content", url);
  28. using (UnityWebRequest www = UnityWebRequest.Post(USERINFOREQUESTURL, wwwForm))
  29. {
  30. www.downloadHandler = new DownloadHandlerBuffer();
  31. yield return www.SendWebRequest();
  32. if (www.result != UnityWebRequest.Result.Success)
  33. {
  34. Debug.Log($"[U2ZCM] {www.error}");
  35. yield break;
  36. }
  37. Debug.Log($"[U2ZCM] {www.downloadHandler.text}");
  38. try
  39. {
  40. uird = JsonConvert.DeserializeObject<Respon_UserInfoData>(www.downloadHandler.text);
  41. }
  42. catch (Exception)
  43. {
  44. Debug.Log($"[U2ZCM] 解析错误");
  45. yield break;
  46. }
  47. if (uird.code != 0)
  48. {
  49. if (OnUserInfoDownloaded != null)
  50. {
  51. OnUserInfoDownloaded(null);
  52. }
  53. yield break;
  54. }
  55. if (OnUserInfoDownloaded != null)
  56. {
  57. UserInfo ui = new UserInfo();
  58. ui.name = uird.data.name;
  59. ui.code = uird.data.code;
  60. ui.save_car = uird.data.save_car;
  61. ui.can_play = uird.data.can_play;
  62. OnUserInfoDownloaded(ui);
  63. }
  64. }
  65. yield break;
  66. }
  67. [SerializeField] private Respon_UserInfoData uird = new Respon_UserInfoData();
  68. [Serializable]
  69. public class Respon_UserInfoData
  70. {
  71. public int code;
  72. public string message;
  73. public UserInfo data;
  74. }
  75. [Serializable]
  76. public class UserInfo
  77. {
  78. public string name;
  79. public string code;
  80. public string save_car;
  81. public string can_play;
  82. }
  83. // ==================================================
  84. private const string USERIMAGEUPLOADURL = @"https://nike-adt.studiocapsule.cn/api/device/upload";
  85. public static UnityAction<string> OnUserImageUploaded;
  86. [SerializeField] private Upload_UserImageData uUID;
  87. public void UpdateUserImageData(string user_code, Texture2D t2dPhoto)
  88. {
  89. uUID = new Upload_UserImageData();
  90. uUID.user_code = user_code;
  91. byte[] fileBytes = t2dPhoto.EncodeToPNG();
  92. uUID.file = fileBytes;
  93. return;
  94. }
  95. public void UploadUserImage() => StartCoroutine(nameof(UploadUserImageAction));
  96. private IEnumerator UploadUserImageAction()
  97. {
  98. WWWForm wwwForm = new WWWForm();
  99. wwwForm.AddField("user_code", uUID.user_code);
  100. wwwForm.AddBinaryData("file", uUID.file);
  101. using (UnityWebRequest www = UnityWebRequest.Post(USERIMAGEUPLOADURL, wwwForm))
  102. {
  103. www.downloadHandler = new DownloadHandlerBuffer();
  104. yield return www.SendWebRequest();
  105. if (www.result != UnityWebRequest.Result.Success)
  106. {
  107. Debug.Log($"[U2ZCM] {www.error}");
  108. yield break;
  109. }
  110. Debug.Log($"[U2ZCM] {www.downloadHandler.text}");
  111. rUID = JsonConvert.DeserializeObject<Respon_UserImageData>(www.downloadHandler.text);
  112. if (rUID.code != 0)
  113. {
  114. Debug.Log($"[U2ZCM] Code Error");
  115. yield break;
  116. }
  117. DownloadQRCode(rUID.data.qr_url); // 搞图
  118. }
  119. yield break;
  120. }
  121. public class Upload_UserImageData
  122. {
  123. public string user_code;
  124. public byte[] file;
  125. }
  126. [SerializeField] private Respon_UserImageData rUID = new Respon_UserImageData();
  127. [Serializable]
  128. public class Respon_UserImageData
  129. {
  130. public int code;
  131. public string message;
  132. public Respon_UserImageDataData data;
  133. }
  134. [Serializable]
  135. public class Respon_UserImageDataData
  136. {
  137. public string qr_url;
  138. }
  139. #endregion
  140. // ==================================================
  141. #region 获取QR图片
  142. public static UnityAction<Texture2D> OnQRImageDownloaded;
  143. [SerializeField] private Texture2D debug_peekQRCode;
  144. public void DownloadQRCode(string url) => StartCoroutine(nameof(DownloadQRCodeAction), url);
  145. private IEnumerator DownloadQRCodeAction(string url)
  146. {
  147. using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(url))
  148. {
  149. yield return www.SendWebRequest();
  150. if (www.result != UnityWebRequest.Result.Success)
  151. {
  152. Debug.Log($"[U2ZCM] {www.error}");
  153. yield break;
  154. }
  155. debug_peekQRCode = DownloadHandlerTexture.GetContent(www); // DEBUG
  156. if (OnQRImageDownloaded != null)
  157. {
  158. OnQRImageDownloaded(DownloadHandlerTexture.GetContent(www));
  159. }
  160. }
  161. yield break;
  162. }
  163. #endregion
  164. // ==================================================
  165. #region 上传文件流
  166. private const string uploadURL = @"https://vw-aud.studiocapsule.cn/api/device/uploadWall";
  167. public UnityAction<string> OnUploadFinished;
  168. public void UploadData(byte[] fileBytes) => StartCoroutine(nameof(UploadDataAction), fileBytes);
  169. private IEnumerator UploadDataAction(byte[] fileBytes)
  170. {
  171. WWWForm wwwForm = new WWWForm();
  172. wwwForm.AddBinaryData("file", fileBytes);
  173. using (UnityWebRequest www = UnityWebRequest.Post(uploadURL, wwwForm))
  174. {
  175. // www.SetRequestHeader("Content-Type", "multipart/form-data"); // wwwForm不要手动设置避免boundary消失
  176. www.downloadHandler = new DownloadHandlerBuffer();
  177. yield return www.SendWebRequest();
  178. if (www.result != UnityWebRequest.Result.Success)
  179. {
  180. Debug.Log($"[U2ZCM] {www.error}");
  181. yield break;
  182. }
  183. Debug.Log($"[U2ZCM] {www.downloadHandler.text}");
  184. ResponData responData = JsonConvert.DeserializeObject<ResponData>(www.downloadHandler.text);
  185. // // 解析方案A 动态类型
  186. // dynamic data = responData.data;
  187. // string qr = data.qr_url;
  188. // 解析方案B
  189. JObject data = JObject.FromObject(responData.data);
  190. string qr_url = data["qr_url"].ToString();
  191. // Debug.Log(qr_url);
  192. DownloadQRCode(qr_url);
  193. }
  194. yield break;
  195. }
  196. #endregion
  197. // ==================================================
  198. public class ResponData
  199. {
  200. public int code;
  201. public string message;
  202. public object data;
  203. }
  204. }