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(); /// /// 设置是否显示 0 = 不显示; 1 = 显示 /// BindableProperty setMinMap{ get; set; } } public class MinMapService : IMinMapService { private string message; public BindableProperty setMinMap { get; set; } = new BindableProperty(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(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); } } } }