XunJianDetails.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using static LangChaoJiekou;
  6. using UnityEngine.UI;
  7. using System.IO;
  8. using LitJson;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. public class XunJianDetails : MonoBehaviour
  12. {
  13. public TMP_Text xjName;
  14. public List<Image> listImages;
  15. public Button normalBtn;
  16. public Button WarningBtn;
  17. public Image normalBG1;
  18. public Image normalBG2;
  19. public Image warningBG1;
  20. public Image WarningBG2;
  21. public XunJianException exception;
  22. public Button lastBtn;
  23. public Button nextBtn;
  24. public Button completeBtn;
  25. public XunJianPhotos UI2;
  26. private Image showImage;
  27. private int showImageNum;
  28. public Sprite startImg;
  29. public Sprite loadImg;
  30. private RoomItem data;
  31. private RoomItem oldData;
  32. #region 巡检更新项
  33. public bool isUpdata;
  34. private List<string> listImageUrl; // 图片
  35. private string recordMemo; // 巡检备注
  36. private string recordStatus;// 1-正常 0-异常
  37. #endregion
  38. private void Start()
  39. {
  40. for (int i = 0; i < listImages.Count; i++)
  41. {
  42. listImages[i].GetComponent<Button>().onClick.AddListener(() =>
  43. {
  44. if (!UI2.gameObject.activeSelf)
  45. UI2.gameObject.SetActive(true);
  46. LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
  47. });
  48. }
  49. normalBtn.onClick.AddListener(() =>
  50. {
  51. exception.gameObject.SetActive(false);
  52. recordStatus = "1";
  53. isUpdata = true;
  54. normalBG1.gameObject.SetActive(true);
  55. normalBG2.gameObject.SetActive(false);
  56. warningBG1.gameObject.SetActive(false);
  57. WarningBG2.gameObject.SetActive(true);
  58. XunJianInitialize.Instance.checkTaskItem.errorObj.SetActive(false);
  59. LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
  60. });
  61. WarningBtn.onClick.AddListener(() =>
  62. {
  63. exception.gameObject.SetActive(true);
  64. recordStatus = "0";
  65. isUpdata = true;
  66. normalBG1.gameObject.SetActive(false);
  67. normalBG2.gameObject.SetActive(true);
  68. warningBG1.gameObject.SetActive(true);
  69. WarningBG2.gameObject.SetActive(false);
  70. XunJianInitialize.Instance.checkTaskItem.errorObj.SetActive(true);
  71. LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
  72. });
  73. lastBtn.onClick.AddListener(() =>
  74. {
  75. // 提交
  76. SubmitData();
  77. XunJianInitialize.Instance.LastItem();
  78. });
  79. nextBtn.onClick.AddListener(() =>
  80. {
  81. // 提交
  82. SubmitData();
  83. XunJianInitialize.Instance.NextItem();
  84. });
  85. completeBtn.onClick.AddListener(() =>
  86. {
  87. // 提交
  88. SubmitData();
  89. // 调用是否退出弹框
  90. XunJianInitialize.Instance.Complete();
  91. });
  92. }
  93. public void InitData(RoomItem data)
  94. {
  95. // SubmitData();
  96. // 提交
  97. this.data = data;
  98. this.oldData = data;
  99. listImageUrl = null;
  100. // 根据数据进行初始化
  101. InitUI();
  102. if (data.recd != null) // 之前提交过
  103. {
  104. // 同步照片
  105. if (data.recd.recordImgList != null)
  106. {
  107. // Json 转换
  108. // List<string> listurl = data.recd.recordImgList;
  109. for (int i = 0; i < data.recd.recordImgList.Count; i++)
  110. {
  111. if (!string.IsNullOrEmpty(data.recd.recordImgList[i]) && data.recd.recordImgList[i] != "null")
  112. {
  113. Debug.Log(" DGJ ===> recordImgList " + data.recd.recordImgList[i]);
  114. JsonData data2 = new JsonData();
  115. data2["fileName"] = data.recd.recordImgList[i];
  116. StartCoroutine(LangChaoJiekou.Instance.DownloadImage(LangChaoJiekou.file_download, data2.ToJson(), (texture, url) =>
  117. {
  118. Debug.Log("DGJ ===> DownLoadImage " + url);
  119. JObject obj = JObject.Parse(url);
  120. url = obj["fileName"].ToString();
  121. if (texture != null)
  122. {
  123. for (int i = 0; i < data.recd.recordImgList.Count; i++)
  124. {
  125. Debug.Log("DGJ ===> DownLoadImage2 " + url + " " + data.recd.recordImgList[i]);
  126. if (data.recd.recordImgList[i] == url)
  127. {
  128. Debug.Log("DGJ ===> DownLoadImage3 " + url);
  129. listImages[i].sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  130. break;
  131. }
  132. }
  133. }
  134. }));
  135. }
  136. }
  137. }
  138. switch (data.recd.recordStatus)
  139. {
  140. case "0":
  141. normalBG1.gameObject.SetActive(false);
  142. normalBG2.gameObject.SetActive(true);
  143. warningBG1.gameObject.SetActive(true);
  144. WarningBG2.gameObject.SetActive(false);
  145. break;
  146. case "1":
  147. normalBG1.gameObject.SetActive(true);
  148. normalBG2.gameObject.SetActive(false);
  149. warningBG1.gameObject.SetActive(false);
  150. WarningBG2.gameObject.SetActive(true);
  151. break;
  152. default:
  153. break;
  154. }
  155. // 同步异常
  156. exception.gameObject.SetActive(data.recd.recordStatus == "0");
  157. //if (data.recd.recordImg != null)
  158. // listImageUrl = JsonConvert.DeserializeObject<List<string>>(data.recd.recordImg);
  159. listImageUrl = new List<string>();
  160. for (int i = 0; i < data.recd.recordImgList.Count; i++)
  161. {
  162. listImageUrl.Add(data.recd.recordImgList[i]);
  163. }
  164. recordStatus = data.recd.recordStatus;
  165. recordMemo = data.recd.recordMemo;
  166. exception.scInput.text = data.recd.recordMemo;
  167. }
  168. else
  169. {
  170. normalBG1.gameObject.SetActive(false);
  171. normalBG2.gameObject.SetActive(true);
  172. warningBG1.gameObject.SetActive(false);
  173. WarningBG2.gameObject.SetActive(true);
  174. listImageUrl = new List<string>();
  175. recordStatus = null;
  176. recordMemo = null;
  177. exception.scInput.text = null;
  178. }
  179. LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
  180. }
  181. private void InitUI()
  182. {
  183. xjName.text = XunJianInitialize.Instance.GetCheckItemNum().ToString() + "." + data.inspItemDesc;
  184. for (int i = 0; i < listImages.Count; i++)
  185. {
  186. listImages[i].sprite = startImg;
  187. }
  188. exception.InitData(data);
  189. exception.gameObject.SetActive(false);
  190. lastBtn.gameObject.SetActive(true);
  191. nextBtn.gameObject.SetActive(true);
  192. completeBtn.gameObject.SetActive(false);
  193. // 如果是第一步 没有上一步按钮
  194. if (data == XunJianAllData.listRoomItem[0])
  195. {
  196. lastBtn.gameObject.SetActive(false);
  197. }
  198. // 如果是最后一步 下一步按钮 替换未完成按钮
  199. if (data == XunJianAllData.listRoomItem[XunJianAllData.listRoomItem.Count - 1])
  200. {
  201. nextBtn.gameObject.SetActive(false);
  202. completeBtn.gameObject.SetActive(true);
  203. }
  204. }
  205. /// <summary>
  206. /// 提交数据
  207. /// </summary>
  208. public void SubmitData()
  209. {
  210. Debug.Log("DGJ ===> SubmitData " + isUpdata);
  211. if (isUpdata)
  212. {
  213. isUpdata = false;
  214. recordMemo = exception.scInput.text;
  215. XunJianInitialize.Instance.checkTaskItem.SubmitData(listImageUrl, recordStatus, recordMemo);
  216. }
  217. }
  218. public void SetPhotosShowImage(Image image)
  219. {
  220. this.showImage = image;
  221. }
  222. public void SetPhotosNum(int i)
  223. {
  224. this.showImageNum = i;
  225. }
  226. public void SetShowImage(Texture2D texture2D)
  227. {
  228. if (showImage == null)
  229. return;
  230. isUpdata = true;
  231. string fileName = SaveLocal(texture2D);
  232. // 将截图应用到Image组件上
  233. showImage.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
  234. StartCoroutine(LangChaoJiekou.Instance.UploadFile(fileName, "http://36.133.197.238:10002" + file_upload, (string msg) =>
  235. {
  236. Debug.Log("UploadFile===>" + msg);
  237. JsonData data = JsonMapper.ToObject(msg);
  238. if (listImageUrl == null || listImageUrl.Count != listImages.Count)
  239. {
  240. listImageUrl = new List<string>();
  241. for (int i = 0; i < listImages.Count; i++)
  242. {
  243. listImageUrl.Add(null);
  244. }
  245. }
  246. listImageUrl[showImageNum] = data["data"].ToString();
  247. if (data["code"].ToString() == "200")
  248. {
  249. }
  250. }));
  251. }
  252. /// <summary>
  253. /// 保存到本地
  254. /// </summary>
  255. /// <param name="rawImage"></param>
  256. /// <param name="Step">某个步骤</param>
  257. /// <param name="id">某个步骤的第几张图片</param>
  258. private string SaveLocal(Texture2D texture2D)
  259. {
  260. byte[] bytes = texture2D.EncodeToPNG();
  261. if (!Directory.Exists(Application.persistentDataPath + "/Image"))
  262. Directory.CreateDirectory(Application.persistentDataPath + "/Image");
  263. string filename = Application.persistentDataPath + "/Image" + "/" + data.inspItemName + showImage.name + ".png";
  264. if (File.Exists(filename))
  265. File.Delete(filename);
  266. // Debug.LogError(filename);
  267. File.WriteAllBytes(filename, bytes);
  268. return filename;
  269. }
  270. /// <summary>
  271. /// 运行模式下Texture转换成Texture2D
  272. /// </summary>
  273. /// <param name="texture"></param>
  274. /// <returns></returns>
  275. public Texture2D TextureToTexture2D(Texture texture)
  276. {
  277. Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
  278. RenderTexture currentRT = RenderTexture.active;
  279. RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
  280. Graphics.Blit(texture, renderTexture);
  281. RenderTexture.active = renderTexture;
  282. texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  283. texture2D.Apply();
  284. RenderTexture.active = currentRT;
  285. RenderTexture.ReleaseTemporary(renderTexture);
  286. return texture2D;
  287. }
  288. }