Sin descripción
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. public RawImage rawImage1;
  21. public RawImage rawImage;
  22. public Texture2D canvasTexture;
  23. public Color drawColor = Color.black;
  24. public float brushSize = 10f;
  25. private bool isDrawing = false;
  26. private Vector2 lastPos;
  27. [SerializeField] GameObject 條跳面板;
  28. [SerializeField] GameObject 寫面板;
  29. [SerializeField] GameObject 看面板;
  30. public CanvasScaler canvasScaler;
  31. float screenWidth;
  32. float screenHeight;
  33. public GameObject buttonPrefab;
  34. public Transform buttonParent;
  35. public GameObject buttonceneter;
  36. private List<GameObject> copy = new List<GameObject>();
  37. void Start()
  38. {
  39. Screen.autorotateToPortrait = false;
  40. Screen.orientation = ScreenOrientation.LandscapeLeft;
  41. float rate = 900.0f / 1900.0f;
  42. if (canvasScaler == null)
  43. {
  44. canvasScaler = GetComponent<CanvasScaler>();
  45. }
  46. if (Main.Global.rate<rate)
  47. {
  48. canvasScaler.matchWidthOrHeight = 1f;
  49. }
  50. else
  51. {
  52. canvasScaler.matchWidthOrHeight = 0f;
  53. }
  54. ddd();
  55. }
  56. void ddd()
  57. {
  58. canvasTexture = new Texture2D((int)rawImage.rectTransform.rect.width, (int)rawImage.rectTransform.rect.height);
  59. Color[] fillColorArray = canvasTexture.GetPixels();
  60. for (int i = 0; i < fillColorArray.Length; ++i)
  61. {
  62. fillColorArray[i] = Color.white;
  63. }
  64. canvasTexture.SetPixels(fillColorArray);
  65. canvasTexture.Apply();
  66. rawImage.texture = canvasTexture;
  67. }
  68. void Update()
  69. {
  70. if (Input.GetMouseButtonDown(0))
  71. {
  72. isDrawing = true;
  73. lastPos = GetMousePosition();
  74. }
  75. else if (Input.GetMouseButtonUp(0))
  76. {
  77. isDrawing = false;
  78. }
  79. if (isDrawing)
  80. {
  81. Vector2 currentPos = GetMousePosition();
  82. DrawLine(lastPos, currentPos);
  83. lastPos = currentPos;
  84. }
  85. }
  86. void DrawLine(Vector2 from, Vector2 to)
  87. {
  88. Vector2 diff = to - from;
  89. float distance = diff.magnitude;
  90. int steps = Mathf.RoundToInt(distance);
  91. for (int i = 0; i < steps; i++)
  92. {
  93. float t = i / distance;
  94. Vector2 point = Vector2.Lerp(from, to, t);
  95. DrawPoint((int)point.x, (int)point.y);
  96. }
  97. canvasTexture.Apply();
  98. }
  99. void DrawPoint(int x, int y)
  100. {
  101. for (int i = -Mathf.RoundToInt(brushSize / 2); i < Mathf.RoundToInt(brushSize / 2); i++)
  102. {
  103. for (int j = -Mathf.RoundToInt(brushSize / 2); j < Mathf.RoundToInt(brushSize / 2); j++)
  104. {
  105. if (x + i >= 0 && x + i < canvasTexture.width && y + j >= 0 && y + j < canvasTexture.height)
  106. {
  107. canvasTexture.SetPixel(x + i, y + j, drawColor);
  108. }
  109. }
  110. }
  111. }
  112. Vector2 GetMousePosition()
  113. {
  114. Vector2 localMousePos;
  115. RectTransformUtility.ScreenPointToLocalPointInRectangle(rawImage.rectTransform, Input.mousePosition, null, out localMousePos);
  116. return localMousePos + rawImage.rectTransform.rect.size / 2;
  117. }
  118. public void SaveImage()
  119. {
  120. 條跳面板.SetActive(true);
  121. byte[] bytes = canvasTexture.EncodeToPNG();
  122. System.IO.File.WriteAllBytes(Application.persistentDataPath + "/drawnImage.png", bytes);
  123. string asd =System.Convert.ToBase64String(bytes);
  124. StartCoroutine(電簽流水查詢(asd));
  125. }
  126. IEnumerator 電簽流水查詢(string asd)
  127. {
  128. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/shto_main.php?";
  129. //Main.Global.預設伺服器路徑="test";
  130. string strcon = "電簽流水查詢";
  131. string se = Main.Global.預設伺服器路徑;
  132. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}", strcon, se);
  133. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  134. yield return request.SendWebRequest();
  135. if (request.result == UnityWebRequest.Result.ConnectionError)
  136. {
  137. Debug.Log(request.error);
  138. yield break;
  139. }
  140. string num = request.downloadHandler.text.ToString();
  141. Debug.Log(num);
  142. string 流水號 = "PC";
  143. if (num == "n")
  144. {
  145. 流水號 ="PC00000001";
  146. }
  147. else
  148. {
  149. string cnum = num.Substring(2);
  150. int inum = int.Parse(cnum);
  151. inum+=1;
  152. if (inum<10)
  153. {
  154. 流水號+="0000000"+inum.ToString();
  155. }
  156. if (inum>9 && inum<100)
  157. {
  158. 流水號+="000000"+inum.ToString();
  159. }
  160. if (inum>99 && inum<1000)
  161. {
  162. 流水號+="00000"+inum.ToString();
  163. }
  164. if (inum>999 && inum<10000)
  165. {
  166. 流水號+="0000"+inum.ToString();
  167. }
  168. if (inum>9999 && inum<100000)
  169. {
  170. 流水號+="000"+inum.ToString();
  171. }
  172. if (inum>99999 && inum<10000000)
  173. {
  174. 流水號+="00"+inum.ToString();
  175. }
  176. if (inum>999999 && inum<10000000)
  177. {
  178. 流水號+="0"+inum.ToString();
  179. }
  180. if (inum>9999999 && inum<100000000)
  181. {
  182. 流水號+=inum.ToString();
  183. }
  184. }
  185. Debug.Log(流水號);
  186. StartCoroutine(電簽上傳(asd,流水號));
  187. }
  188. IEnumerator 電簽上傳(string pa1,string pa2)
  189. {
  190. string 阿帕契路徑1 = "";
  191. if (Main.Global.阿帕契路徑=="http://106.1.48.106:8080/wp-content/themes/event-star/shto_main.php?")
  192. {
  193. 阿帕契路徑1="http://106.1.48.106:8080/wp-content/themes/event-star/shto_update.php?";
  194. }
  195. else
  196. {
  197. 阿帕契路徑1="http://210.61.41.234/ShTo/wp-content/themes/event-star/shto_update.php?";
  198. }
  199. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/shto_update.php?";
  200. //Main.Global.預設伺服器路徑="test";
  201. string se = Main.Global.預設伺服器路徑;
  202. string pa = "王詩文";
  203. WWWForm form = new WWWForm();
  204. form.AddField("se", se);
  205. form.AddField("PA", pa);
  206. form.AddField("PA1", pa1);
  207. form.AddField("PA2", pa2);
  208. using (UnityWebRequest request = UnityWebRequest.Post(阿帕契路徑1, form))
  209. {
  210. yield return request.SendWebRequest();
  211. if (request.result == UnityWebRequest.Result.ConnectionError)
  212. {
  213. Debug.Log(request.error);
  214. }
  215. else
  216. {
  217. Debug.Log("Image uploaded successfully");
  218. }
  219. }
  220. }
  221. IEnumerator 電簽清單()
  222. {
  223. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/shto_main.php?";
  224. //Main.Global.預設伺服器路徑="test";
  225. string strcon = "電簽清單";
  226. string se = Main.Global.預設伺服器路徑;
  227. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}", strcon, se);
  228. Debug.Log(strUrl);
  229. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  230. yield return request.SendWebRequest();
  231. if (request.result == UnityWebRequest.Result.ConnectionError)
  232. {
  233. Debug.Log(request.error);
  234. yield break;
  235. }
  236. JSONNode json = JSON.Parse(request.downloadHandler.text);
  237. Debug.Log(json);
  238. int sum = 0;
  239. for (int i = 0; i < json.Count; i++)
  240. {
  241. GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
  242. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(3, -sum);
  243. buttonObj.transform.SetParent(buttonParent, false);
  244. string idname = json[i][0].ToString().Trim('"');
  245. TextMeshProUGUI text = buttonObj.GetComponentInChildren<TextMeshProUGUI>();
  246. text.text = idname;
  247. Button newButton = buttonObj.GetComponentInChildren<Button>();
  248. int index = i;
  249. newButton.onClick.AddListener(() =>
  250. {
  251. string pa1 = json[index][2].ToString().Trim('"');
  252. byte[] bytes = System.Convert.FromBase64String(pa1);
  253. Texture2D texture = new Texture2D(2, 2);
  254. texture.LoadImage(bytes);
  255. rawImage1.texture = texture;
  256. });
  257. sum+=50;
  258. copy.Add(buttonObj);
  259. }
  260. buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
  261. }
  262. public void 看()
  263. {
  264. StartCoroutine(電簽清單());
  265. rawImage1.texture = Texture2D.blackTexture;
  266. 寫面板.SetActive(false);
  267. 看面板.SetActive(true);
  268. }
  269. public void 寫()
  270. {
  271. 寫面板.SetActive(true);
  272. 看面板.SetActive(false);
  273. if (copy != null)
  274. {
  275. DeletetextCopy();
  276. }
  277. ClearCanvas();
  278. }
  279. public void DeletetextCopy()
  280. {
  281. // 刪除複製出來的物件
  282. foreach (GameObject i in copy)
  283. {
  284. Destroy(i);
  285. }
  286. copy.Clear();
  287. }
  288. public void 彈關()
  289. {
  290. 條跳面板.SetActive(false);
  291. ClearCanvas();
  292. }
  293. public void back()
  294. {
  295. change_Secen(1);
  296. }
  297. public void ClearCanvas()
  298. {
  299. Color[] clearColorArray = new Color[canvasTexture.width * canvasTexture.height];
  300. for (int i = 0; i < clearColorArray.Length; i++)
  301. {
  302. clearColorArray[i] = Color.clear;
  303. }
  304. canvasTexture.SetPixels(clearColorArray);
  305. canvasTexture.Apply();
  306. ddd();
  307. }
  308. public void change_Secen(int _screenNum)
  309. {
  310. SceneManager.LoadScene(_screenNum);
  311. }
  312. }