Brak opisu
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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] GameObject 選單;
  21. [SerializeField] GameObject 未准;
  22. [SerializeField] GameObject 核准;
  23. [SerializeField] RawImage 放圖用;
  24. [SerializeField] GameObject 主頁;
  25. [SerializeField] GameObject 圖頁;
  26. public GameObject buttonPrefab;
  27. public Transform buttonParent;
  28. public GameObject buttonceneter;
  29. private List<GameObject> copy = new List<GameObject>();
  30. [SerializeField] TextMeshProUGUI 假別;
  31. [SerializeField] TextMeshProUGUI 日期;
  32. [SerializeField] TextMeshProUGUI 申請人;
  33. [SerializeField] TextMeshProUGUI 編號;
  34. [SerializeField] TextMeshProUGUI 事由;
  35. public CanvasScaler canvasScaler;
  36. [SerializeField] GameObject 讀取面板;
  37. [SerializeField] TextMeshProUGUI 讀取;
  38. [SerializeField] GameObject 彈跳面板;
  39. [SerializeField] TextMeshProUGUI 彈跳文字;
  40. private string loadingText = "";
  41. private int dotCount = 0;
  42. string 選擇資料庫;
  43. string 選擇流水號;
  44. void Start()
  45. {
  46. Screen.autorotateToPortrait = false;
  47. Screen.orientation = ScreenOrientation.Portrait;
  48. Main.Global.螢幕方向="直";
  49. //Main.Global.固高=1900f;
  50. //Main.Global.固寬=900f;
  51. //Main.Global.固比=1f;
  52. //Main.Global.螢幕格式="瘦";
  53. //Main.Global.PA="王詩文";
  54. canvasScaler.referenceResolution = new Vector2(Main.Global.固寬, Main.Global.固高);
  55. canvasScaler.matchWidthOrHeight=Main.Global.固比;
  56. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/hskin_login.php?";
  57. //Main.Global.預設伺服器路徑="test";
  58. 主頁.SetActive(true);
  59. 選單.SetActive(false);
  60. 圖頁.SetActive(false);
  61. 未准.SetActive(false);
  62. 核准.SetActive(false);
  63. StartCoroutine(AnimateText());
  64. StartCoroutine(加班核准清單());
  65. }
  66. IEnumerator AnimateText()
  67. {
  68. string qqq = 讀取.text.ToString();
  69. while (true)
  70. {
  71. yield return new WaitForSeconds(0.5f);
  72. if (dotCount < 3)
  73. {
  74. loadingText += ".";
  75. dotCount++;
  76. }
  77. else
  78. {
  79. loadingText = "";
  80. dotCount = 0;
  81. }
  82. 讀取.text = qqq+loadingText;
  83. }
  84. }
  85. IEnumerator 加班核准清單()
  86. {
  87. 讀取面板.SetActive(true);
  88. if (copy!= null)
  89. {
  90. DeleteCopies(copy);
  91. }
  92. string strcon = "加班核准清單";
  93. string se = Main.Global.預設伺服器路徑;
  94. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}", strcon, se);
  95. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  96. yield return request.SendWebRequest();
  97. if (request.result == UnityWebRequest.Result.ConnectionError)
  98. {
  99. //Debug.Log(request.error);
  100. yield break;
  101. }
  102. JSONNode json = JSON.Parse(request.downloadHandler.text);
  103. int sum = 5;
  104. for (int i = 0; i < json.Count; i++)
  105. {
  106. GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
  107. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
  108. buttonObj.transform.SetParent(buttonParent, false);
  109. TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
  110. SetTextValue(text[0], json[i][0]);
  111. SetTextValue(text[1], json[i][2]);
  112. SetTextValue(text[2], json[i][3]);
  113. SetTextValue(text[3], json[i][5]);
  114. SetTextValue(text[4], json[i][6]);
  115. Button bt = buttonObj.GetComponentInChildren<Button>();
  116. int index = i;
  117. bt.onClick.AddListener(() =>
  118. {
  119. SetTextValue(申請人, json[index][0]);
  120. SetTextValue(編號, json[index][1]);
  121. SetTextValue(日期, json[index][2]);
  122. SetTextValue(假別, json[index][5]);
  123. SetTextValue(事由, json[index][6]);
  124. 主頁.SetActive(false);
  125. 圖頁.SetActive(true);
  126. 選單.SetActive(true);
  127. 未准.SetActive(true);
  128. 核准.SetActive(true);
  129. if (json[index][12] ==null)
  130. {
  131. 選擇資料庫="";
  132. }
  133. else
  134. {
  135. 選擇資料庫=json[index][12].ToString().Trim('"');
  136. }
  137. if (json[index][13] ==null)
  138. {
  139. 選擇流水號="";
  140. }
  141. else
  142. {
  143. 選擇流水號=json[index][13].ToString().Trim('"');
  144. }
  145. if (選擇資料庫 !="" && 選擇流水號 !="")
  146. {
  147. 讀取圖片();
  148. }
  149. else
  150. {
  151. 放圖用.texture =Texture2D.blackTexture;
  152. }
  153. });
  154. sum+=75;
  155. copy.Add(buttonObj);
  156. }
  157. buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
  158. 讀取面板.SetActive(false);
  159. }
  160. public void 讀取圖片()
  161. {
  162. string ip = "";
  163. if (Main.Global.預設伺服器路徑=="test")
  164. {
  165. ip="106.1.48.106,4567";
  166. }
  167. else
  168. {
  169. ip="mssql-147629-0.cloudclusters.net,12711";
  170. }
  171. string connectionString = "Data Source="+ ip +";Initial Catalog="+ 選擇資料庫 +";User ID=B70340;Password=Lee0911274990";
  172. Debug.Log(connectionString);
  173. using (SqlConnection connection = new SqlConnection(connectionString))
  174. {
  175. try
  176. {
  177. connection.Open();
  178. string query = "SELECT 圖片 FROM 加班圖片庫 WHERE 流水號 LIKE '" + 選擇流水號 + "'";
  179. using (SqlCommand command = new SqlCommand(query, connection))
  180. {
  181. using (SqlDataReader reader = command.ExecuteReader())
  182. {
  183. if (reader.Read())
  184. {
  185. // 讀取圖片的二進位資料
  186. byte[] imageData = (byte[])reader["圖片"];
  187. // 將二進位資料轉換為 Texture2D
  188. Texture2D texture = new Texture2D(1, 1);
  189. texture.LoadImage(imageData);
  190. // 將 Texture2D 設置為 RawImage 的 texture
  191. 放圖用.texture = texture;
  192. float aspectRatio = (float)texture.width / texture.height;
  193. float newWidth = Mathf.Min(720f, 放圖用.rectTransform.rect.width);
  194. float newHeight = newWidth / aspectRatio;
  195. 放圖用.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
  196. }
  197. }
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. Debug.LogError("Error connecting to the database: " + ex.Message);
  203. 彈跳面板.SetActive(true);
  204. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  205. }
  206. finally
  207. {
  208. connection.Close();
  209. }
  210. }
  211. }
  212. private void SetTextValue(TextMeshProUGUI text, string value)
  213. {
  214. if (value == null)
  215. {
  216. text.text = "";
  217. }
  218. else
  219. {
  220. string jsonString = value.Trim('"');
  221. string unescapedString = Regex.Unescape(jsonString);
  222. text.text = unescapedString;
  223. }
  224. }
  225. public void 核准紐()
  226. {
  227. if (日期.text != "" && 編號.text != "")
  228. {
  229. StartCoroutine(加班審核核准());
  230. }
  231. else
  232. {
  233. 彈跳面板.SetActive(false);
  234. 彈跳文字.text = "未選擇日期";
  235. }
  236. }
  237. IEnumerator 加班審核核准()
  238. {
  239. 讀取面板.SetActive(true);
  240. string strcon = "加班審核核准";
  241. string se = Main.Global.預設伺服器路徑;
  242. string pa = Main.Global.PA;
  243. DateTime currentDate = DateTime.Now;
  244. string pa1 = currentDate.ToString("yyyy/MM/dd");
  245. string pa2 = 日期.text;
  246. string pa3 = 編號.text;
  247. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}&PA2={4}&PA3={5}", strcon, se, pa, pa1, pa2, pa3);
  248. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  249. yield return request.SendWebRequest();
  250. if (request.result == UnityWebRequest.Result.ConnectionError)
  251. {
  252. //Debug.Log(request.error);
  253. yield break;
  254. }
  255. 圖頁.SetActive(false);
  256. 主頁.SetActive(true);
  257. 選單.SetActive(false);
  258. 未准.SetActive(false);
  259. 核准.SetActive(false);
  260. 彈跳面板.SetActive(true);
  261. 彈跳文字.text = "核准成功";
  262. StartCoroutine(加班核准清單());
  263. }
  264. public void 未准紐()
  265. {
  266. if (日期.text != "" && 編號.text != "")
  267. {
  268. StartCoroutine(加班審核未准());
  269. }
  270. else
  271. {
  272. 彈跳面板.SetActive(false);
  273. 彈跳文字.text = "未選擇日期";
  274. }
  275. }
  276. IEnumerator 加班審核未准()
  277. {
  278. 讀取面板.SetActive(true);
  279. string strcon = "加班審核未准";
  280. string se = Main.Global.預設伺服器路徑;
  281. string pa = "未准-"+Main.Global.PA;
  282. DateTime currentDate = DateTime.Now;
  283. string pa1 = currentDate.ToString("yyyy/MM/dd");
  284. string pa2 = 日期.text;
  285. string pa3 = 編號.text;
  286. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA={2}&PA1={3}&PA2={4}&PA3={5}", strcon, se, pa, pa1, pa2, pa3);
  287. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  288. yield return request.SendWebRequest();
  289. if (request.result == UnityWebRequest.Result.ConnectionError)
  290. {
  291. //Debug.Log(request.error);
  292. yield break;
  293. }
  294. 圖頁.SetActive(false);
  295. 主頁.SetActive(true);
  296. 選單.SetActive(false);
  297. 未准.SetActive(false);
  298. 核准.SetActive(false);
  299. 彈跳面板.SetActive(true);
  300. 彈跳文字.text = "未准成功";
  301. StartCoroutine(加班核准清單());
  302. }
  303. public void 切換紐()
  304. {
  305. if (主頁.activeSelf && !圖頁.activeSelf)
  306. {
  307. 主頁.SetActive(false);
  308. 圖頁.SetActive(true);
  309. 選單.SetActive(true);
  310. 未准.SetActive(true);
  311. 核准.SetActive(true);
  312. }
  313. else if (圖頁.activeSelf && !主頁.activeSelf)
  314. {
  315. 圖頁.SetActive(false);
  316. 主頁.SetActive(true);
  317. 選單.SetActive(false);
  318. 未准.SetActive(false);
  319. 核准.SetActive(false);
  320. }
  321. }
  322. public void 彈跳面板關閉()
  323. {
  324. 彈跳面板.SetActive(false);
  325. 彈跳文字.text = "";
  326. }
  327. private void DeleteCopies(List<GameObject> copyList)
  328. {
  329. foreach (GameObject copy in copyList)
  330. {
  331. Destroy(copy);
  332. }
  333. copyList.Clear();
  334. }
  335. public void back()
  336. {
  337. change_Secen(2);
  338. }
  339. public void change_Secen(int _screenNum)
  340. {
  341. SceneManager.LoadScene(_screenNum);
  342. }
  343. }