123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using static LangChaoJiekou;
- using UnityEngine.UI;
- using System.IO;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- public class XunJianDetails : MonoBehaviour
- {
- public TMP_Text xjName;
- public List<Image> listImages;
- public Button normalBtn;
- public Button WarningBtn;
- public Image normalBG1;
- public Image normalBG2;
- public Image warningBG1;
- public Image WarningBG2;
- public XunJianException exception;
- public Button lastBtn;
- public Button nextBtn;
- public Button completeBtn;
- public XunJianPhotos UI2;
- private Image showImage;
- private int showImageNum;
- public Sprite startImg;
- public Sprite loadImg;
- private RoomItem data;
- private RoomItem oldData;
- #region 巡检更新项
- public bool isUpdata;
- private List<string> listImageUrl; // 图片
- private string recordMemo; // 巡检备注
- private string recordStatus;// 1-正常 0-异常
- #endregion
- private void Start()
- {
- for (int i = 0; i < listImages.Count; i++)
- {
- listImages[i].GetComponent<Button>().onClick.AddListener(() =>
- {
- if (!UI2.gameObject.activeSelf)
- UI2.gameObject.SetActive(true);
- LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
- });
- }
- normalBtn.onClick.AddListener(() =>
- {
- exception.gameObject.SetActive(false);
- recordStatus = "1";
- isUpdata = true;
- normalBG1.gameObject.SetActive(true);
- normalBG2.gameObject.SetActive(false);
- warningBG1.gameObject.SetActive(false);
- WarningBG2.gameObject.SetActive(true);
- XunJianInitialize.Instance.checkTaskItem.errorObj.SetActive(false);
- LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
- });
- WarningBtn.onClick.AddListener(() =>
- {
- exception.gameObject.SetActive(true);
- recordStatus = "0";
- isUpdata = true;
- normalBG1.gameObject.SetActive(false);
- normalBG2.gameObject.SetActive(true);
- warningBG1.gameObject.SetActive(true);
- WarningBG2.gameObject.SetActive(false);
- XunJianInitialize.Instance.checkTaskItem.errorObj.SetActive(true);
- LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
- });
- lastBtn.onClick.AddListener(() =>
- {
- // 提交
- SubmitData();
- XunJianInitialize.Instance.LastItem();
- });
- nextBtn.onClick.AddListener(() =>
- {
- // 提交
- SubmitData();
- XunJianInitialize.Instance.NextItem();
- });
- completeBtn.onClick.AddListener(() =>
- {
- // 提交
- SubmitData();
- // 调用是否退出弹框
- XunJianInitialize.Instance.Complete();
- });
- }
- public void InitData(RoomItem data)
- {
- // SubmitData();
- // 提交
- this.data = data;
- this.oldData = data;
- listImageUrl = null;
- // 根据数据进行初始化
- InitUI();
- if (data.recd != null) // 之前提交过
- {
- // 同步照片
- if (data.recd.recordImgList != null)
- {
- // Json 转换
- // List<string> listurl = data.recd.recordImgList;
- for (int i = 0; i < data.recd.recordImgList.Count; i++)
- {
- if (!string.IsNullOrEmpty(data.recd.recordImgList[i]) && data.recd.recordImgList[i] != "null")
- {
- Debug.Log(" DGJ ===> recordImgList " + data.recd.recordImgList[i]);
- JsonData data2 = new JsonData();
- data2["fileName"] = data.recd.recordImgList[i];
- StartCoroutine(LangChaoJiekou.Instance.DownloadImage(LangChaoJiekou.file_download, data2.ToJson(), (texture, url) =>
- {
- Debug.Log("DGJ ===> DownLoadImage " + url);
- JObject obj = JObject.Parse(url);
- url = obj["fileName"].ToString();
- if (texture != null)
- {
- for (int i = 0; i < data.recd.recordImgList.Count; i++)
- {
- Debug.Log("DGJ ===> DownLoadImage2 " + url + " " + data.recd.recordImgList[i]);
- if (data.recd.recordImgList[i] == url)
- {
- Debug.Log("DGJ ===> DownLoadImage3 " + url);
- listImages[i].sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
- break;
- }
- }
- }
- }));
- }
- }
- }
- switch (data.recd.recordStatus)
- {
- case "0":
- normalBG1.gameObject.SetActive(false);
- normalBG2.gameObject.SetActive(true);
- warningBG1.gameObject.SetActive(true);
- WarningBG2.gameObject.SetActive(false);
- break;
- case "1":
- normalBG1.gameObject.SetActive(true);
- normalBG2.gameObject.SetActive(false);
- warningBG1.gameObject.SetActive(false);
- WarningBG2.gameObject.SetActive(true);
- break;
- default:
- break;
- }
- // 同步异常
- exception.gameObject.SetActive(data.recd.recordStatus == "0");
- //if (data.recd.recordImg != null)
- // listImageUrl = JsonConvert.DeserializeObject<List<string>>(data.recd.recordImg);
- listImageUrl = new List<string>();
- for (int i = 0; i < data.recd.recordImgList.Count; i++)
- {
- listImageUrl.Add(data.recd.recordImgList[i]);
- }
- recordStatus = data.recd.recordStatus;
- recordMemo = data.recd.recordMemo;
- exception.scInput.text = data.recd.recordMemo;
- }
- else
- {
- normalBG1.gameObject.SetActive(false);
- normalBG2.gameObject.SetActive(true);
- warningBG1.gameObject.SetActive(false);
- WarningBG2.gameObject.SetActive(true);
- listImageUrl = new List<string>();
- recordStatus = null;
- recordMemo = null;
- exception.scInput.text = null;
- }
- LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
- }
- private void InitUI()
- {
- xjName.text = XunJianInitialize.Instance.GetCheckItemNum().ToString() + "." + data.inspItemDesc;
- for (int i = 0; i < listImages.Count; i++)
- {
- listImages[i].sprite = startImg;
- }
- exception.InitData(data);
- exception.gameObject.SetActive(false);
- lastBtn.gameObject.SetActive(true);
- nextBtn.gameObject.SetActive(true);
- completeBtn.gameObject.SetActive(false);
- // 如果是第一步 没有上一步按钮
- if (data == XunJianAllData.listRoomItem[0])
- {
- lastBtn.gameObject.SetActive(false);
- }
- // 如果是最后一步 下一步按钮 替换未完成按钮
- if (data == XunJianAllData.listRoomItem[XunJianAllData.listRoomItem.Count - 1])
- {
- nextBtn.gameObject.SetActive(false);
- completeBtn.gameObject.SetActive(true);
- }
- }
- /// <summary>
- /// 提交数据
- /// </summary>
- public void SubmitData()
- {
- Debug.Log("DGJ ===> SubmitData " + isUpdata);
- if (isUpdata)
- {
- isUpdata = false;
- recordMemo = exception.scInput.text;
- XunJianInitialize.Instance.checkTaskItem.SubmitData(listImageUrl, recordStatus, recordMemo);
- }
- }
- public void SetPhotosShowImage(Image image)
- {
- this.showImage = image;
- }
- public void SetPhotosNum(int i)
- {
- this.showImageNum = i;
- }
- public void SetShowImage(Texture2D texture2D)
- {
- if (showImage == null)
- return;
- isUpdata = true;
- string fileName = SaveLocal(texture2D);
- // 将截图应用到Image组件上
- showImage.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
- StartCoroutine(LangChaoJiekou.Instance.UploadFile(fileName, "http://36.133.197.238:10002" + file_upload, (string msg) =>
- {
- Debug.Log("UploadFile===>" + msg);
- JsonData data = JsonMapper.ToObject(msg);
- if (listImageUrl == null || listImageUrl.Count != listImages.Count)
- {
- listImageUrl = new List<string>();
- for (int i = 0; i < listImages.Count; i++)
- {
- listImageUrl.Add(null);
- }
- }
- listImageUrl[showImageNum] = data["data"].ToString();
- if (data["code"].ToString() == "200")
- {
- }
- }));
- }
- /// <summary>
- /// 保存到本地
- /// </summary>
- /// <param name="rawImage"></param>
- /// <param name="Step">某个步骤</param>
- /// <param name="id">某个步骤的第几张图片</param>
- private string SaveLocal(Texture2D texture2D)
- {
- byte[] bytes = texture2D.EncodeToPNG();
- if (!Directory.Exists(Application.persistentDataPath + "/Image"))
- Directory.CreateDirectory(Application.persistentDataPath + "/Image");
- string filename = Application.persistentDataPath + "/Image" + "/" + data.inspItemName + showImage.name + ".png";
- if (File.Exists(filename))
- File.Delete(filename);
- // Debug.LogError(filename);
- File.WriteAllBytes(filename, bytes);
- return filename;
- }
- /// <summary>
- /// 运行模式下Texture转换成Texture2D
- /// </summary>
- /// <param name="texture"></param>
- /// <returns></returns>
- public Texture2D TextureToTexture2D(Texture texture)
- {
- Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
- RenderTexture currentRT = RenderTexture.active;
- RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
- Graphics.Blit(texture, renderTexture);
- RenderTexture.active = renderTexture;
- texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
- texture2D.Apply();
- RenderTexture.active = currentRT;
- RenderTexture.ReleaseTemporary(renderTexture);
- return texture2D;
- }
- }
|