WebController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using UnityEngine.SceneManagement;
  9. /* 本文件二次封装Web request
  10. * 不需要客户端确认是否需要提交accss_token,access_token白名单在EnviromentSetting里面
  11. * PostRequest 需要提交url, wwwForm, 上传附件path, 回调函数
  12. */
  13. public class WebController : MonoBehaviour {
  14. // Start is called before the first frame update
  15. //void Start()
  16. //{
  17. //}
  18. // Update is called once per frame
  19. //void Update()
  20. //{
  21. //}
  22. public static IEnumerator PostRequest(string url, WWWForm wwwForm, string filePath = null, System.Action<string> callback=null)
  23. {
  24. // 检测是否需要添加access_token
  25. bool accessTokenReq = true;
  26. if (EnviromentSetting.accessTokenWhiteList.Contains<string>(url))
  27. {
  28. accessTokenReq = false;
  29. }
  30. //foreach (string whiteUrl in EnviromentSetting.accessTokenWhiteList)
  31. //{
  32. // if (url == whiteUrl)
  33. // {
  34. // accessTokenReq = false;
  35. // }
  36. //}
  37. // 添加access token
  38. if (accessTokenReq)
  39. {
  40. // 如果access token过期,跳转登录场景
  41. TimeSpan ts = DateTime.Now - EnviromentSetting.accessTokenReceivedTime;
  42. if (ts.TotalHours >= 24)
  43. {
  44. GameData.subScene = null;
  45. SceneManager.LoadScene("Login");
  46. }
  47. else
  48. {
  49. wwwForm.AddField("access_token", EnviromentSetting.accessToken);
  50. }
  51. }
  52. // 添加表单字段
  53. //foreach (var item in postData)
  54. //{
  55. // form.AddField(item.Key, item.Value);
  56. //}
  57. // 添加文件
  58. if (filePath != null)
  59. {
  60. byte[] fileData = System.IO.File.ReadAllBytes(filePath);
  61. wwwForm.AddBinaryData(Path.GetFileName(filePath), fileData);
  62. }
  63. // 创建 UnityWebRequest 对象
  64. url = EnviromentSetting.serverIp + url;
  65. using (UnityWebRequest request = UnityWebRequest.Post(url, wwwForm))
  66. {
  67. // 发送请求并等待响应
  68. yield return request.SendWebRequest();
  69. // 检查是否有错误
  70. if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
  71. {
  72. Debug.Log("上传失败: " + request.error);
  73. if (request.error == "Cannot connect to destination host")
  74. {
  75. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "network_error", EnviromentSetting.languageCode });
  76. MessageBoxController.ShowMessage(msg, () => Application.Quit());
  77. }
  78. Debug.Log("上传失败信息: " + request.downloadHandler.text);
  79. }
  80. else
  81. {
  82. // 上传成功,获取服务器响应
  83. string responseText = request.downloadHandler.text;
  84. callback?.Invoke(responseText);
  85. Debug.Log("上传成功!服务器响应: " + responseText);
  86. }
  87. }
  88. }
  89. }