123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class LangChaoJiekou : MonoSingleton<LangChaoJiekou>
- {
- //1.调用 /task/getMyList接口传参为header中的Authorization和distributeTarget(机房名称,后续根据需要再修改),这个接口跟上边的/task/getList接口返回的内容结构是一样的,所以这个几口下我没再写返回定义
- //2.通过/task/getMyList可以查询到是否有任务,有的话返回值里有inspId字段,是巡检任务的ID
- //根据这个inspId调用/insp/getInfo接口获取巡检的详细信息,主要是inspItemList里有这次巡检需要巡检的巡检项ID列表
- //3./inspItem/getList这个接口返回的是系统中定义的所有巡检项,通过inspItemList中的ID可以对应出本次巡检任务中的巡检项信息
- //4.开始巡检时调用/record/execute接口,参数id传distributeId的值
- //5.通过/record/addRecord接口上传巡检项的巡检记录,其中distributeId来自/task/getMyList的接口返回
- //6.图片文件通过文件上传接口上传,然后将上传接口返回的data字段的值存到recordImg字段中,多张图片的返回值可以用逗号隔开
- //7.通过/record/getRecord接口查询回显数据,查询出的图片字段想要显示可以分别调用文件下载接口,将值作为fileName参数就可以得到
- //inline的图片了
-
- public const string insp_getlist = "/inms/metaverse-cooperate/insp/getInfo";
- public const string inspItem_getlist = "/inms/metaverse-cooperate/inspItem/getList";
- public const string task_getlist = "/inms/metaverse-cooperate/task/getList";
- public const string task_getMyList = "/inms/metaverse-cooperate/task/getMyList";
- public const string record_addRecord = "/inms/metaverse-cooperate/record/addRecord";
- public const string record_getRecord = "/inms/metaverse-cooperate/record/getRecord";
- public const string anon_login = "/inms/api/auth-external-microservice/v1/public/anon/login";
- public const string file_upload = "/inms/metaverse-cooperate/file/upload";
- public const string file_download = "/inms/metaverse-cooperate/file/download";
- public const string execute = "/inms/metaverse-cooperate/record/execute";
- public const string getmachinelist = "/inms/metaverse-cooperate/task/getRoom";
- public string token = "";
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- //登录
- public void GotToLogin(string username, string password,Action<bool> callback)
- {
- JsonData data = new JsonData();
- data["appCode"] = "METAVERSE";
- data["password"] = password;
- data["username"] = username;
- StartCoroutine(SendHttp(anon_login, data.ToJson(), (string msg) =>
- {
- Debug.Log("anon_login===>" + msg);
- try
- {
- JsonData d = JsonMapper.ToObject(msg);
- token = d["data"]["token"]["accessToken"].ToString();
- login.UserInfo.Instance.Token = token;
- callback.Invoke(true);
- }
- catch
- {
- callback.Invoke(false);
- }
- }));
- }
- public void GetMachineList( Action<List<string>> callback)
- {
- StartCoroutine(SendHttp(getmachinelist, "", (msg) =>
- {
- Debug.Log(getmachinelist + " " + msg);
- JsonData data = JsonMapper.ToObject(msg);
- string str = data["data"].ToString();
- Debug.Log(str);
- Person listMachineArry = JsonConvert.DeserializeObject<Person>(msg);
-
- callback(listMachineArry.data);
- }, "application/json",false));
- }
- public class Person
- {
- public string code { get; set; }
- public string msg { get; set; }
- public List<string> data { get; set; }
- public string traceId { get; set; }
- }
- //检查是否有巡检
- public void checkXunjian(string distributeTarget,Action<TaskItem> callback)
- {
- JsonData data = new JsonData();
- data["distributeTarget"] = distributeTarget;//"机房A";
- StartCoroutine(SendHttp(task_getMyList, data.ToJson(), (string msg) => {
- Debug.Log("task_getMyList===>" + msg);
- try
- {
- JsonData data = JsonMapper.ToObject(msg);
- TaskItem ti = JsonConvert.DeserializeObject<TaskItem>(data["data"]["records"][0].ToJson());
- if(ti.inspId!=null)
- callback.Invoke(ti);
- else
- callback.Invoke(null);
- }
- catch
- {
- callback.Invoke(null);
- }
- }));
- }
- //获取巡检项目列表
- public void getXunJian(TaskItem ti,Action<List<RoomItem>> callback)
- {
- JsonData data = new JsonData();
- data["id"] = ti.distributeId;
- StartCoroutine(SendHttp(record_getRecord, data.ToJson(), (string msg) =>
- {
- Debug.Log("record_getRecord===>"+msg);
- List<Record> tslist = new List<Record>();
- try
- {
- JsonData data2 = JsonMapper.ToObject(msg);
- tslist = JsonConvert.DeserializeObject<List<Record>>(data2["data"]["recordList"].ToJson());
- }
- catch
- {
- }
- JsonData data = new JsonData();
- data["inspId"] = ti.inspId;
- StartCoroutine(SendHttp(insp_getlist, data.ToJson(), (string msg) => {
- Debug.Log("inspItem_getlist===>" + msg);
- JsonData d2 = JsonMapper.ToObject(msg);
- try
- {
- List<RoomItem> rdlist = JsonConvert.DeserializeObject<List<RoomItem>>(d2["data"]["inspItemList"].ToJson());
- for (int i = 0; i < rdlist.Count; i++)
- {
- for (int j = 0; j < tslist.Count; j++)
- {
- if (rdlist[i].inspItemId == tslist[j].inspItemId)
- {
- rdlist[i].recd = tslist[j];
- }
- }
- }
- callback.Invoke(rdlist);
- }
- catch
- {
- callback.Invoke(new List<RoomItem>());
- }
- }, "", false));
- },"",false));
- }
- //上报记录
- public void addRecord(Record add,Action<bool> callback)
- {
- /*
- AddRecord add = new AddRecord();
- add.distributeId = ti.distributeId;
- add.recordTargetType = "1";
- add.recordTarget = ti.distributeTarget;
- add.recordTargetId = ti.distributeTargetId;
- add.inspItemId = "1722899997636894720";
- add.inspItemName = "机房巡检项1";
- add.recordImg = "common/2023/12/file.png";
- add.recordMemo = "异常情况2";
- add.recordStatus = "0";
- addRecord(add, (bool b) => { });*/
- Debug.Log("DGJ ===> addRecord " + JsonConvert.SerializeObject(add));
- StartCoroutine(SendHttp(record_addRecord, JsonConvert.SerializeObject(add), (string msg) => {
- try
- {
- JsonData data = JsonMapper.ToObject(msg);
- if (data["msg"].ToString() == "Success")
- {
- callback.Invoke(true);
- }else
- {
- callback.Invoke(false);
- }
- }
- catch
- {
- callback.Invoke(false);
- }
- Debug.Log("record_addRecord====>" + msg);
- }));
- }
- public class TaskItem
- {
- public string distributeId;
- public string inspId;
- public string resourcePool;
- public string distributeTime;
- public string distributeName;
- public string distributeTarget;
- public string distributeTargetId;
- public string distributePerson;
- public string distributePersonName;
- public string distributeExecutor;
- public string distributeExecutorName;
- public string distributeExecuteTime;
- public string inspType;
- public string distributeStatus;
- public string remindId;
- public string updateTime;
- public string createTime;
- }
- public class RoomError
- {
- public string createTime;
- public string itemErrorId;
- public string createUser;
- public string createUserName;
- public string inspItemId;
- public string itemErrorName;
- }
- public class RoomItem
- {
- public string createTime;
- public string inspItemType;
- public List<RoomError> errorList;
- public string inspItemName;
- public string inspItemDesc;
- public string createUser;
- public string createUserName;
- public string inspItemId;
- public Record recd;
- }
- /// <summary>
- /// 上报信息
- /// </summary>
- public class Record
- {
- public string distributeId;
- public string recordTargetType;
- public string recordTarget;
- public string recordTargetId;
- public string inspItemId;
- public string inspItemName;
- // public string recordImg; // 图片
- public string recordMemo; // 巡检备注
- public string recordStatus;// 1-正常 0-异常
- public List<string> recordImgList;// 图片
- }
- public void Start()
- {
- /* GotToLogin("lisongnan", "13579Wp!#%&(", (bool b) => {
- if (b)
- {
-
- }
- });
- */
- //测试接口
- /* GotToLogin("lisongnan", "13579Wp!#%&(",(bool b)=> {
- if (b)
- {
- checkXunjian("机房A", (TaskItem item) => {
- if(item!=null)
- {
- getXunJian(item, (List<RoomItem> list)=>{
- for (int i = 0; i < list.Count; i++)
- {
- Debug.Log("RoomItem===>"+JsonConvert.SerializeObject(list[i]));
- }
- });
- }
- });
- }
- });
- StartCoroutine(UploadFile("D:\\Screenshot_0_Btn.png", "http://36.133.197.238:10002" + file_upload, (string msg) => {
- Debug.Log("UploadFile===>" + msg);
- JsonData d2 = JsonMapper.ToObject(msg);
- JsonData data = new JsonData();
- data["fileName"] = d2["data"].ToString();
- StartCoroutine(DownloadImage(file_download, data.ToJson(), (Texture2D msg) => {
- // img.texture = msg;
- }));
- }));*/
- }
- public void initHead(string ContentType)
- {
- requestHeader.Clear();
- requestHeader.Add("Authorization", token);
- requestHeader.Add("Content-Type", ContentType);
- }
- //下载图片
- public IEnumerator DownloadImage(string url, string jsonString, Action<Texture2D> CallBack)
- {
- UnityWebRequest webRequest;
- url = "http://36.133.197.238:10002" + url;
- JsonData data = JsonMapper.ToObject(jsonString);
- url += "?";
- foreach (var item in data.Keys)
- {
- url += item + "=" + data[item].ToString() + "&";
- }
- webRequest = UnityWebRequest.Get(url);
-
- Debug.Log("下载==》" + url + " " + jsonString);
- Debug.Log("下载参数==》" + jsonString );
- using (webRequest)
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
-
- while(!webRequest.downloadHandler.isDone)
- {
- yield return null;
- }
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- CallBack(null);
- }
- else
- {
-
- Debug.Log("CallBack==>" + webRequest.downloadHandler.data.Length);
- Texture2D d2d = new Texture2D(1,1);
- d2d.LoadImage(webRequest.downloadHandler.data);
- CallBack(d2d);
- }
- }
- }
- public IEnumerator DownloadImage(string url, string jsonString, Action<Texture2D ,string> CallBack)
- {
- UnityWebRequest webRequest;
- url = "http://36.133.197.238:10002" + url;
- JsonData data = JsonMapper.ToObject(jsonString);
- url += "?";
- foreach (var item in data.Keys)
- {
- url += item + "=" + data[item].ToString() + "&";
- }
- webRequest = UnityWebRequest.Get(url);
- Debug.Log("下载==》" + url + " " + jsonString);
- Debug.Log("下载参数==》" + jsonString);
- using (webRequest)
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- while (!webRequest.downloadHandler.isDone)
- {
- yield return null;
- }
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- CallBack(null,jsonString);
- }
- else
- {
- Debug.Log("CallBack==>" + webRequest.downloadHandler.data.Length);
- Texture2D d2d = new Texture2D(1, 1);
- d2d.LoadImage(webRequest.downloadHandler.data);
- CallBack(d2d, jsonString);
- }
- }
- }
- //上传图片
- public IEnumerator UploadFile(string path, string url, Action<string> CallBack)
- {
-
- // 读取要上传的文件
- byte[] bytes = File.ReadAllBytes(path);
- WWWForm formData = new WWWForm();
- formData.AddBinaryData("file", bytes);
- Debug.Log("DGJ ===> UploadFile URL " + url + " Path" + path +" Bytes "+ bytes.Length);
- // 创建UnityWebRequest对象
- UnityWebRequest request = UnityWebRequest.Post(url, formData);
- // 发送请求
- yield return request.SendWebRequest();
- // 处理响应数据
- if (request.isNetworkError || request.isHttpError)
- {
- Debug.Log(request.error);
- }
- else
- {
- CallBack?.Invoke(request.downloadHandler.text);
- Debug.Log("Upload complete!"+ request.downloadHandler.text);
- }
- }
- public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack, string ContentType = "application/json", bool isPost = true)
- {
- Debug.Log("Start Queue SendHttp " + methodName + " jsonstring==>" + jsonString);
- string url;
- url = "http://36.133.197.238:10002" + methodName;
- Debug.Log("URL:" + url + " isPost " + isPost);
- UnityWebRequest webRequest;
- if (!isPost)
- {
- JsonData data = JsonMapper.ToObject(jsonString);
- url += "?";
- foreach (var item in data.Keys)
- {
- url += item + "=" + data[item].ToString() + "&";
- }
- Debug.Log("URL:" + url + " isPost " + isPost);
- webRequest = UnityWebRequest.Get(url);
- }
- else
- {
- webRequest = new UnityWebRequest(url, "POST");
- }
- initHead(ContentType);
- using (webRequest)
- {
- if (!isPost)
- {
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- }
- else
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- }
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- Debug.Log("CallBack==>" + webRequest.downloadHandler.text);
- if (webRequest.isHttpError || webRequest.isNetworkError)
- {
- Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
- string error = webRequest.downloadHandler.text;
- JObject jObject = JObject.Parse(error);
- CallBack(error);
- }
- else
- {
- //Debug.Log(webRequest.downloadHandler.text);
- var mes = webRequest.downloadHandler.text;
- CallBack(mes);
- }
- }
- }
-
- }
- /*
-
- public RawImage img;
- public void login()
- {
- Record add = new Record();
- add.distributeId = "1732204405933334528";
- add.recordTargetType = "1";
- add.recordTarget = "";
- add.recordTargetId = "";
- add.inspItemId = "";
- add.inspItemName = "";
- add.recordImg = "";
- add.recordMemo = "";
- add.recordStatus = "";
- JsonData data = new JsonData();
- data["appCode"] = "METAVERSE";
- data["password"] = "13579Wp!#%&(";
- data["username"] = "lisongnan";
- StartCoroutine(SendHttp(anon_login, data.ToJson(), (string msg)=> {
- Debug.Log("anon_login===>" + msg);
- JsonData d = JsonMapper.ToObject(msg);
- token = d["data"]["token"]["accessToken"].ToString();
- JsonData data = new JsonData();
- data["distributeTarget"] = "机房A";
- StartCoroutine(SendHttp(task_getMyList, data.ToJson(), (string msg) => {
- Debug.Log("task_getMyList===>"+ msg);
- JsonData d2 = JsonMapper.ToObject(msg);
- JsonData data = new JsonData();
- data["inspId"] = d2["data"]["records"][0]["inspId"].ToString();
- string distributeId = d2["data"]["records"][0]["distributeId"].ToString();
- StartCoroutine(SendHttp(inspItem_getlist, data.ToJson(), (string msg) => {
- Debug.Log("inspItem_getlist===>" + msg);
- JsonData d2 = JsonMapper.ToObject(msg);
- JsonData data = new JsonData();
- data["id"] = distributeId;
- StartCoroutine(SendHttp(record_getRecord, data.ToJson(), (string msg) => {
- Debug.Log("record_getRecord===>" + msg);
- JsonData d2 = JsonMapper.ToObject(msg);
- StartCoroutine(UploadFile("D:\\Button.png", "http://36.133.197.238:10002" + file_upload, (string msg) => {
- Debug.Log("UploadFile===>" + msg);
- JsonData d2 = JsonMapper.ToObject(msg);
-
- JsonData data = new JsonData();
- data["fileName"] = d2["data"].ToString();
- StartCoroutine(DownloadImage(file_download, data.ToJson(), (Texture2D msg) => {
- img.texture = msg;
- }));
- }));
- }, "application/json", false));
- }));
- }));
- Debug.Log("login===>"+msg); })) ;
- }
- */
|