123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System.Collections;
- using System.Text;
- using Blue;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- using UnityEngine.Networking;
- public interface IMinMapService : IService
- {
- void Get();
- void Set();
- /// <summary>
- /// 设置是否显示 0 = 不显示; 1 = 显示
- /// </summary>
- BindableProperty<int> setMinMap{ get; set; }
- }
- public class MinMapService : IMinMapService
- {
- private string message;
- public BindableProperty<int> setMinMap { get; set; } = new BindableProperty<int>(2);
- public void OnInit()
- {
- // userInfoGetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/getUserInfo";
- // userInfoSetUrl = "https://api-fat3.ghz-tech.com/mr-navigator/v1/user/settings";
- }
- public void Get()
- {
- CoroutineSystem.Instance.StartCoroutine(GetRequest());
- }
- public void Set()
- {
- CoroutineSystem.Instance.StartCoroutine(SetRequest(setMinMap.Value));
- }
- private IEnumerator GetRequest()
- {
- using (UnityWebRequest webRequest = new UnityWebRequest(HttpAction.Instance.baseurl+ "/user/getUserInfo"))
- {
- webRequest.downloadHandler = new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- yield return webRequest.SendWebRequest();
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- {
- //Debug.LogError(webRequest.error);
- //Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
- }
- else
- {
- //Debug.LogError(webRequest.downloadHandler.text);
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
- {
- message = webRequest.downloadHandler.text;
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200")
- {
- message = jobject["data"].ToString();
- //Debug.LogError(message);
- if (!string.IsNullOrWhiteSpace(message))
- {
- GetUserInfo getUserInfo = JsonConvert.DeserializeObject<GetUserInfo>(message);
- Debug.LogError("CY 值:"+getUserInfo.minMap);
- setMinMap.Value = getUserInfo.minMap;
- }
- }
- }
- }
- }
- }
- private IEnumerator SetRequest(int isOpen)
- {
- using (UnityWebRequest webRequest = new UnityWebRequest(HttpAction.Instance.baseurl + "/user/settings", "POST"))
- {
- GetUserInfo getUserInfo = new GetUserInfo();
- getUserInfo.minMap = isOpen;
- string jsonData = JsonConvert.SerializeObject(getUserInfo);
- Debug.LogError("JsonData"+jsonData);
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- yield return webRequest.SendWebRequest();
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
- else
- {
- //Debug.LogError("上传:"+webRequest.downloadHandler.text);
- }
- }
- }
- }
|