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.
 
 
 

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