LangChaoJiekou.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text;
  9. using UnityEngine;
  10. using UnityEngine.Networking;
  11. using UnityEngine.UI;
  12. public class LangChaoJiekou : MonoSingleton<LangChaoJiekou>
  13. {
  14. //1.调用 /task/getMyList接口传参为header中的Authorization和distributeTarget(机房名称,后续根据需要再修改),这个接口跟上边的/task/getList接口返回的内容结构是一样的,所以这个几口下我没再写返回定义
  15. //2.通过/task/getMyList可以查询到是否有任务,有的话返回值里有inspId字段,是巡检任务的ID
  16. //根据这个inspId调用/insp/getInfo接口获取巡检的详细信息,主要是inspItemList里有这次巡检需要巡检的巡检项ID列表
  17. //3./inspItem/getList这个接口返回的是系统中定义的所有巡检项,通过inspItemList中的ID可以对应出本次巡检任务中的巡检项信息
  18. //4.开始巡检时调用/record/execute接口,参数id传distributeId的值
  19. //5.通过/record/addRecord接口上传巡检项的巡检记录,其中distributeId来自/task/getMyList的接口返回
  20. //6.图片文件通过文件上传接口上传,然后将上传接口返回的data字段的值存到recordImg字段中,多张图片的返回值可以用逗号隔开
  21. //7.通过/record/getRecord接口查询回显数据,查询出的图片字段想要显示可以分别调用文件下载接口,将值作为fileName参数就可以得到
  22. //inline的图片了
  23. public const string insp_getlist = "/inms/metaverse-cooperate/insp/getInfo";
  24. public const string inspItem_getlist = "/inms/metaverse-cooperate/inspItem/getList";
  25. public const string task_getlist = "/inms/metaverse-cooperate/task/getList";
  26. public const string task_getMyList = "/inms/metaverse-cooperate/task/getMyList";
  27. public const string record_addRecord = "/inms/metaverse-cooperate/record/addRecord";
  28. public const string record_getRecord = "/inms/metaverse-cooperate/record/getRecord";
  29. public const string anon_login = "/inms/api/auth-external-microservice/v1/public/anon/login";
  30. public const string file_upload = "/inms/metaverse-cooperate/file/upload";
  31. public const string file_download = "/inms/metaverse-cooperate/file/download";
  32. public const string execute = "/inms/metaverse-cooperate/record/execute";
  33. public const string getmachinelist = "/inms/metaverse-cooperate/task/getRoom";
  34. public string token = "";
  35. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  36. //登录
  37. public void GotToLogin(string username, string password,Action<bool> callback)
  38. {
  39. JsonData data = new JsonData();
  40. data["appCode"] = "METAVERSE";
  41. data["password"] = password;
  42. data["username"] = username;
  43. StartCoroutine(SendHttp(anon_login, data.ToJson(), (string msg) =>
  44. {
  45. Debug.Log("anon_login===>" + msg);
  46. try
  47. {
  48. JsonData d = JsonMapper.ToObject(msg);
  49. token = d["data"]["token"]["accessToken"].ToString();
  50. login.UserInfo.Instance.Token = token;
  51. callback.Invoke(true);
  52. }
  53. catch
  54. {
  55. callback.Invoke(false);
  56. }
  57. }));
  58. }
  59. public void GetMachineList( Action<List<string>> callback)
  60. {
  61. StartCoroutine(SendHttp(getmachinelist, "", (msg) =>
  62. {
  63. Debug.Log(getmachinelist + " " + msg);
  64. JsonData data = JsonMapper.ToObject(msg);
  65. string str = data["data"].ToString();
  66. Debug.Log(str);
  67. Person listMachineArry = JsonConvert.DeserializeObject<Person>(msg);
  68. callback(listMachineArry.data);
  69. }, "application/json",false));
  70. }
  71. public class Person
  72. {
  73. public string code { get; set; }
  74. public string msg { get; set; }
  75. public List<string> data { get; set; }
  76. public string traceId { get; set; }
  77. }
  78. //检查是否有巡检
  79. public void checkXunjian(string distributeTarget,Action<TaskItem> callback)
  80. {
  81. JsonData data = new JsonData();
  82. data["distributeTarget"] = distributeTarget;//"机房A";
  83. StartCoroutine(SendHttp(task_getMyList, data.ToJson(), (string msg) => {
  84. Debug.Log("task_getMyList===>" + msg);
  85. try
  86. {
  87. JsonData data = JsonMapper.ToObject(msg);
  88. TaskItem ti = JsonConvert.DeserializeObject<TaskItem>(data["data"]["records"][0].ToJson());
  89. if(ti.inspId!=null)
  90. callback.Invoke(ti);
  91. else
  92. callback.Invoke(null);
  93. }
  94. catch
  95. {
  96. callback.Invoke(null);
  97. }
  98. }));
  99. }
  100. //获取巡检项目列表
  101. public void getXunJian(TaskItem ti,Action<List<RoomItem>> callback)
  102. {
  103. JsonData data = new JsonData();
  104. data["id"] = ti.distributeId;
  105. StartCoroutine(SendHttp(record_getRecord, data.ToJson(), (string msg) =>
  106. {
  107. Debug.Log("record_getRecord===>"+msg);
  108. List<Record> tslist = new List<Record>();
  109. try
  110. {
  111. JsonData data2 = JsonMapper.ToObject(msg);
  112. tslist = JsonConvert.DeserializeObject<List<Record>>(data2["data"]["recordList"].ToJson());
  113. }
  114. catch
  115. {
  116. }
  117. JsonData data = new JsonData();
  118. data["inspId"] = ti.inspId;
  119. StartCoroutine(SendHttp(insp_getlist, data.ToJson(), (string msg) => {
  120. Debug.Log("inspItem_getlist===>" + msg);
  121. JsonData d2 = JsonMapper.ToObject(msg);
  122. try
  123. {
  124. List<RoomItem> rdlist = JsonConvert.DeserializeObject<List<RoomItem>>(d2["data"]["inspItemList"].ToJson());
  125. for (int i = 0; i < rdlist.Count; i++)
  126. {
  127. for (int j = 0; j < tslist.Count; j++)
  128. {
  129. if (rdlist[i].inspItemId == tslist[j].inspItemId)
  130. {
  131. rdlist[i].recd = tslist[j];
  132. }
  133. }
  134. }
  135. callback.Invoke(rdlist);
  136. }
  137. catch
  138. {
  139. callback.Invoke(new List<RoomItem>());
  140. }
  141. }, "", false));
  142. },"",false));
  143. }
  144. //上报记录
  145. public void addRecord(Record add,Action<bool> callback)
  146. {
  147. /*
  148. AddRecord add = new AddRecord();
  149. add.distributeId = ti.distributeId;
  150. add.recordTargetType = "1";
  151. add.recordTarget = ti.distributeTarget;
  152. add.recordTargetId = ti.distributeTargetId;
  153. add.inspItemId = "1722899997636894720";
  154. add.inspItemName = "机房巡检项1";
  155. add.recordImg = "common/2023/12/file.png";
  156. add.recordMemo = "异常情况2";
  157. add.recordStatus = "0";
  158. addRecord(add, (bool b) => { });*/
  159. Debug.Log("DGJ ===> addRecord " + JsonConvert.SerializeObject(add));
  160. StartCoroutine(SendHttp(record_addRecord, JsonConvert.SerializeObject(add), (string msg) => {
  161. try
  162. {
  163. JsonData data = JsonMapper.ToObject(msg);
  164. if (data["msg"].ToString() == "Success")
  165. {
  166. callback.Invoke(true);
  167. }else
  168. {
  169. callback.Invoke(false);
  170. }
  171. }
  172. catch
  173. {
  174. callback.Invoke(false);
  175. }
  176. Debug.Log("record_addRecord====>" + msg);
  177. }));
  178. }
  179. public class TaskItem
  180. {
  181. public string distributeId;
  182. public string inspId;
  183. public string resourcePool;
  184. public string distributeTime;
  185. public string distributeName;
  186. public string distributeTarget;
  187. public string distributeTargetId;
  188. public string distributePerson;
  189. public string distributePersonName;
  190. public string distributeExecutor;
  191. public string distributeExecutorName;
  192. public string distributeExecuteTime;
  193. public string inspType;
  194. public string distributeStatus;
  195. public string remindId;
  196. public string updateTime;
  197. public string createTime;
  198. }
  199. public class RoomError
  200. {
  201. public string createTime;
  202. public string itemErrorId;
  203. public string createUser;
  204. public string createUserName;
  205. public string inspItemId;
  206. public string itemErrorName;
  207. }
  208. public class RoomItem
  209. {
  210. public string createTime;
  211. public string inspItemType;
  212. public List<RoomError> errorList;
  213. public string inspItemName;
  214. public string inspItemDesc;
  215. public string createUser;
  216. public string createUserName;
  217. public string inspItemId;
  218. public Record recd;
  219. }
  220. /// <summary>
  221. /// 上报信息
  222. /// </summary>
  223. public class Record
  224. {
  225. public string distributeId;
  226. public string recordTargetType;
  227. public string recordTarget;
  228. public string recordTargetId;
  229. public string inspItemId;
  230. public string inspItemName;
  231. // public string recordImg; // 图片
  232. public string recordMemo; // 巡检备注
  233. public string recordStatus;// 1-正常 0-异常
  234. public List<string> recordImgList;// 图片
  235. }
  236. public void Start()
  237. {
  238. /* GotToLogin("lisongnan", "13579Wp!#%&(", (bool b) => {
  239. if (b)
  240. {
  241. }
  242. });
  243. */
  244. //测试接口
  245. /* GotToLogin("lisongnan", "13579Wp!#%&(",(bool b)=> {
  246. if (b)
  247. {
  248. checkXunjian("机房A", (TaskItem item) => {
  249. if(item!=null)
  250. {
  251. getXunJian(item, (List<RoomItem> list)=>{
  252. for (int i = 0; i < list.Count; i++)
  253. {
  254. Debug.Log("RoomItem===>"+JsonConvert.SerializeObject(list[i]));
  255. }
  256. });
  257. }
  258. });
  259. }
  260. });
  261. StartCoroutine(UploadFile("D:\\Screenshot_0_Btn.png", "http://36.133.197.238:10002" + file_upload, (string msg) => {
  262. Debug.Log("UploadFile===>" + msg);
  263. JsonData d2 = JsonMapper.ToObject(msg);
  264. JsonData data = new JsonData();
  265. data["fileName"] = d2["data"].ToString();
  266. StartCoroutine(DownloadImage(file_download, data.ToJson(), (Texture2D msg) => {
  267. // img.texture = msg;
  268. }));
  269. }));*/
  270. }
  271. public void initHead(string ContentType)
  272. {
  273. requestHeader.Clear();
  274. requestHeader.Add("Authorization", token);
  275. requestHeader.Add("Content-Type", ContentType);
  276. }
  277. //下载图片
  278. public IEnumerator DownloadImage(string url, string jsonString, Action<Texture2D> CallBack)
  279. {
  280. UnityWebRequest webRequest;
  281. url = "http://36.133.197.238:10002" + url;
  282. JsonData data = JsonMapper.ToObject(jsonString);
  283. url += "?";
  284. foreach (var item in data.Keys)
  285. {
  286. url += item + "=" + data[item].ToString() + "&";
  287. }
  288. webRequest = UnityWebRequest.Get(url);
  289. Debug.Log("下载==》" + url + " " + jsonString);
  290. Debug.Log("下载参数==》" + jsonString );
  291. using (webRequest)
  292. {
  293. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  294. foreach (var v in requestHeader)
  295. {
  296. webRequest.SetRequestHeader(v.Key, v.Value);
  297. }
  298. yield return webRequest.SendWebRequest();
  299. while(!webRequest.downloadHandler.isDone)
  300. {
  301. yield return null;
  302. }
  303. if (webRequest.isHttpError || webRequest.isNetworkError)
  304. {
  305. CallBack(null);
  306. }
  307. else
  308. {
  309. Debug.Log("CallBack==>" + webRequest.downloadHandler.data.Length);
  310. Texture2D d2d = new Texture2D(1,1);
  311. d2d.LoadImage(webRequest.downloadHandler.data);
  312. CallBack(d2d);
  313. }
  314. }
  315. }
  316. public IEnumerator DownloadImage(string url, string jsonString, Action<Texture2D ,string> CallBack)
  317. {
  318. UnityWebRequest webRequest;
  319. url = "http://36.133.197.238:10002" + url;
  320. JsonData data = JsonMapper.ToObject(jsonString);
  321. url += "?";
  322. foreach (var item in data.Keys)
  323. {
  324. url += item + "=" + data[item].ToString() + "&";
  325. }
  326. webRequest = UnityWebRequest.Get(url);
  327. Debug.Log("下载==》" + url + " " + jsonString);
  328. Debug.Log("下载参数==》" + jsonString);
  329. using (webRequest)
  330. {
  331. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  332. foreach (var v in requestHeader)
  333. {
  334. webRequest.SetRequestHeader(v.Key, v.Value);
  335. }
  336. yield return webRequest.SendWebRequest();
  337. while (!webRequest.downloadHandler.isDone)
  338. {
  339. yield return null;
  340. }
  341. if (webRequest.isHttpError || webRequest.isNetworkError)
  342. {
  343. CallBack(null,jsonString);
  344. }
  345. else
  346. {
  347. Debug.Log("CallBack==>" + webRequest.downloadHandler.data.Length);
  348. Texture2D d2d = new Texture2D(1, 1);
  349. d2d.LoadImage(webRequest.downloadHandler.data);
  350. CallBack(d2d, jsonString);
  351. }
  352. }
  353. }
  354. //上传图片
  355. public IEnumerator UploadFile(string path, string url, Action<string> CallBack)
  356. {
  357. // 读取要上传的文件
  358. byte[] bytes = File.ReadAllBytes(path);
  359. WWWForm formData = new WWWForm();
  360. formData.AddBinaryData("file", bytes);
  361. Debug.Log("DGJ ===> UploadFile URL " + url + " Path" + path +" Bytes "+ bytes.Length);
  362. // 创建UnityWebRequest对象
  363. UnityWebRequest request = UnityWebRequest.Post(url, formData);
  364. // 发送请求
  365. yield return request.SendWebRequest();
  366. // 处理响应数据
  367. if (request.isNetworkError || request.isHttpError)
  368. {
  369. Debug.Log(request.error);
  370. }
  371. else
  372. {
  373. CallBack?.Invoke(request.downloadHandler.text);
  374. Debug.Log("Upload complete!"+ request.downloadHandler.text);
  375. }
  376. }
  377. public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack, string ContentType = "application/json", bool isPost = true)
  378. {
  379. Debug.Log("Start Queue SendHttp " + methodName + " jsonstring==>" + jsonString);
  380. string url;
  381. url = "http://36.133.197.238:10002" + methodName;
  382. Debug.Log("URL:" + url + " isPost " + isPost);
  383. UnityWebRequest webRequest;
  384. if (!isPost)
  385. {
  386. JsonData data = JsonMapper.ToObject(jsonString);
  387. url += "?";
  388. foreach (var item in data.Keys)
  389. {
  390. url += item + "=" + data[item].ToString() + "&";
  391. }
  392. Debug.Log("URL:" + url + " isPost " + isPost);
  393. webRequest = UnityWebRequest.Get(url);
  394. }
  395. else
  396. {
  397. webRequest = new UnityWebRequest(url, "POST");
  398. }
  399. initHead(ContentType);
  400. using (webRequest)
  401. {
  402. if (!isPost)
  403. {
  404. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  405. foreach (var v in requestHeader)
  406. {
  407. webRequest.SetRequestHeader(v.Key, v.Value);
  408. }
  409. }
  410. else
  411. {
  412. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  413. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  414. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  415. }
  416. foreach (var v in requestHeader)
  417. {
  418. webRequest.SetRequestHeader(v.Key, v.Value);
  419. }
  420. yield return webRequest.SendWebRequest();
  421. Debug.Log("CallBack==>" + webRequest.downloadHandler.text);
  422. if (webRequest.isHttpError || webRequest.isNetworkError)
  423. {
  424. Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
  425. string error = webRequest.downloadHandler.text;
  426. JObject jObject = JObject.Parse(error);
  427. CallBack(error);
  428. }
  429. else
  430. {
  431. //Debug.Log(webRequest.downloadHandler.text);
  432. var mes = webRequest.downloadHandler.text;
  433. CallBack(mes);
  434. }
  435. }
  436. }
  437. }
  438. /*
  439. public RawImage img;
  440. public void login()
  441. {
  442. Record add = new Record();
  443. add.distributeId = "1732204405933334528";
  444. add.recordTargetType = "1";
  445. add.recordTarget = "";
  446. add.recordTargetId = "";
  447. add.inspItemId = "";
  448. add.inspItemName = "";
  449. add.recordImg = "";
  450. add.recordMemo = "";
  451. add.recordStatus = "";
  452. JsonData data = new JsonData();
  453. data["appCode"] = "METAVERSE";
  454. data["password"] = "13579Wp!#%&(";
  455. data["username"] = "lisongnan";
  456. StartCoroutine(SendHttp(anon_login, data.ToJson(), (string msg)=> {
  457. Debug.Log("anon_login===>" + msg);
  458. JsonData d = JsonMapper.ToObject(msg);
  459. token = d["data"]["token"]["accessToken"].ToString();
  460. JsonData data = new JsonData();
  461. data["distributeTarget"] = "机房A";
  462. StartCoroutine(SendHttp(task_getMyList, data.ToJson(), (string msg) => {
  463. Debug.Log("task_getMyList===>"+ msg);
  464. JsonData d2 = JsonMapper.ToObject(msg);
  465. JsonData data = new JsonData();
  466. data["inspId"] = d2["data"]["records"][0]["inspId"].ToString();
  467. string distributeId = d2["data"]["records"][0]["distributeId"].ToString();
  468. StartCoroutine(SendHttp(inspItem_getlist, data.ToJson(), (string msg) => {
  469. Debug.Log("inspItem_getlist===>" + msg);
  470. JsonData d2 = JsonMapper.ToObject(msg);
  471. JsonData data = new JsonData();
  472. data["id"] = distributeId;
  473. StartCoroutine(SendHttp(record_getRecord, data.ToJson(), (string msg) => {
  474. Debug.Log("record_getRecord===>" + msg);
  475. JsonData d2 = JsonMapper.ToObject(msg);
  476. StartCoroutine(UploadFile("D:\\Button.png", "http://36.133.197.238:10002" + file_upload, (string msg) => {
  477. Debug.Log("UploadFile===>" + msg);
  478. JsonData d2 = JsonMapper.ToObject(msg);
  479. JsonData data = new JsonData();
  480. data["fileName"] = d2["data"].ToString();
  481. StartCoroutine(DownloadImage(file_download, data.ToJson(), (Texture2D msg) => {
  482. img.texture = msg;
  483. }));
  484. }));
  485. }, "application/json", false));
  486. }));
  487. }));
  488. Debug.Log("login===>"+msg); })) ;
  489. }
  490. */