IMinMapService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections;
  2. using System.Text;
  3. using Blue;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public interface IMinMapService : IService
  9. {
  10. void Get();
  11. void Set();
  12. /// <summary>
  13. /// 设置是否显示 0 = 不显示; 1 = 显示
  14. /// </summary>
  15. BindableProperty<int> setMinMap{ get; set; }
  16. }
  17. public class MinMapService : IMinMapService
  18. {
  19. private string userInfoGetUrl;
  20. private string userInfoSetUrl;
  21. private string message;
  22. public BindableProperty<int> setMinMap { get; set; } = new BindableProperty<int>(2);
  23. public void OnInit()
  24. {
  25. userInfoGetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/getUserInfo";
  26. userInfoSetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/settings";
  27. }
  28. public void Get()
  29. {
  30. CoroutineSystem.Instance.StartCoroutine(GetRequest());
  31. }
  32. public void Set()
  33. {
  34. CoroutineSystem.Instance.StartCoroutine(SetRequest(setMinMap.Value));
  35. }
  36. private IEnumerator GetRequest()
  37. {
  38. using (UnityWebRequest webRequest = new UnityWebRequest(userInfoGetUrl))
  39. {
  40. webRequest.downloadHandler = new DownloadHandlerBuffer();
  41. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  42. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  43. yield return webRequest.SendWebRequest();
  44. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  45. {
  46. //Debug.LogError(webRequest.error);
  47. //Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  48. }
  49. else
  50. {
  51. //Debug.LogError(webRequest.downloadHandler.text);
  52. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  53. {
  54. message = webRequest.downloadHandler.text;
  55. JObject jobject = JObject.Parse(message);
  56. if (jobject["code"].ToString() == "200")
  57. {
  58. message = jobject["data"].ToString();
  59. //Debug.LogError(message);
  60. if (!string.IsNullOrWhiteSpace(message))
  61. {
  62. GetUserInfo getUserInfo = JsonConvert.DeserializeObject<GetUserInfo>(message);
  63. //Debug.LogError("值:"+getUserInfo.minMap);
  64. setMinMap.Value = getUserInfo.minMap;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. private IEnumerator SetRequest(int isOpen)
  72. {
  73. using (UnityWebRequest webRequest = new UnityWebRequest(userInfoSetUrl, "POST"))
  74. {
  75. GetUserInfo getUserInfo = new GetUserInfo();
  76. getUserInfo.minMap = isOpen;
  77. string jsonData = JsonConvert.SerializeObject(getUserInfo);
  78. Debug.LogError("JsonData"+jsonData);
  79. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  80. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  81. webRequest.downloadHandler = new DownloadHandlerBuffer();
  82. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  83. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  84. yield return webRequest.SendWebRequest();
  85. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  86. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  87. else
  88. {
  89. //Debug.LogError("上传:"+webRequest.downloadHandler.text);
  90. }
  91. }
  92. }
  93. }