No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

休假申請.cs 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using TMPro;
  10. using System;
  11. using System.IO;
  12. using ZXing;
  13. using ZXing.QrCode;
  14. using System.Net;
  15. using SimpleJSON;
  16. using System.ComponentModel;
  17. using System.Runtime.InteropServices;
  18. public class 休假申請 : MonoBehaviour
  19. {
  20. [SerializeField] Button 存檔紐;
  21. [SerializeField] Button 刪除紐;
  22. [SerializeField] Button 切換紐;
  23. [SerializeField] Button 回首紐;
  24. [SerializeField] RawImage 縮圖;
  25. [SerializeField] RawImage 拍照用;
  26. [SerializeField] RawImage 放圖用;
  27. [SerializeField] GameObject 主頁;
  28. [SerializeField] GameObject 圖頁;
  29. [SerializeField] TextMeshProUGUI 提示字;
  30. private WebCamTexture myCam;//接收攝影機返回的圖片數據
  31. public GameObject buttonPrefab;
  32. public Transform buttonParent;
  33. public GameObject buttonceneter;
  34. private List<GameObject> copy = new List<GameObject>();
  35. public GameObject buttonPrefab1;
  36. public Transform buttonParent1;
  37. public GameObject buttonceneter1;
  38. private List<GameObject> copy1 = new List<GameObject>();
  39. public TMP_Dropdown 年;
  40. public TMP_Dropdown 月;
  41. public TMP_Dropdown 假別;
  42. public TMP_Dropdown 時數;
  43. [SerializeField] TextMeshProUGUI 日期;
  44. [SerializeField] TextMeshProUGUI 申請人;
  45. [SerializeField] TextMeshProUGUI 編號;
  46. [SerializeField] TMP_InputField 事由;
  47. public CanvasScaler canvasScaler;
  48. [SerializeField] GameObject 人員清單面板;
  49. [SerializeField] GameObject 人員清單按鈕;
  50. [SerializeField] GameObject 讀取面板;
  51. [SerializeField] TextMeshProUGUI 讀取;
  52. [SerializeField] GameObject 彈跳面板;
  53. [SerializeField] TextMeshProUGUI 彈跳文字;
  54. private string loadingText = "";
  55. private int dotCount = 0;
  56. string 生效;
  57. string 圖片資料庫;
  58. bool 是否有圖;
  59. string 選擇資料庫;
  60. string 選擇流水號;
  61. void Start()
  62. {
  63. Screen.autorotateToPortrait = false;
  64. Screen.orientation = ScreenOrientation.Portrait;
  65. Main.Global.螢幕方向="直";
  66. //Main.Global.固高=1900f;
  67. //Main.Global.固寬=900f;
  68. //Main.Global.固比=1f;
  69. //Main.Global.螢幕格式="瘦";
  70. //Main.Global.考勤主管權限=false;
  71. //Main.Global.PA="盧婕柔";
  72. //Main.Global.編號="999";
  73. canvasScaler.referenceResolution = new Vector2(Main.Global.固寬, Main.Global.固高);
  74. canvasScaler.matchWidthOrHeight=Main.Global.固比;
  75. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/hskin_login.php?";
  76. //Main.Global.預設伺服器路徑="test";
  77. 存檔紐.interactable = false;
  78. 刪除紐.interactable = false;
  79. 切換紐.interactable = false;
  80. 回首紐.interactable = true;
  81. 主頁.SetActive(true);
  82. 圖頁.SetActive(false);
  83. if (Main.Global.考勤主管權限==true)
  84. {
  85. 人員清單按鈕.SetActive(true);
  86. }
  87. else
  88. {
  89. 人員清單按鈕.SetActive(false);
  90. }
  91. StartCoroutine(AnimateText());
  92. StartCoroutine(圖片資料庫選擇());
  93. 是否有圖=false;
  94. }
  95. IEnumerator AnimateText()
  96. {
  97. string qqq = 讀取.text.ToString();
  98. while (true)
  99. {
  100. yield return new WaitForSeconds(0.5f);
  101. if (dotCount < 3)
  102. {
  103. loadingText += ".";
  104. dotCount++;
  105. }
  106. else
  107. {
  108. loadingText = "";
  109. dotCount = 0;
  110. }
  111. 讀取.text = qqq+loadingText;
  112. }
  113. }
  114. IEnumerator 圖片資料庫選擇()
  115. {
  116. 讀取面板.SetActive(true);
  117. string strcon = "圖片資料庫選擇";
  118. string se = Main.Global.預設伺服器路徑;
  119. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}", strcon, se);
  120. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  121. yield return request.SendWebRequest();
  122. if (request.result == UnityWebRequest.Result.ConnectionError)
  123. {
  124. //Debug.Log(request.error);
  125. yield break;
  126. }
  127. string str = request.downloadHandler.text.ToString();
  128. if(str == "n")
  129. {
  130. 讀取面板.SetActive(false);
  131. 彈跳面板.SetActive(true);
  132. 彈跳文字.text = "圖片資料庫已滿,請找系統管理員!!";
  133. yield return new WaitForSeconds(2);
  134. change_Secen(2);
  135. }
  136. else
  137. {
  138. 圖片資料庫 =str;
  139. yield return StartCoroutine(年份清單());
  140. }
  141. }
  142. IEnumerator 年份清單()
  143. {
  144. string strcon = "年份清單";
  145. string se = Main.Global.預設伺服器路徑;
  146. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}", strcon, se);
  147. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  148. yield return request.SendWebRequest();
  149. if (request.result == UnityWebRequest.Result.ConnectionError)
  150. {
  151. //Debug.Log(request.error);
  152. yield break;
  153. }
  154. JSONNode json = JSON.Parse(request.downloadHandler.text);
  155. 年.options.Clear();
  156. List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
  157. for (int i = 0; i < json.Count; i++)
  158. {
  159. string str = json[i].ToString().Trim('"');
  160. options.Add(new TMP_Dropdown.OptionData(str));
  161. }
  162. 年.AddOptions(options);
  163. DateTime currentDate = DateTime.Now;
  164. string year = currentDate.Year.ToString();
  165. string month = currentDate.Month.ToString("D2");
  166. int currentIndex = 年.options.FindIndex(option => option.text == year);
  167. 年.value = currentIndex;
  168. int currentIndex1 = 月.options.FindIndex(option => option.text == month);
  169. 月.value = currentIndex1;
  170. 年.onValueChanged.AddListener((value) => OnDropdown1ValueChanged());
  171. 月.onValueChanged.AddListener((value) => OnDropdown1ValueChanged());
  172. if (Main.Global.PA=="測試號")
  173. {
  174. 申請人.text=Main.Global.PA;
  175. 編號.text=Main.Global.編號;
  176. yield return StartCoroutine(請假申請明細表());
  177. }
  178. else
  179. {
  180. yield return StartCoroutine(請假申請人員清單());
  181. }
  182. }
  183. private void OnDropdown1ValueChanged()
  184. {
  185. StartCoroutine(請假申請人員清單());
  186. }
  187. IEnumerator 請假申請人員清單()
  188. {
  189. 讀取面板.SetActive(true);
  190. if (copy!= null)
  191. {
  192. DeleteCopies(copy);
  193. }
  194. string strcon = "請假申請人員清單";
  195. string se = Main.Global.預設伺服器路徑;
  196. string pa = 月.options[月.value].text;
  197. string pa1 = 年.options[年.value].text;
  198. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}", strcon, se, pa,pa1);
  199. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  200. yield return request.SendWebRequest();
  201. if (request.result == UnityWebRequest.Result.ConnectionError)
  202. {
  203. //Debug.Log(request.error);
  204. yield break;
  205. }
  206. JSONNode json = JSON.Parse(request.downloadHandler.text);
  207. int sum = 5;
  208. for (int i = 0; i < json.Count; i++)
  209. {
  210. GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
  211. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
  212. buttonObj.transform.SetParent(buttonParent, false);
  213. TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
  214. text[0].text = (i+1).ToString();
  215. text[1].text = json[i][1].ToString().Trim('"');
  216. text[2].text = json[i][2].ToString().Trim('"');
  217. Button bt = buttonObj.GetComponentInChildren<Button>();
  218. int index = i;
  219. bt.onClick.AddListener(() =>
  220. {
  221. 人員清單面板.SetActive(false);
  222. 申請人.text=json[index][2].ToString().Trim('"');
  223. 編號.text=json[index][1].ToString().Trim('"');
  224. 日期.text ="";
  225. 事由.text ="";
  226. StartCoroutine(請假申請明細表());
  227. });
  228. sum+=75;
  229. copy.Add(buttonObj);
  230. }
  231. buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
  232. if (Main.Global.考勤主管權限==true)
  233. {
  234. StartCoroutine(請假申請明細表());
  235. }
  236. else
  237. {
  238. 申請人.text=Main.Global.PA;
  239. 編號.text=Main.Global.編號;
  240. yield return StartCoroutine(請假申請明細表());
  241. }
  242. }
  243. IEnumerator 請假申請明細表()
  244. {
  245. 讀取面板.SetActive(true);
  246. if (copy1!= null)
  247. {
  248. DeleteCopies(copy1);
  249. }
  250. string strcon = "請假申請明細表";
  251. string se = Main.Global.預設伺服器路徑;
  252. string pa = 編號.text;
  253. string pa1 = 年.options[年.value].text + "/" + 月.options[月.value].text;
  254. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}", strcon, se, pa, pa1);
  255. Debug.Log(strUrl);
  256. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  257. yield return request.SendWebRequest();
  258. if (request.result == UnityWebRequest.Result.ConnectionError)
  259. {
  260. //Debug.Log(request.error);
  261. yield break;
  262. }
  263. JSONNode json = JSON.Parse(request.downloadHandler.text);
  264. Debug.Log(json);
  265. int sum = 5;
  266. for (int i = 0; i < json.Count; i++)
  267. {
  268. for (int j = 0; j < json[0].Count; j++)
  269. {
  270. if (json[i][j] == null)
  271. {
  272. json[i][j]="";
  273. }
  274. }
  275. }
  276. for (int i = 0; i < json.Count; i++)
  277. {
  278. GameObject buttonObj = Instantiate(buttonPrefab1) as GameObject;
  279. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
  280. buttonObj.transform.SetParent(buttonParent1, false);
  281. TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
  282. SetTextValue(text[0], json[i][0]);
  283. SetTextValue(text[1], json[i][1]);
  284. SetTextValue(text[2], json[i][2]);
  285. SetTextValue(text[3], json[i][3]);
  286. SetTextValue(text[4], json[i][4]);
  287. SetTextValue(text[5], json[i][9]);
  288. Image[] childImages = buttonObj.GetComponentsInChildren<Image>();
  289. string da = json[i][1].ToString().Trim('"');
  290. string da1 = json[i][2].ToString().Trim('"');
  291. if (da=="星期日")
  292. {
  293. foreach (Image childImage in childImages)
  294. {
  295. if (childImage.transform.parent == buttonObj.transform) // 檢查是否是小圖像
  296. {
  297. childImage.color = new Color32(255, 160, 160, 255); // 改為紅色,你可以自己更改顏色
  298. }
  299. }
  300. }
  301. if (da=="星期六")
  302. {
  303. foreach (Image childImage in childImages)
  304. {
  305. if (childImage.transform.parent == buttonObj.transform) // 檢查是否是小圖像
  306. {
  307. childImage.color = new Color32(255, 160, 160, 255); // 改為紅色,你可以自己更改顏色
  308. }
  309. }
  310. }
  311. if (da1=="國定假日")
  312. {
  313. foreach (Image childImage in childImages)
  314. {
  315. if (childImage.transform.parent == buttonObj.transform) // 檢查是否是小圖像
  316. {
  317. childImage.color = new Color32(255, 160, 160, 255); // 改為紅色,你可以自己更改顏色
  318. }
  319. }
  320. }
  321. else if (da1 != "" )
  322. {
  323. foreach (Image childImage in childImages)
  324. {
  325. if (childImage.transform.parent == buttonObj.transform) // 檢查是否是小圖像
  326. {
  327. childImage.color = new Color32(255, 255, 130, 255); // 改為紅色,你可以自己更改顏色
  328. }
  329. }
  330. }
  331. Button bt = buttonObj.GetComponentInChildren<Button>();
  332. Image im = bt.GetComponentInChildren<Image>();
  333. im.color = new Color32(255, 255, 255, 0);
  334. int index = i;
  335. bt.onClick.AddListener(() =>
  336. {
  337. if (json[index][1].ToString().Trim('"')=="星期六" || json[index][1].ToString().Trim('"')=="星期日" || json[index][2].ToString().Trim('"')=="國定假日")
  338. {
  339. 彈跳面板.SetActive(true);
  340. 彈跳文字.text = "星期六/星期日或國定假日不需要申請休假!!";
  341. }
  342. else
  343. {
  344. 日期.text =json[index][0].ToString().Trim('"');
  345. 生效 =json[index][4].ToString().Trim('"');
  346. 選擇資料庫=json[index][7].ToString().Trim('"');
  347. 選擇流水號=json[index][8].ToString().Trim('"');
  348. 存檔紐.interactable = true;
  349. 刪除紐.interactable = true;
  350. 切換紐.interactable = true;
  351. 回首紐.interactable = true;
  352. SetTextValue1(事由, json[index][3]);
  353. int currentIndex1 = 時數.options.FindIndex(option => option.text == json[index][9].ToString().Trim('"'));
  354. 時數.value = currentIndex1;
  355. int currentIndex = 假別.options.FindIndex(option => option.text == json[index][2].ToString().Trim('"'));
  356. 假別.value = currentIndex;
  357. 縮圖.texture = Texture2D.blackTexture;
  358. if (選擇資料庫 !="" && 選擇流水號 !="")
  359. {
  360. 讀取圖片();
  361. }
  362. else
  363. {
  364. 放圖用.texture =Texture2D.blackTexture;
  365. }
  366. Debug.Log(生效);
  367. Debug.Log(選擇資料庫);
  368. Debug.Log(選擇流水號);
  369. }
  370. });
  371. sum+=75;
  372. copy1.Add(buttonObj);
  373. }
  374. buttonceneter1.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter1.GetComponent<RectTransform>().sizeDelta.x, sum);
  375. 讀取面板.SetActive(false);
  376. }
  377. public void 讀取圖片()
  378. {
  379. string ip = "";
  380. if (Main.Global.預設伺服器路徑=="test")
  381. {
  382. ip="106.1.48.106,4567";
  383. }
  384. else
  385. {
  386. ip="mssql-147629-0.cloudclusters.net,12711";
  387. }
  388. string connectionString = "Data Source="+ ip +";Initial Catalog="+ 選擇資料庫 +";User ID=B70340;Password=Lee0911274990";
  389. Debug.Log(connectionString);
  390. using (SqlConnection connection = new SqlConnection(connectionString))
  391. {
  392. try
  393. {
  394. connection.Open();
  395. string query = "SELECT 圖片 FROM 請假圖片庫 WHERE 流水號 LIKE '" + 選擇流水號 + "'";
  396. using (SqlCommand command = new SqlCommand(query, connection))
  397. {
  398. using (SqlDataReader reader = command.ExecuteReader())
  399. {
  400. if (reader.Read())
  401. {
  402. // 讀取圖片的二進位資料
  403. byte[] imageData = (byte[])reader["圖片"];
  404. // 將二進位資料轉換為 Texture2D
  405. Texture2D texture = new Texture2D(1, 1);
  406. texture.LoadImage(imageData);
  407. // 將 Texture2D 設置為 RawImage 的 texture
  408. 放圖用.texture = texture;
  409. float aspectRatio = (float)texture.width / texture.height;
  410. float newWidth = Mathf.Min(720f, 放圖用.rectTransform.rect.width);
  411. float newHeight = newWidth / aspectRatio;
  412. 放圖用.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
  413. }
  414. }
  415. }
  416. }
  417. catch (Exception ex)
  418. {
  419. Debug.LogError("Error connecting to the database: " + ex.Message);
  420. 彈跳面板.SetActive(true);
  421. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  422. }
  423. finally
  424. {
  425. connection.Close();
  426. }
  427. }
  428. }
  429. private void SetTextValue1(TMP_InputField text, string value)
  430. {
  431. if (value == null)
  432. {
  433. text.text = "";
  434. }
  435. else
  436. {
  437. string jsonString = value.Trim('"');
  438. string unescapedString = Regex.Unescape(jsonString);
  439. text.text = unescapedString;
  440. }
  441. }
  442. private void SetTextValue(TextMeshProUGUI text, string value)
  443. {
  444. if (value == null)
  445. {
  446. text.text = "";
  447. }
  448. else
  449. {
  450. string jsonString = value.Trim('"');
  451. string unescapedString = Regex.Unescape(jsonString);
  452. text.text = unescapedString;
  453. }
  454. }
  455. public void 修改()
  456. {
  457. string str = 時數.options[時數.value].text;
  458. if (申請人.text == "" || 日期.text =="" || str =="")
  459. {
  460. 彈跳面板.SetActive(true);
  461. 彈跳文字.text = "人員、日期、請休假時數 不能為空白!!";
  462. }
  463. else
  464. {
  465. if (生效 == "已核准" || 生效 =="未核准")
  466. {
  467. 彈跳面板.SetActive(true);
  468. 彈跳文字.text = "已簽核的資料不能再做修改";
  469. }
  470. else
  471. {
  472. StartCoroutine(是否有請假資料());
  473. }
  474. }
  475. }
  476. public void 刪除()
  477. {
  478. if (申請人.text == "" || 日期.text =="" )
  479. {
  480. 彈跳面板.SetActive(true);
  481. 彈跳文字.text = "人員、日期 不能為空白!!";
  482. }
  483. else
  484. {
  485. if (生效 == "已核准" || 生效 =="未核准")
  486. {
  487. 彈跳面板.SetActive(true);
  488. 彈跳文字.text = "已簽核的資料不能再做修改";
  489. }
  490. else
  491. {
  492. StartCoroutine(請假資料刪除());
  493. }
  494. }
  495. }
  496. IEnumerator 是否有請假資料()
  497. {
  498. 讀取面板.SetActive(true);
  499. string strcon = "是否有請假資料";
  500. string se = Main.Global.預設伺服器路徑;
  501. string pa = 日期.text;
  502. string pa1 = 編號.text;
  503. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}", strcon, se, pa, pa1);
  504. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  505. yield return request.SendWebRequest();
  506. if (request.result == UnityWebRequest.Result.ConnectionError)
  507. {
  508. //Debug.Log(request.error);
  509. yield break;
  510. }
  511. string str = request.downloadHandler.text.ToString();
  512. if (str == "n")
  513. {
  514. yield return StartCoroutine(查詢最後一筆圖片編號("新增"));
  515. }
  516. else
  517. {
  518. if (選擇資料庫 !="" && 選擇流水號 !="")
  519. {
  520. yield return StartCoroutine(請假資料修改(""));
  521. }
  522. else
  523. {
  524. yield return StartCoroutine(查詢最後一筆圖片編號("修改"));
  525. }
  526. }
  527. }
  528. IEnumerator 請假資料新增(string 新流水)
  529. {
  530. string pa4 = "";
  531. string pa5 = "";
  532. if (縮圖.texture != null && 縮圖.texture != Texture2D.blackTexture)
  533. {
  534. 圖片上傳(新流水);
  535. pa4 = 圖片資料庫;
  536. pa5 = 新流水;
  537. }
  538. string strcon = "請假資料新增";
  539. string se = Main.Global.預設伺服器路徑;
  540. string pa = 編號.text;
  541. string pa1 = 日期.text;
  542. string pa2 = 假別.options[假別.value].text;
  543. string pa3 = 事由.text;
  544. string pa6 = 時數.options[時數.value].text;
  545. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}&PA2={4}&PA3={5}&PA4={6}&PA5={7}&PA6={8}", strcon, se, pa, pa1, pa2, pa3, pa4, pa5, pa6);
  546. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  547. yield return request.SendWebRequest();
  548. if (request.result == UnityWebRequest.Result.ConnectionError)
  549. {
  550. //Debug.Log(request.error);
  551. yield break;
  552. }
  553. yield return StartCoroutine(請假申請明細表());
  554. 縮圖.texture = Texture2D.blackTexture;
  555. 彈跳面板.SetActive(true);
  556. 彈跳文字.text = "新增成功";
  557. }
  558. IEnumerator 查詢最後一筆圖片編號(string 狀態)
  559. {
  560. string strcon = "查詢最後一筆圖片編號";
  561. string se = Main.Global.預設伺服器路徑;
  562. string pa1 = 圖片資料庫;
  563. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA1={2}", strcon, se,pa1);
  564. Debug.Log(strUrl);
  565. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  566. yield return request.SendWebRequest();
  567. if (request.result == UnityWebRequest.Result.ConnectionError)
  568. {
  569. //Debug.Log(request.error);
  570. yield break;
  571. }
  572. string str = request.downloadHandler.text.ToString();
  573. if (str == "n")
  574. {
  575. str ="IR00000000";
  576. }
  577. string cnum = str.Substring(2);
  578. int inum = int.Parse(cnum);
  579. inum+=1;
  580. string 新流水 = "IR";
  581. if (inum<10)
  582. {
  583. 新流水+="0000000"+inum.ToString();
  584. }
  585. if (inum>9 && inum<100)
  586. {
  587. 新流水+="000000"+inum.ToString();
  588. }
  589. if (inum>99 && inum<1000)
  590. {
  591. 新流水+="00000"+inum.ToString();
  592. }
  593. if (inum>999 && inum<10000)
  594. {
  595. 新流水+="0000"+inum.ToString();
  596. }
  597. if (inum>9999 && inum<100000)
  598. {
  599. 新流水+="000"+inum.ToString();
  600. }
  601. if (inum>99999 && inum<10000000)
  602. {
  603. 新流水+="00"+inum.ToString();
  604. }
  605. if (inum>999999 && inum<10000000)
  606. {
  607. 新流水+="0"+inum.ToString();
  608. }
  609. if (inum>9999999 && inum<100000000)
  610. {
  611. 新流水+=inum.ToString();
  612. }
  613. if (狀態 == "修改")
  614. {
  615. yield return StartCoroutine(請假資料修改(新流水));
  616. }
  617. else
  618. {
  619. yield return StartCoroutine(請假資料新增(新流水));
  620. }
  621. }
  622. public void 圖片上傳(string 流水)
  623. {
  624. string ip = "";
  625. if (Main.Global.預設伺服器路徑=="test")
  626. {
  627. ip="106.1.48.106,4567";
  628. }
  629. else
  630. {
  631. ip="mssql-147629-0.cloudclusters.net,12711";
  632. }
  633. string connectionString = "Data Source="+ ip +";Initial Catalog="+ 圖片資料庫 +";User ID=B70340;Password=Lee0911274990";
  634. Debug.Log(connectionString);
  635. using (SqlConnection connection = new SqlConnection(connectionString))
  636. {
  637. try
  638. {
  639. connection.Open();
  640. Texture2D texture2D = new Texture2D((放圖用.texture as Texture2D).width, (放圖用.texture as Texture2D).height);
  641. Graphics.CopyTexture(放圖用.texture, texture2D);
  642. // 將 Texture2D 轉換為 PNG 格式的二進制數據
  643. byte[] imageBytes = texture2D.EncodeToPNG();
  644. string query = "INSERT INTO 請假圖片庫 (流水號, 圖片) VALUES (@流水號, @binary_file)";
  645. using (SqlCommand command = new SqlCommand(query, connection))
  646. {
  647. // 添加流水號參數
  648. command.Parameters.AddWithValue("@流水號", 流水);
  649. // 添加二進制圖片數據參數
  650. command.Parameters.AddWithValue("@binary_file", imageBytes);
  651. int rowsAffected = command.ExecuteNonQuery();
  652. Debug.Log("成功插入 " + rowsAffected + " 行數據");
  653. }
  654. }
  655. catch (Exception ex)
  656. {
  657. Debug.LogError("Error connecting to the database: " + ex.Message);
  658. 彈跳面板.SetActive(true);
  659. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  660. }
  661. finally
  662. {
  663. connection.Close();
  664. }
  665. }
  666. }
  667. public void 圖片修改()
  668. {
  669. string 流水=選擇流水號;
  670. string ip = "";
  671. if (Main.Global.預設伺服器路徑=="test")
  672. {
  673. ip="106.1.48.106,4567";
  674. }
  675. else
  676. {
  677. ip="mssql-147629-0.cloudclusters.net,12711";
  678. }
  679. string connectionString = "Data Source="+ ip +";Initial Catalog="+ 選擇資料庫 +";User ID=B70340;Password=Lee0911274990";
  680. Debug.Log(connectionString);
  681. using (SqlConnection connection = new SqlConnection(connectionString))
  682. {
  683. try
  684. {
  685. connection.Open();
  686. Texture2D texture2D = new Texture2D((放圖用.texture as Texture2D).width, (放圖用.texture as Texture2D).height);
  687. Graphics.CopyTexture(放圖用.texture, texture2D);
  688. // 將 Texture2D 轉換為 PNG 格式的二進制數據
  689. byte[] imageBytes = texture2D.EncodeToPNG();
  690. string query = "UPDATE 請假圖片庫 SET 圖片 = @binary_file WHERE 流水號 LIKE @流水號";
  691. using (SqlCommand command = new SqlCommand(query, connection))
  692. {
  693. // 添加流水號參數
  694. command.Parameters.AddWithValue("@流水號", 流水);
  695. // 添加二進制圖片數據參數
  696. command.Parameters.AddWithValue("@binary_file", imageBytes);
  697. int rowsAffected = command.ExecuteNonQuery();
  698. Debug.Log("成功修改 " + rowsAffected + " 行數據");
  699. }
  700. }
  701. catch (Exception ex)
  702. {
  703. Debug.LogError("Error connecting to the database: " + ex.Message);
  704. 彈跳面板.SetActive(true);
  705. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  706. }
  707. finally
  708. {
  709. connection.Close();
  710. }
  711. }
  712. }
  713. IEnumerator 請假資料修改(string 流水)
  714. {
  715. string strcon = "請假資料修改";
  716. string pa4 = 選擇資料庫;
  717. string pa5 = 選擇流水號;
  718. if (縮圖.texture != null && 縮圖.texture != Texture2D.blackTexture)
  719. {
  720. if (選擇資料庫 !="" && 選擇流水號 !="")
  721. {
  722. 圖片修改();
  723. }
  724. else
  725. {
  726. 圖片上傳(流水);
  727. pa4 = 圖片資料庫;
  728. pa5 = 流水;
  729. }
  730. }
  731. string se = Main.Global.預設伺服器路徑;
  732. string pa = 編號.text;
  733. string pa1 = 日期.text;
  734. string pa2 = 假別.options[假別.value].text;
  735. string pa3 = 事由.text;
  736. string pa6 = 時數.options[時數.value].text;
  737. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}&PA2={4}&PA3={5}&PA4={6}&PA5={7}&PA6={8}", strcon, se, pa, pa1, pa2, pa3, pa4, pa5, pa6);
  738. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  739. Debug.Log(strUrl);
  740. yield return request.SendWebRequest();
  741. if (request.result == UnityWebRequest.Result.ConnectionError)
  742. {
  743. //Debug.Log(request.error);
  744. yield break;
  745. }
  746. 彈跳面板.SetActive(true);
  747. 彈跳文字.text = "修改成功";
  748. 縮圖.texture = Texture2D.blackTexture;
  749. yield return StartCoroutine(請假申請明細表());
  750. }
  751. IEnumerator 請假資料刪除()
  752. {
  753. 讀取面板.SetActive(true);
  754. string strcon = "請假資料刪除";
  755. string se = Main.Global.預設伺服器路徑;
  756. string pa = 日期.text;
  757. string pa1 = 編號.text;
  758. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}", strcon, se, pa, pa1);
  759. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  760. yield return request.SendWebRequest();
  761. if (request.result == UnityWebRequest.Result.ConnectionError)
  762. {
  763. //Debug.Log(request.error);
  764. yield break;
  765. }
  766. 事由.text = "";
  767. 生效="";
  768. yield return StartCoroutine(請假資料圖片刪除());
  769. }
  770. IEnumerator 請假資料圖片刪除()
  771. {
  772. string strcon = "請假資料圖片刪除";
  773. string se = Main.Global.預設伺服器路徑;
  774. string pa1 = 選擇資料庫;
  775. string pa = 選擇流水號;
  776. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}", strcon, se, pa, pa1);
  777. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  778. yield return request.SendWebRequest();
  779. if (request.result == UnityWebRequest.Result.ConnectionError)
  780. {
  781. //Debug.Log(request.error);
  782. yield break;
  783. }
  784. 選擇資料庫="";
  785. 選擇流水號="";
  786. 放圖用.texture =Texture2D.blackTexture;
  787. 縮圖.texture = Texture2D.blackTexture;
  788. 彈跳面板.SetActive(true);
  789. 彈跳文字.text = "刪除成功";
  790. yield return StartCoroutine(請假申請明細表());
  791. }
  792. public void 頁面切換()
  793. {
  794. if (主頁.activeSelf && !圖頁.activeSelf)
  795. {
  796. 主頁.SetActive(false);
  797. 圖頁.SetActive(true);
  798. 拍照用.gameObject.SetActive(false);
  799. 放圖用.gameObject.SetActive(true);
  800. 存檔紐.interactable = false;
  801. 刪除紐.interactable = false;
  802. 切換紐.interactable = true;
  803. 回首紐.interactable = true;
  804. }
  805. else if (圖頁.activeSelf && !主頁.activeSelf)
  806. {
  807. 圖頁.SetActive(false);
  808. 主頁.SetActive(true);
  809. if (myCam != null)
  810. {
  811. myCam.Stop();
  812. myCam = null;
  813. }
  814. 存檔紐.interactable = true;
  815. 刪除紐.interactable = true;
  816. 切換紐.interactable = true;
  817. 回首紐.interactable = true;
  818. }
  819. }
  820. public void 相機鈕()
  821. {
  822. if (拍照用.gameObject.activeSelf && !放圖用.gameObject.activeSelf)
  823. {
  824. Texture2D photoTexture = CapturePhoto();
  825. if (photoTexture != null)
  826. {
  827. 放圖用.rectTransform.sizeDelta = new Vector2(720f, 1280f);
  828. 放圖用.texture = photoTexture;
  829. 縮圖.texture = photoTexture;
  830. }
  831. 存檔紐.interactable = true;
  832. 刪除紐.interactable = true;
  833. 切換紐.interactable = true;
  834. 回首紐.interactable = true;
  835. myCam.Stop();
  836. myCam = null;
  837. 提示字.text ="";
  838. 拍照用.gameObject.SetActive(false);
  839. 放圖用.gameObject.SetActive(true);
  840. 圖頁.SetActive(false);
  841. 主頁.SetActive(true);
  842. }
  843. else if (放圖用.gameObject.activeSelf && !拍照用.gameObject.activeSelf)
  844. {
  845. StartCoroutine(open_Camera(60));
  846. 提示字.text ="拍照中";
  847. 存檔紐.interactable = false;
  848. 刪除紐.interactable = false;
  849. 切換紐.interactable = false;
  850. 回首紐.interactable = false;
  851. 拍照用.gameObject.SetActive(true);
  852. 放圖用.gameObject.SetActive(false);
  853. }
  854. }
  855. private IEnumerator open_Camera(int fps)
  856. {
  857. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); //授權開啟鏡頭
  858. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  859. {
  860. /* (攝影機名稱, 攝影機要拍到的寬度, 攝影機要拍到的高度, 攝影機的FPS) */
  861. myCam = new WebCamTexture(WebCamTexture.devices[0].name, 720, 1280, fps);
  862. 拍照用.texture = myCam;
  863. myCam.Play();//開啟攝影機
  864. }
  865. }
  866. private Texture2D CapturePhoto()
  867. {
  868. if (myCam != null)
  869. {
  870. int width = myCam.width;
  871. int height = myCam.height;
  872. // 创建一个新的 Texture2D,并读取当前帧的像素
  873. Texture2D photoTexture = new Texture2D(width, height);
  874. photoTexture.SetPixels(myCam.GetPixels());
  875. photoTexture.Apply();
  876. Texture2D rotatedTexture = RotateTexture(photoTexture, true);
  877. return rotatedTexture;
  878. }
  879. Texture2D RotateTexture(Texture2D originalTexture, bool clockwise)
  880. {
  881. int width = originalTexture.width;
  882. int height = originalTexture.height;
  883. Texture2D rotatedTexture = new Texture2D(height, width);
  884. Color32[] originalPixels = originalTexture.GetPixels32();
  885. Color32[] rotatedPixels = new Color32[width * height];
  886. for (int y = 0; y < height; y++)
  887. {
  888. for (int x = 0; x < width; x++)
  889. {
  890. int newX = clockwise ? y : height - y - 1;
  891. int newY = clockwise ? width - x - 1 : x;
  892. rotatedPixels[newY * height + newX] = originalPixels[y * width + x];
  893. }
  894. }
  895. rotatedTexture.SetPixels32(rotatedPixels);
  896. rotatedTexture.Apply();
  897. return rotatedTexture;
  898. }
  899. return null;
  900. }
  901. public void 彈跳面板關閉()
  902. {
  903. 彈跳面板.SetActive(false);
  904. 彈跳文字.text = "";
  905. }
  906. public void 人員面板關閉()
  907. {
  908. 人員清單面板.SetActive(false);
  909. }
  910. public void 人員面板開啟()
  911. {
  912. 人員清單面板.SetActive(true);
  913. }
  914. private void DeleteCopies(List<GameObject> copyList)
  915. {
  916. foreach (GameObject copy in copyList)
  917. {
  918. Destroy(copy);
  919. }
  920. copyList.Clear();
  921. }
  922. public void back()
  923. {
  924. change_Secen(2);
  925. }
  926. public void change_Secen(int _screenNum)
  927. {
  928. SceneManager.LoadScene(_screenNum);
  929. }
  930. }