IMinMapService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 message;
  20. public BindableProperty<int> setMinMap { get; set; } = new BindableProperty<int>(2);
  21. public void OnInit()
  22. {
  23. // userInfoGetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/getUserInfo";
  24. // userInfoSetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/settings";
  25. }
  26. public void Get()
  27. {
  28. CoroutineSystem.Instance.StartCoroutine(GetRequest());
  29. }
  30. public void Set()
  31. {
  32. CoroutineSystem.Instance.StartCoroutine(SetRequest(setMinMap.Value));
  33. }
  34. private IEnumerator GetRequest()
  35. {
  36. using (UnityWebRequest webRequest = new UnityWebRequest(HttpAction.Instance.baseurl+ "/user/getUserInfo"))
  37. {
  38. webRequest.downloadHandler = new DownloadHandlerBuffer();
  39. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  40. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  41. yield return webRequest.SendWebRequest();
  42. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  43. {
  44. //Debug.LogError(webRequest.error);
  45. //Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  46. }
  47. else
  48. {
  49. //Debug.LogError(webRequest.downloadHandler.text);
  50. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  51. {
  52. message = webRequest.downloadHandler.text;
  53. JObject jobject = JObject.Parse(message);
  54. if (jobject["code"].ToString() == "200")
  55. {
  56. message = jobject["data"].ToString();
  57. //Debug.LogError(message);
  58. if (!string.IsNullOrWhiteSpace(message))
  59. {
  60. GetUserInfo getUserInfo = JsonConvert.DeserializeObject<GetUserInfo>(message);
  61. Debug.LogError("CY 值:"+getUserInfo.minMap);
  62. setMinMap.Value = getUserInfo.minMap;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. private IEnumerator SetRequest(int isOpen)
  70. {
  71. using (UnityWebRequest webRequest = new UnityWebRequest(HttpAction.Instance.baseurl + "/user/settings", "POST"))
  72. {
  73. GetUserInfo getUserInfo = new GetUserInfo();
  74. getUserInfo.minMap = isOpen;
  75. string jsonData = JsonConvert.SerializeObject(getUserInfo);
  76. Debug.LogError("JsonData"+jsonData);
  77. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  78. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  79. webRequest.downloadHandler = new DownloadHandlerBuffer();
  80. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  81. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  82. yield return webRequest.SendWebRequest();
  83. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  84. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  85. else
  86. {
  87. //Debug.LogError("上传:"+webRequest.downloadHandler.text);
  88. }
  89. }
  90. }
  91. }