You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

196 regels
6.1 KiB

  1. using CIG;
  2. using Cysharp.Threading.Tasks;
  3. using System;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. public class HttpManager : MonoBehaviour
  8. {
  9. static public int isZhengShi = 3;
  10. string urlHead = GetUrlHead(isZhengShi);
  11. [HideInInspector]
  12. public string phoneNum = "";
  13. [HideInInspector]
  14. public string wxUnionID = "";
  15. [HideInInspector]
  16. public string LastOrderNo;
  17. public CIG3StoreProduct LastProduct;
  18. static string GetUrlHead(int _isZhengShi)
  19. {
  20. string urlHead;
  21. switch (_isZhengShi)
  22. {
  23. case 0:
  24. urlHead = "http://192.168.0.113:8000/yogame/";
  25. break;
  26. case 1:
  27. urlHead = "http://cat.xiuxiangames.com/yogame/";
  28. break;
  29. case 2:
  30. urlHead = "https://cat.xiuxiangames.com/yogameuat/";
  31. break;
  32. case 3:
  33. urlHead = "https://zshi.gamesverses.com/zsmxj/";
  34. break;
  35. default:
  36. urlHead = "http://192.168.0.113:8000/yogame/"; // 默认地址
  37. break;
  38. }
  39. return urlHead;
  40. }
  41. // 单例模式
  42. public static HttpManager Instance { get; private set; }
  43. private void Awake()
  44. {
  45. // 实现单例模式
  46. if (Instance != null && Instance != this)
  47. {
  48. Destroy(gameObject);
  49. }
  50. else
  51. {
  52. Instance = this;
  53. DontDestroyOnLoad(gameObject);
  54. }
  55. }
  56. public async UniTask<T> RequestPost<F, T>(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new()
  57. {
  58. string url = urlHead + _url;
  59. string json = req.ToJson();
  60. Debug.Log("Request URL: " + url);
  61. Debug.Log("Request Body (JSON): " + json);
  62. Debug.Log("Request Body Length: " + Encoding.UTF8.GetBytes(json).Length);
  63. try
  64. {
  65. // 使用 UnityWebRequest.Post 初始化请求对象
  66. using (UnityWebRequest www = UnityWebRequest.Post(url, ""))
  67. {
  68. www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  69. www.SetRequestHeader("Accept", "*/*");
  70. // 手动设置 UploadHandler 以发送 JSON 数据
  71. www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json))
  72. {
  73. contentType = "application/json"
  74. };
  75. www.downloadHandler = new DownloadHandlerBuffer();
  76. try
  77. {
  78. UnityWebRequest op = await www.SendWebRequest().ToUniTask();
  79. //Debug.Log("HTTP Status Code: " + www.responseCode);
  80. //if (op.result != UnityWebRequest.Result.Success)
  81. //{
  82. // Debug.LogError("Error: " + op.error);
  83. // return null;
  84. //}
  85. Debug.Log("HTTP Status Code: " + www.responseCode);
  86. if (www.isNetworkError || www.isHttpError)
  87. {
  88. Debug.LogError("Error: " + www.error);
  89. return null;
  90. }
  91. if (string.IsNullOrEmpty(op.downloadHandler.text))
  92. {
  93. Debug.LogError("Empty or null response received.");
  94. return null;
  95. }
  96. Debug.Log("Response Text: " + op.downloadHandler.text);
  97. T response = JsonUtility.FromJson<T>(op.downloadHandler.text);
  98. return response;
  99. }
  100. catch (Exception ex)
  101. {
  102. Debug.LogError("Exception during request: " + ex);
  103. return null;
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. Debug.LogError("Error creating UnityWebRequest: " + ex.Message);
  110. return null;
  111. }
  112. }
  113. public async UniTask<T> RequestGet<F, T>(string _url, F req) where F : WWWRequest, new() where T : WWWResponse, new()
  114. {
  115. string url = urlHead + _url;
  116. // 将请求对象序列化为 JSON,并进行 URL 编码
  117. string json = req.ToJson();
  118. Debug.Log("Request URL: " + url);
  119. Debug.Log("Request Body (JSON): " + json);
  120. string encodedQuery = req.ToQueryString();
  121. url += "?" + encodedQuery;
  122. Debug.Log("Request URL with Query Parameter: " + url);
  123. try
  124. {
  125. // 使用 UnityWebRequest.Get 初始化请求对象
  126. using (UnityWebRequest www = UnityWebRequest.Get(url))
  127. {
  128. www.SetRequestHeader("Accept", "*/*");
  129. try
  130. {
  131. UnityWebRequest op = await www.SendWebRequest().ToUniTask();
  132. //Debug.Log("HTTP Status Code: " + www.responseCode);
  133. //if (op.result != UnityWebRequest.Result.Success)
  134. //{
  135. // Debug.LogError("Error: " + op.error);
  136. // return null;
  137. //}
  138. Debug.Log("HTTP Status Code: " + www.responseCode);
  139. if (www.isNetworkError || www.isHttpError)
  140. {
  141. Debug.LogError("Error: " + www.error);
  142. return null;
  143. }
  144. if (string.IsNullOrEmpty(op.downloadHandler.text))
  145. {
  146. Debug.LogError("Empty or null response received.");
  147. return null;
  148. }
  149. Debug.Log("Response Text: " + op.downloadHandler.text);
  150. T response = JsonUtility.FromJson<T>(op.downloadHandler.text);
  151. return response;
  152. }
  153. catch (Exception ex)
  154. {
  155. Debug.LogError("Exception during request: " + ex);
  156. return null;
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. Debug.LogError("Error creating UnityWebRequest: " + ex.Message);
  163. return null;
  164. }
  165. }
  166. }