RoomFile.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net.Http;
  6. using LitJson;
  7. using Paroxe.PdfRenderer;
  8. using UnityEngine;
  9. using UnityEngine.Experimental.GlobalIllumination;
  10. using UnityEngine.UI;
  11. using UnityEngine.Video;
  12. using XRTool.Util;
  13. public class RoomFile : UnitySingleton<RoomFile>
  14. {
  15. public Button openFileListBtn;
  16. public GameObject fileListView;
  17. public Button clearAllBtn;
  18. public Button deleteFileBtn;
  19. public Button downLoadFileBtn;
  20. public Button returnBtn;
  21. public GameObject msgRemind;
  22. //public Text msgRemindText;
  23. //public Text showfileCountText;
  24. public Text fileNameText;
  25. public Texture2D defaulttex;
  26. public RawImage showFileTex;
  27. public RectTransform defaulttexrect;
  28. public RawImage showVideoTex;
  29. public VideoPlayer videoPlayer;
  30. public RectTransform videorect;
  31. public PDFViewer pdfview;
  32. public GameObject[] ShowFileViewList;
  33. public static int fileUpdataCount = 0;
  34. public Action<int> UpdataCountAction;
  35. private Action ClearAllfileAction;
  36. public static Action<FileConfig> ClickFileItemAction;
  37. public static Action<FileConfig> IfFilePrefabConfigListAction;//点击知道是否实例化的监听
  38. public static Action<FileConfig> ClickOnPrefabAction;
  39. public static Action<FileConfig> HideChooseKuangAction;
  40. public static Action<FileConfig> HidefileChooseAction;
  41. private FileConfig downLoadConfig;
  42. protected override void Awake()
  43. {
  44. base.Awake();
  45. WSHandler.Rtc.OnReceiveFile += ReceiveFile;
  46. UpdataCountAction += OnUpdataCount;
  47. WSHandler.Rtc.OnClearFile += ClearFile;
  48. WSHandler.Rtc.OnCoordinate += Coordinate;
  49. WSHandler.Rtc.OnGetuploadCert += GetuploadCert;
  50. WSHandler.Rtc.OnSendFile += SendFileSucess;
  51. ClickFileItemAction += ClickFileItme;
  52. openFileListBtn.onClick.AddListener(ClickOnOpenFileListBtn);
  53. clearAllBtn.onClick.AddListener(ClickOnClearAllBtn);
  54. returnBtn.onClick.AddListener(ClickOnReturnBtn);
  55. ClearAllfileAction += ClearAllfile;
  56. WSHandler.Rtc.OnClearFileSucess += ClearFileSucess;
  57. downLoadFileBtn.onClick.AddListener(DownLoadFile);
  58. }
  59. private void ClickOnReturnBtn()
  60. {
  61. fileListView.gameObject.SetActive(false);
  62. }
  63. private void SendFileSucess(JsonData data)
  64. {
  65. FileConfig fileConfig = new FileConfig();
  66. fileConfig.PeerId = data["data"]["data"]["peerId"].ToString();
  67. fileConfig.From = data["data"]["data"]["from"].ToString();
  68. fileConfig.CreateTime = data["data"]["data"]["createTime"].ToString();
  69. fileConfig.Url = data["data"]["data"]["url"].ToString();
  70. fileConfig.Path = data["data"]["data"]["path"].ToString();
  71. fileConfig.FileName = data["data"]["data"]["fileName"].ToString();
  72. InstantFileConfig(fileConfig);
  73. }
  74. private void GetuploadCert(JsonData data)
  75. {
  76. if (data["data"]["code"].ToString() == "200")
  77. {
  78. //string path = "C:/Users/peitengfei/Desktop/tupian_1.png";
  79. string cert = data["data"]["data"]["cert"].ToString();
  80. if (test.path != "")
  81. {
  82. NetWorkHeaders.Instance.Upload(cert, test.path, UploadResult);
  83. }
  84. if (SaveShot.bytes != null)
  85. {
  86. NetWorkHeaders.Instance.Upload(cert, SaveShot.bytes, UploadResult);
  87. }
  88. }
  89. }
  90. private void UploadResult(string result)
  91. {
  92. Debug.Log("上传" + result);
  93. JsonData data = null;
  94. string code = "";
  95. try
  96. {
  97. data = JsonMapper.ToObject(result);
  98. code = data["code"].ToString();
  99. }
  100. catch (Exception e)
  101. {
  102. Debug.LogError(e.Data);
  103. return;
  104. }
  105. switch (code)
  106. {
  107. case "200":
  108. string url = data["data"]["url"].ToString();
  109. string path = data["data"]["path"].ToString();
  110. string fileName = data["data"]["fileName"].ToString();
  111. WSHandler.Rtc.sendFile(url, path, fileName);
  112. break;
  113. case "6007":
  114. TipMgr.Instance.ShowTip("文件不合法");
  115. break;
  116. }
  117. }
  118. private void OnUpdataCount(int num)
  119. {
  120. msgRemind.SetActive(true);
  121. //msgRemindText.text = num.ToString();
  122. }
  123. private void ReceiveFile(JsonData data)
  124. {
  125. FileConfig fileConfig = new FileConfig();
  126. fileConfig.PeerId = data["data"]["peerId"].ToString();
  127. fileConfig.From = data["data"]["from"].ToString();
  128. fileConfig.CreateTime = data["data"]["createTime"].ToString();
  129. fileConfig.Url = data["data"]["url"].ToString();
  130. fileConfig.Path = data["data"]["path"].ToString();
  131. fileConfig.FileName = data["data"]["fileName"].ToString();
  132. InstantFileConfig(fileConfig);
  133. }
  134. private void InstantFileConfig(FileConfig fileConfig)
  135. {
  136. if (FileList.Instance)
  137. {
  138. FileList.Instance.AddItem(fileConfig);
  139. }
  140. }
  141. public void Init()
  142. {
  143. fileListView.gameObject.SetActive(false);
  144. if (FileList.Instance)
  145. {
  146. fileUpdataCount = 0;
  147. msgRemind.SetActive(false);
  148. //Debug.Log(RemoteRtc.Instance.fileConfiglist + "BBB");
  149. FileList.Instance.Init(RemoteRtc.Instance.fileConfiglist);
  150. }
  151. HideFile();
  152. fileNameText.gameObject.SetActive(false);
  153. clearAllBtn.gameObject.SetActive(false);
  154. deleteFileBtn.gameObject.SetActive(false);
  155. downLoadFileBtn.gameObject.SetActive(false);
  156. }
  157. public void OpenInit()
  158. {
  159. fileUpdataCount = 0;
  160. msgRemind.SetActive(false);
  161. }
  162. public void ShowClearBtn()
  163. {
  164. clearAllBtn.gameObject.SetActive(true);
  165. }
  166. public void HideClearBtn()
  167. {
  168. clearAllBtn.gameObject.SetActive(false);
  169. }
  170. private void ClickOnOpenFileListBtn()
  171. {
  172. fileListView.gameObject.SetActive(true);
  173. OpenInit();
  174. }
  175. private void ClickOnClearAllBtn()
  176. {
  177. // PopForms.Instance.ShowPublic(PopType.PopTwo, RtcStrConfig.clearallFile, "确定", ClearAllfileAction);
  178. ClearAllfile();
  179. deleteFileBtn.gameObject.SetActive(false);
  180. fileNameText.text = "";
  181. downLoadFileBtn.gameObject.SetActive(false);
  182. }
  183. private void ClearAllfile()
  184. {
  185. HideClearBtn();
  186. WSHandler.Rtc.clearFile();
  187. }
  188. private void ClearFileSucess(JsonData data)
  189. {
  190. if (data["data"]["code"].ToString() == "200")
  191. {
  192. ClearAllFileItem();
  193. }
  194. }
  195. private void ClearFile(JsonData data)
  196. {
  197. if (data["method"].ToString() == "clearFile")
  198. {
  199. ClearAllFileItem();
  200. }
  201. }
  202. public void ClearAllFileItem()
  203. {
  204. if (RemoteRtc.Instance && RemoteRtc.Instance.fileConfiglist != null && RemoteRtc.Instance.fileConfiglist.Count > 0)
  205. {
  206. RemoteRtc.Instance.fileConfiglist.Clear();
  207. }
  208. if (FileList.Instance)
  209. {
  210. msgRemind.SetActive(false);
  211. fileUpdataCount = 0;
  212. FileList.Instance.DestoryAll();
  213. }
  214. ClearAllFilePrefab();
  215. }
  216. public void ClearAllFilePrefab()
  217. {
  218. HideFile();
  219. }
  220. private void ClickFileItme(FileConfig fileConfig)
  221. {
  222. if (fileConfig != null)
  223. {
  224. fileNameText.gameObject.SetActive(true);
  225. fileNameText.text = fileConfig.FileName;
  226. if (UserInfo.activateType == 2 || UserInfo.activateType == 3)
  227. {
  228. downLoadFileBtn.gameObject.SetActive(true);
  229. //downLoadFileBtn.onClick.RemoveAllListeners();
  230. //downLoadFileBtn.onClick.AddListener()
  231. //
  232. }
  233. if (RoomMainInfo.isCreator == "0")
  234. {
  235. deleteFileBtn.gameObject.SetActive(true);
  236. deleteFileBtn.onClick.RemoveAllListeners();
  237. deleteFileBtn.onClick.AddListener(() => ClickOnDeleteBtn(fileConfig.Path));
  238. }
  239. switch (fileConfig.FileType)
  240. {
  241. case RoomFileType.png:
  242. ShowFile(0);
  243. ShowImage(fileConfig);
  244. break;
  245. case RoomFileType.jpg:
  246. ShowFile(0);
  247. ShowImage(fileConfig);
  248. break;
  249. case RoomFileType.jpeg:
  250. ShowFile(0);
  251. ShowImage(fileConfig);
  252. break;
  253. case RoomFileType.mp4:
  254. ShowFile(1);
  255. ShowMp4(fileConfig);
  256. break;
  257. case RoomFileType.pdf:
  258. ShowFile(2);
  259. ShowPdf(fileConfig);
  260. break;
  261. case RoomFileType.zip:
  262. ShowFile(3);
  263. break;
  264. }
  265. downLoadConfig = fileConfig;
  266. }
  267. }
  268. public void ShowFile(int index)
  269. {
  270. for (int i = 0; i < ShowFileViewList.Length; i++)
  271. {
  272. if (index == i)
  273. {
  274. if (i == 0)
  275. {
  276. showFileTex.texture = defaulttex;
  277. }
  278. ShowFileViewList[i].SetActive(true);
  279. }
  280. else
  281. {
  282. ShowFileViewList[i].SetActive(false);
  283. }
  284. }
  285. }
  286. public void HideFile()
  287. {
  288. for (int i = 0; i < ShowFileViewList.Length; i++)
  289. {
  290. ShowFileViewList[i].SetActive(false);
  291. }
  292. showFileTex.texture = defaulttex;
  293. }
  294. private void ClickOnDeleteBtn(string path)
  295. {
  296. if (!string.IsNullOrEmpty(path))
  297. {
  298. deleteFileBtn.gameObject.SetActive(false);
  299. WSHandler.Rtc.deleteFile(path);
  300. }
  301. }
  302. public void DeleteSucess(FileConfig fileConfig)
  303. {
  304. HideFile();
  305. switch (fileConfig.FileType)
  306. {
  307. case RoomFileType.zip:
  308. break;
  309. }
  310. }
  311. private void Coordinate(JsonData data)
  312. {
  313. string action = data["data"]["action"].ToString();
  314. switch (action)
  315. {
  316. case "10001":
  317. if (RemoteRtc.Instance)
  318. {
  319. float Valuex = float.Parse(data["data"]["params"]["x"].ToString());
  320. float Valuey = float.Parse(data["data"]["params"]["y"].ToString());
  321. RoomMainForms.Instance.webTex.autoFocusPoint = new Vector2(Valuex, 1 - Valuey);
  322. //TextMesh textMesh = obj.transform.Find("Text").GetComponent<TextMesh>();
  323. //textMesh.text = "**" + RemoteRtc.Instance.webTex.autoFocusPoint;
  324. }
  325. break;
  326. }
  327. }
  328. public void ShowImage(FileConfig fileConfig)
  329. {
  330. if (!string.IsNullOrEmpty(fileConfig.Url))
  331. {
  332. string url = fileConfig.Url;
  333. if (!fileConfig.Url.Contains("http"))
  334. {
  335. url = "https://" + fileConfig.Url;
  336. }
  337. NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) => {
  338. if (tex)
  339. {
  340. Adaption(tex, defaulttexrect);
  341. showFileTex.texture = tex;
  342. }
  343. });
  344. }
  345. }
  346. public void ShowMp4(FileConfig fileConfig)
  347. {
  348. if (!string.IsNullOrEmpty(fileConfig.Url))
  349. {
  350. string url = fileConfig.Url;
  351. if (!fileConfig.Url.Contains("http"))
  352. {
  353. url = "https://" + fileConfig.Url;
  354. }
  355. videoPlayer.url = url;
  356. StartCoroutine(enumerator(videoPlayer, videorect));
  357. }
  358. }
  359. public void ShowPdf(FileConfig fileConfig)
  360. {
  361. if (!string.IsNullOrEmpty(fileConfig.Url))
  362. {
  363. string url = fileConfig.Url;
  364. if (!fileConfig.Url.Contains("http"))
  365. {
  366. url = "https://" + fileConfig.Url;
  367. }
  368. pdfview.FileURL = url;
  369. pdfview.LoadDocument();
  370. }
  371. }
  372. private void Adaption(Texture tex, RectTransform rectTransform)
  373. {
  374. float standard_width = 1920f;
  375. float standard_height = 1080f;
  376. float video_width = tex.width;
  377. float video_height = tex.height;
  378. //Debug.Log(tex.width + "***" + tex.height);
  379. if (standard_width <= video_width && standard_height > video_height)
  380. {
  381. float video_aspect = standard_width / video_width;
  382. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  383. }
  384. else if (standard_width > video_width && standard_height <= video_height)
  385. {
  386. float video_aspect = standard_height / video_height;
  387. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  388. }
  389. else if (standard_width > video_width && standard_height > video_height)
  390. {
  391. if (standard_width / video_width > standard_height / video_height)
  392. {
  393. float video_aspect = standard_height / video_height;
  394. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  395. }
  396. else
  397. {
  398. float video_aspect = standard_width / video_width;
  399. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  400. }
  401. }
  402. else
  403. {
  404. if (standard_width / video_width > standard_height / video_height)
  405. {
  406. float video_aspect = standard_height / video_height;
  407. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  408. }
  409. else
  410. {
  411. float video_aspect = standard_width / video_width;
  412. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  413. }
  414. }
  415. }
  416. private IEnumerator enumerator(VideoPlayer videoPlayer, RectTransform rectTransform)
  417. {
  418. while (!videoPlayer.isPrepared)
  419. {
  420. yield return null;
  421. }
  422. float standard_width = 1920f;
  423. float standard_height = 1080f;
  424. float video_width = videoPlayer.texture.width;
  425. float video_height = videoPlayer.texture.height;
  426. //Debug.Log(video_width + "***" + video_height);
  427. if (standard_width <= video_width && standard_height > video_height)
  428. {
  429. float video_aspect = standard_width / video_width;
  430. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  431. }
  432. else if (standard_width > video_width && standard_height <= video_height)
  433. {
  434. float video_aspect = standard_height / video_height;
  435. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  436. }
  437. else if (standard_width > video_width && standard_height > video_height)
  438. {
  439. if (standard_width / video_width > standard_height / video_height)
  440. {
  441. float video_aspect = standard_height / video_height;
  442. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  443. }
  444. else
  445. {
  446. float video_aspect = standard_width / video_width;
  447. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  448. }
  449. }
  450. else
  451. {
  452. if (standard_width / video_width > standard_height / video_height)
  453. {
  454. float video_aspect = standard_height / video_height;
  455. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  456. //Debug.Log("BBB" + video_aspect + "///" + video_width * video_aspect + "***" + video_height * video_aspect);
  457. }
  458. else
  459. {
  460. float video_aspect = standard_width / video_width;
  461. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  462. //Debug.Log("CCC" + video_aspect + "///" + video_width * video_aspect + "***" + video_height * video_aspect);
  463. }
  464. }
  465. }
  466. protected override void OnDestroy()
  467. {
  468. ClickFileItemAction -= ClickFileItme;
  469. WSHandler.Rtc.OnReceiveFile -= ReceiveFile;
  470. UpdataCountAction -= OnUpdataCount;
  471. ClearAllfileAction -= ClearAllfile;
  472. WSHandler.Rtc.OnClearFileSucess -= ClearFileSucess;
  473. WSHandler.Rtc.OnClearFile -= ClearFile;
  474. WSHandler.Rtc.OnCoordinate -= Coordinate;
  475. WSHandler.Rtc.OnGetuploadCert -= GetuploadCert;
  476. WSHandler.Rtc.OnSendFile -= SendFileSucess;
  477. }
  478. private void DownLoadFile()
  479. {
  480. if (downLoadConfig == null)
  481. return;
  482. string downloadUrl = "https://" + downLoadConfig.Url;
  483. // string fileName = Application.persistentDataPath + "/DownLoad/" + fileConfig.FileName;
  484. StartCoroutine(DownLoadFileCorutine(downloadUrl, downLoadConfig.FileName));
  485. }
  486. private IEnumerator DownLoadFileCorutine(string downloadUrl, string fileName)
  487. {
  488. string filePath = GetFilePath(fileName);
  489. filePath = "/storage/emulated/0/DownLoad/" + fileName;
  490. Debug.Log("DownLoadFileCorutine " + filePath);
  491. Debug.Log("Application.persistentDataPath " + Application.persistentDataPath);
  492. using (WWW www = new WWW(downloadUrl))
  493. {
  494. yield return www;
  495. if (string.IsNullOrEmpty(www.error))
  496. {
  497. // 下载成功,保存文件到本地
  498. File.WriteAllBytes(filePath, www.bytes);
  499. Debug.Log("File downloaded to: " + filePath);
  500. }
  501. else
  502. {
  503. Debug.Log("Download failed: " + www.error);
  504. }
  505. }
  506. }
  507. private string GetFilePath(string fileName)
  508. {
  509. string downloadPath = Path.Combine(Application.persistentDataPath, "DownLoad");
  510. // 检测文件夹是否存在
  511. if (!Directory.Exists(downloadPath))
  512. {
  513. // 文件夹不存在,创建一个新的文件夹
  514. Directory.CreateDirectory(downloadPath);
  515. }
  516. string filePath = Path.Combine(downloadPath, fileName);
  517. return filePath;
  518. }
  519. }