Nessuna descrizione
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.

login.cs 53KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.EventSystems;
  8. using Google;
  9. using System;
  10. using Unity.Collections.LowLevel.Unsafe;
  11. using System.Data;
  12. using UnityEngine.SceneManagement;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using Lunar;
  16. using HtmlAgilityPack;
  17. using UnityEngine.Networking;
  18. using SimpleJSON;
  19. using System.Data.SqlClient;
  20. public class login : MonoBehaviour
  21. {
  22. public Sprite playSprite;
  23. public Sprite pauseSprite;
  24. public GameObject buttonPrefab1;
  25. public Transform buttonParent1;
  26. public GameObject buttonceneter1;
  27. public ScrollRect buttonscroll1;
  28. public RectTransform contentRectTransform;
  29. [SerializeField] TextMeshProUGUI[] 新聞標題;
  30. [SerializeField] TextMeshProUGUI[] 新聞日期;
  31. [SerializeField] Button[] 新聞按鈕;
  32. [SerializeField] TextMeshProUGUI[] 氣象標題;
  33. [SerializeField] Button[] 氣象按鈕;
  34. [SerializeField] Button 綁定紐;
  35. [SerializeField] GameObject 編輯頁, 編輯頁1, 編輯頁2,編輯_pl,新增_pl, 首頁_pl,下排紐, 編輯頁3;
  36. [SerializeField] Button 刪除,登出,保存;
  37. [SerializeField] TextMeshProUGUI 編輯文字,信箱_lb, 今日日期, 農曆日期, 沖煞, 宜, 忌, 節氣, 侯;
  38. [SerializeField] GameObject 登入頁;
  39. [SerializeField] GameObject 登入1;
  40. [SerializeField] GameObject 登入2;
  41. [SerializeField] GameObject 輸入面;
  42. [SerializeField] TextMeshProUGUI 輸入問題;
  43. [SerializeField] GameObject 輸入1;
  44. [SerializeField] GameObject 輸入2;
  45. [SerializeField] GameObject 主畫面;
  46. [SerializeField] GameObject 貼文面;
  47. [SerializeField] GameObject 介紹面;
  48. [SerializeField] GameObject 更新面;
  49. [SerializeField] TMP_InputField 密碼;
  50. [SerializeField] GameObject 錯誤訊息2_lb;
  51. [SerializeField] TextMeshProUGUI UID;
  52. [SerializeField] TextMeshProUGUI 編輯問題;
  53. [SerializeField] TMP_InputField 初姓名;
  54. [SerializeField] TMP_InputField 初電話;
  55. [SerializeField] TMP_InputField 初地址;
  56. [SerializeField] TMP_InputField 姓名;
  57. [SerializeField] TMP_InputField 電話;
  58. [SerializeField] TMP_InputField 地址;
  59. [SerializeField] FirebaseManager firebaseManager;
  60. [SerializeField] TextMeshProUGUI 彈跳文字;
  61. [SerializeField] GameObject 彈跳面板;
  62. public CanvasScaler canvasScaler;
  63. float screenWidth;
  64. float screenHeight;
  65. private ScreenOrientation currentOrientation;
  66. [SerializeField] GameObject 讀取面板;
  67. [SerializeField] TextMeshProUGUI 讀取;
  68. [SerializeField] GameObject 刪除面板;
  69. public string WebUrl = "244610800088-mgl15q9eq32dothu32ug3epbaof9aevh.apps.googleusercontent.com";
  70. private string loadingText = "";
  71. private int dotCount = 0;
  72. private string 編輯判斷 = "";
  73. DataTable 貼文;
  74. private List<GameObject> posts = new List<GameObject>(); // 已生成的贴文列表
  75. private int currentIndex = 1;
  76. private float 總高 = 0;
  77. private bool isLoadingPost = false;
  78. private string 預刪除單號 = "";
  79. private string 介紹 = "";
  80. private string 圖片資料庫名稱 = "";
  81. private string 錄音資料庫名稱 = "";
  82. private bool 檢查 = false;
  83. UniWebView webView1;
  84. DataTable 網頁;
  85. void Start()
  86. {
  87. StartCoroutine(SetupLayout());
  88. //SQL_CommString.SQL_連線字串_圖片_SQL();
  89. //SQL_CommString.SQL_連線字串_錄音_SQL();
  90. //Main.Global.使用者UID="RmzmaqTfoFa6gwyZ6tqcBSgQLxd2";
  91. //Main.Global.使用者暱稱="脩";
  92. }
  93. IEnumerator SetupLayout()
  94. {
  95. StartCoroutine(AnimateText());
  96. 讀取面板.SetActive(true);
  97. Screen.autorotateToPortrait = false;
  98. Screen.orientation = ScreenOrientation.Portrait;
  99. screenWidth = Screen.width;
  100. screenHeight = Screen.height;
  101. float rate = 900.0f / 1900.0f;
  102. if (canvasScaler == null)
  103. {
  104. canvasScaler = GetComponent<CanvasScaler>();
  105. }
  106. if (Screen.width < Screen.height)
  107. {
  108. Main.Global.rate = screenWidth / screenHeight;
  109. Main.Global.rate_ = 1 / Main.Global.rate;
  110. if (Main.Global.rate > rate)
  111. {
  112. canvasScaler.matchWidthOrHeight = 1f;
  113. }
  114. else
  115. {
  116. canvasScaler.matchWidthOrHeight = 0f;
  117. }
  118. }
  119. else
  120. {
  121. Main.Global.rate_ = screenWidth / screenHeight;
  122. Main.Global.rate = 1 / Main.Global.rate_;
  123. if (Main.Global.rate > rate)
  124. {
  125. canvasScaler.matchWidthOrHeight = 1f;
  126. }
  127. else
  128. {
  129. canvasScaler.matchWidthOrHeight = 0f;
  130. }
  131. }
  132. // 等待一帧以确保布局更新已完成
  133. yield return null;
  134. 讀取sql();
  135. bool isConnected = ConnectToDatabase(Main.Global.伺服器);
  136. if (isConnected)
  137. {
  138. SQL_CommString.SQL_連線字串_正式_SQL(Main.Global.伺服器);
  139. SQL_game_sys.SQL_版本號();
  140. 讀取面板.SetActive(false);
  141. if (SQL_Module.dr.Read())
  142. {
  143. string 版本號1 = SQL_Module.dr["安卓系統版本號"].ToString();
  144. string 版本號2 = SQL_Module.dr["安卓送審版本號"].ToString();
  145. if (Application.version != 版本號1 && Application.version != 版本號2)
  146. {
  147. 登入頁.SetActive(false);
  148. 主畫面.SetActive(false);
  149. 貼文面.SetActive(false);
  150. 介紹面.SetActive(false);
  151. 更新面.SetActive(true);
  152. }
  153. else
  154. {
  155. Debug.Log(貼文面.GetComponent<RectTransform>().rect.width);
  156. if (Main.Global.回貼)
  157. {
  158. 登入頁.SetActive(false);
  159. 主畫面.SetActive(false);
  160. 貼文面.SetActive(true);
  161. 介紹面.SetActive(false);
  162. 更新面.SetActive(false);
  163. 下排紐.SetActive(true);
  164. 編輯_pl.SetActive(false);
  165. 新增_pl.SetActive(true);
  166. 首頁_pl.SetActive(true);
  167. }
  168. else
  169. {
  170. 登入頁.SetActive(true);
  171. 主畫面.SetActive(false);
  172. 貼文面.SetActive(false);
  173. 介紹面.SetActive(false);
  174. 更新面.SetActive(false);
  175. 登入1.SetActive(true);
  176. 登入2.SetActive(false);
  177. 下排紐.SetActive(false);
  178. }
  179. firebaseManager.auth.StateChanged += AuthStateChanged;
  180. }
  181. }
  182. DataSet ds = new();
  183. SQL_game_sys.SQL_網頁讀取();
  184. SQL_Module.da.Fill(ds);
  185. 網頁 = ds.Tables[0];
  186. }
  187. else
  188. {
  189. 彈跳面板.SetActive(true);
  190. 讀取面板.SetActive(false);
  191. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  192. 登入頁.SetActive(false);
  193. 主畫面.SetActive(false);
  194. 貼文面.SetActive(false);
  195. 介紹面.SetActive(false);
  196. 更新面.SetActive(false);
  197. 下排紐.SetActive(false);
  198. }
  199. }
  200. public bool ConnectToDatabase(string server)
  201. {
  202. string ConString = "server=" + server + ",4567;database=小農日記;uid=b70340;pwd=Lee0911274990;Connect Timeout=5";
  203. try
  204. {
  205. // 尝试连接数据库
  206. using (SqlConnection connection = new SqlConnection(ConString))
  207. {
  208. connection.Open();
  209. // 如果连接成功,则返回 true
  210. return true;
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. // 如果连接失败,则在控制台中输出异常信息
  216. Debug.Log("连接到数据库时出现异常:" + ex.Message);
  217. // 返回 false 表示连接失败
  218. return false;
  219. }
  220. }
  221. public void 讀取sql()
  222. {
  223. string connectionString = "Data Source=mssql-147629-0.cloudclusters.net,12711;Initial Catalog=GCM-IP-DATE02;User ID=GCMIP02;Password=F52dfgS5ZVD";
  224. using (SqlConnection connection = new SqlConnection(connectionString))
  225. {
  226. try
  227. {
  228. connection.Open();
  229. string query = "SELECT 客戶,IP FROM 客戶IP讀取 Where 客戶=N'菩芸伺服器'";
  230. using (SqlCommand command = new SqlCommand(query, connection))
  231. {
  232. using (SqlDataReader reader = command.ExecuteReader())
  233. {
  234. while (reader.Read()) // 假設只取前兩筆資料
  235. {
  236. // 取得類別和位址資料
  237. Main.Global.伺服器 = reader["IP"].ToString();
  238. }
  239. }
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. Debug.LogError("Error connecting to the database: " + ex.Message);
  245. 彈跳面板.SetActive(true);
  246. 讀取面板.SetActive(false);
  247. 登入頁.SetActive(false);
  248. 主畫面.SetActive(false);
  249. 貼文面.SetActive(false);
  250. 介紹面.SetActive(false);
  251. 更新面.SetActive(false);
  252. 下排紐.SetActive(false);
  253. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  254. }
  255. finally
  256. {
  257. connection.Close();
  258. }
  259. }
  260. }
  261. public void 離開APP()
  262. {
  263. Application.Quit();
  264. }
  265. private void 家網頁(string 網址)
  266. {
  267. var webViewGameObject = new GameObject("UniWebView");
  268. webView1 = webViewGameObject.AddComponent<UniWebView>();
  269. webView1.Load(網址);
  270. webView1.Frame = new Rect(0, 0, screenWidth, screenHeight-150);
  271. webView1.Show();
  272. }
  273. IEnumerator AnimateText()
  274. {
  275. string qqq = 讀取.text.ToString();
  276. while (true)
  277. {
  278. yield return new WaitForSeconds(0.5f);
  279. if (dotCount < 3)
  280. {
  281. loadingText += ".";
  282. dotCount++;
  283. }
  284. else
  285. {
  286. loadingText = "";
  287. dotCount = 0;
  288. }
  289. 讀取.text = qqq+loadingText;
  290. }
  291. }
  292. private void post計算()
  293. {
  294. 總高= 0;
  295. foreach (var buttonObj in posts)
  296. {
  297. RectTransform panelRectTransform = buttonObj.GetComponent<RectTransform>(); // 获取 RectTransform
  298. Vector2 panelSizeDelta = panelRectTransform.sizeDelta; // 获取大小
  299. 總高 += panelSizeDelta.y; // 加总大小
  300. }
  301. RectTransform contentRectTransform = buttonParent1.GetComponent<RectTransform>();
  302. contentRectTransform.sizeDelta = new Vector2(contentRectTransform.sizeDelta.x, 總高);
  303. }
  304. void OnScrollValueChanged(Vector2 value)
  305. {
  306. if (isLoadingPost)
  307. {
  308. return; // 如果正在加载贴文,则退出方法
  309. }
  310. //Debug.Log(buttonscroll1.velocity.y);
  311. //Debug.Log(buttonscroll1.normalizedPosition.y);
  312. if (buttonscroll1.velocity.y > 0 && buttonscroll1.normalizedPosition.y > 0.9f && currentIndex > 1)
  313. {
  314. // 向上滚动,生成新的贴文并卸载底部的贴文
  315. bool 首位 = true;
  316. isLoadingPost = true;
  317. RemovePost(首位);
  318. currentIndex--;
  319. CreatePost(currentIndex - 1, 首位);
  320. post計算();
  321. isLoadingPost = false;
  322. }
  323. else if (buttonscroll1.velocity.y < 0 && buttonscroll1.normalizedPosition.y < 0.1f && currentIndex < GetTotalPostsCount() - 2)
  324. {
  325. // 向下滚动,生成新的贴文并卸载顶部的贴文
  326. bool 首位 = false;
  327. isLoadingPost = true;
  328. RemovePost(首位);
  329. currentIndex++;
  330. CreatePost(currentIndex+1, 首位);
  331. post計算();
  332. isLoadingPost = false;
  333. }
  334. Vector2 newPosition = buttonscroll1.content.anchoredPosition;
  335. }
  336. void CreatePost(int i, bool 首位)
  337. {
  338. if (i < 0 || i >= GetTotalPostsCount())
  339. return;
  340. // 从预制体创建贴文对象
  341. GameObject buttonObj = Instantiate(buttonPrefab1, buttonParent1);
  342. buttonObj.name = "Post_" + i;
  343. ScrollRect scroll = buttonObj.transform.Find("相片")?.GetComponent<ScrollRect>();
  344. RectTransform content = scroll.content;
  345. Image copy = content.Find("圖")?.GetComponent<Image>();
  346. 圖片資料庫名稱=貼文.Rows[i][12].ToString();
  347. 錄音資料庫名稱=貼文.Rows[i][13].ToString();
  348. SQL_CommString.SQL_連線字串_錄音_SQL(Main.Global.伺服器,錄音資料庫名稱);
  349. SQL_CommString.SQL_連線字串_圖片_SQL(Main.Global.伺服器,圖片資料庫名稱);
  350. 圖片讀取(貼文.Rows[i][0].ToString(), copy, content, scroll);
  351. copy.gameObject.SetActive(false);
  352. TextMeshProUGUI count = buttonObj.transform.Find("相片數")?.GetComponent<TextMeshProUGUI>();
  353. PhotoScrollView scrollView = scroll.AddComponent<PhotoScrollView>();
  354. scrollView.largeScrollView =buttonscroll1;
  355. scrollView.smallScrollView =scroll; // 设置 ScrollRect 变量
  356. scrollView.pageIndicatorText = count; // 设置 TextMeshProUGUI 变量
  357. scrollView.swipeThreshold = 200f; // 设置 swipeThreshold 变量
  358. scrollView.snapSpeed = 10f; // 设置 snapSpeed 变量
  359. scrollView.threshold = 100f;
  360. scrollView.InitAfterMainScriptStarts();
  361. TextMeshProUGUI name = buttonObj.transform.Find("暱稱")?.GetComponent<TextMeshProUGUI>();
  362. TextMeshProUGUI place = buttonObj.transform.Find("地點")?.GetComponent<TextMeshProUGUI>();
  363. TextMeshProUGUI time = buttonObj.transform.Find("時間")?.GetComponent<TextMeshProUGUI>();
  364. TextMeshProUGUI detail = buttonObj.transform.Find("內容")?.GetComponent<TextMeshProUGUI>();
  365. Image radio = buttonObj.transform.Find("顯示錄音")?.GetComponent<Image>();
  366. TextMeshProUGUI rtime = radio.transform.Find("時間")?.GetComponent<TextMeshProUGUI>();
  367. Button bt = radio.transform.Find("按鈕")?.GetComponent<Button>();
  368. Slider timeSlider = radio.transform.Find("時間軸")?.GetComponent<Slider>();
  369. Button bt1 = buttonObj.transform.Find("刪除")?.GetComponent<Button>();
  370. int iii = i;
  371. bt1.onClick.AddListener(() =>
  372. {
  373. 預刪除單號=貼文.Rows[iii][0].ToString();
  374. 貼文面.SetActive(false);
  375. 刪除面板.SetActive(true);
  376. });
  377. name.text =Main.Global.使用者暱稱;
  378. place.text=貼文.Rows[i][9].ToString()+貼文.Rows[i][10].ToString();
  379. time.text=ConvertToCustomFormat(貼文.Rows[i][1].ToString());
  380. float contentHeight = 0f;
  381. if (貼文.Rows[i][8].ToString() != "")
  382. {
  383. radio.gameObject.SetActive(true);
  384. AudioPlayer audio = buttonObj.AddComponent<AudioPlayer>();
  385. audio.timeText = rtime;
  386. audio.progressBar = timeSlider;
  387. audio.playPauseButton = bt;
  388. audio.playSprite = playSprite;
  389. audio.pauseSprite = pauseSprite;
  390. SQL_game_sys.SQL_讀取錄音(貼文.Rows[i][0].ToString());
  391. if (SQL_Module.dr.Read())
  392. {
  393. byte[] imageData = (byte[])SQL_Module.dr["錄音檔"];
  394. audio.LoadAudioClip(imageData);
  395. }
  396. if (貼文.Rows[i][2].ToString() != "")
  397. {
  398. RectTransform contentRectTransform = detail.rectTransform;
  399. contentRectTransform.anchoredPosition += new Vector2(0, -100);
  400. }
  401. contentHeight += 100f;
  402. }
  403. else
  404. {
  405. radio.gameObject.SetActive(false);
  406. }
  407. if (貼文.Rows[i][2].ToString() != "")
  408. {
  409. detail.gameObject.SetActive(true);
  410. detail.text =貼文.Rows[i][2].ToString();
  411. contentHeight += detail.preferredHeight;
  412. }
  413. else
  414. {
  415. detail.gameObject.SetActive(false);
  416. }
  417. RectTransform panelRectTransform = buttonObj.GetComponent<RectTransform>();
  418. RectTransform parentRectTransform = 貼文面.GetComponent<RectTransform>();
  419. // 获取当前预制体的大小
  420. Vector2 panelSizeDelta = panelRectTransform.sizeDelta;
  421. // 将预制体的大小设置为与父对象一样宽,高度增加 contentHeight
  422. panelRectTransform.sizeDelta = new Vector2(parentRectTransform.rect.width, panelSizeDelta.y + contentHeight);
  423. // 设置 anchoring 和 pivot,使得预制体顶部对齐父对象的顶部
  424. panelRectTransform.anchorMin = new Vector2(0, 1);
  425. panelRectTransform.anchorMax = new Vector2(1, 1);
  426. panelRectTransform.pivot = new Vector2(0.5f, 1);
  427. // 将生成的贴文添加到列表中
  428. if (首位)
  429. {
  430. buttonObj.transform.SetAsFirstSibling();
  431. posts.Insert(0, buttonObj);
  432. }
  433. else
  434. {
  435. posts.Add(buttonObj);
  436. buttonObj.transform.SetAsLastSibling();
  437. }
  438. }
  439. // 卸载贴文
  440. void RemovePost(bool 首位)
  441. {
  442. if (首位)
  443. {
  444. int lastIndex = posts.Count - 1;
  445. RectTransform panelRectTransform = posts[lastIndex].GetComponent<RectTransform>(); // 获取 RectTransform
  446. Vector2 panelSizeDelta = panelRectTransform.sizeDelta;
  447. Destroy(posts[lastIndex]); // 销毁末位的游戏对象
  448. posts.RemoveAt(lastIndex);
  449. Vector2 newPosition = buttonscroll1.content.anchoredPosition;
  450. newPosition.y += panelSizeDelta.y;
  451. buttonscroll1.content.anchoredPosition = newPosition;
  452. }
  453. else
  454. {
  455. RectTransform panelRectTransform = posts[0].GetComponent<RectTransform>(); // 获取 RectTransform
  456. Vector2 panelSizeDelta = panelRectTransform.sizeDelta;
  457. Destroy(posts[0]); // 销毁首位的游戏对象
  458. posts.RemoveAt(0);
  459. Vector2 newPosition = buttonscroll1.content.anchoredPosition;
  460. newPosition.y -= panelSizeDelta.y;
  461. buttonscroll1.content.anchoredPosition = newPosition;
  462. }
  463. }
  464. // 获取总贴文数量
  465. int GetTotalPostsCount()
  466. {
  467. // 这里需要根据实际情况获取贴文的总数
  468. return 貼文.Rows.Count;
  469. }
  470. string FormatTime(float time)
  471. {
  472. // 格式化時間
  473. int minutes = Mathf.FloorToInt(time / 60);
  474. int seconds = Mathf.FloorToInt(time % 60);
  475. return string.Format("{0:00}:{1:00}", minutes, seconds);
  476. }
  477. private string ConvertToCustomFormat(string dateTimeString)
  478. {
  479. // 解析年、月、日、时、分、秒
  480. string year = dateTimeString.Substring(0, 4);
  481. string month = dateTimeString.Substring(4, 2);
  482. string day = dateTimeString.Substring(6, 2);
  483. string hour = dateTimeString.Substring(9, 2);
  484. string minute = dateTimeString.Substring(11, 2);
  485. string second = dateTimeString.Substring(13, 2);
  486. // 使用解析的部分来构建所需的日期时间格式
  487. string formattedDateTime = $"{year}/{month}/{day}-{hour}:{minute}:{second}";
  488. return formattedDateTime;
  489. }
  490. private void 圖片讀取(string 單號, Image copy, RectTransform content, ScrollRect 滾動框)
  491. {
  492. DataSet ds1 = new();
  493. SQL_game_sys.SQL_讀取圖片(單號);
  494. SQL_Module.da.Fill(ds1);
  495. DataTable data = ds1.Tables[0];
  496. float sum = 0;
  497. if (data.Rows.Count > 1)
  498. {
  499. 滾動框.horizontal = true;
  500. }
  501. else
  502. {
  503. 滾動框.horizontal = false;
  504. }
  505. for (int i = 0; i < data.Rows.Count; i++)
  506. {
  507. GameObject buttonObj = Instantiate(copy.gameObject) as GameObject;
  508. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(sum, 0);
  509. buttonObj.GetComponent<RectTransform>().sizeDelta = new Vector2(滾動框.GetComponent<RectTransform>().rect.width, buttonObj.GetComponent<RectTransform>().sizeDelta.y);
  510. buttonObj.transform.SetParent(content, false);
  511. RawImage image = buttonObj.GetComponentInChildren<RawImage>();
  512. byte[] imageData = (byte[])data.Rows[i][1];
  513. Texture2D texture = new Texture2D(1, 1);
  514. texture.LoadImage(imageData);
  515. image.texture = texture;
  516. if (texture.width > texture.height)
  517. {
  518. // 照片的寬度大於高度,按照寬度等比例縮放
  519. float scaleFactor = 滾動框.GetComponent<RectTransform>().rect.width / texture.width;
  520. image.rectTransform.sizeDelta = new Vector2(滾動框.GetComponent<RectTransform>().rect.width, texture.height * scaleFactor);
  521. }
  522. else
  523. {
  524. // 照片的高度大於寬度,按照高度等比例縮放
  525. float scaleFactor = 滾動框.GetComponent<RectTransform>().rect.width / texture.height;
  526. image.rectTransform.sizeDelta = new Vector2(texture.width * scaleFactor, 880);
  527. }
  528. sum += 滾動框.GetComponent<RectTransform>().rect.width;
  529. }
  530. content.sizeDelta = new Vector2(sum, content.sizeDelta.y);
  531. }
  532. public async void SignInWithGoogle()
  533. {
  534. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="";
  535. try
  536. {
  537. GoogleSignIn.Configuration = new GoogleSignInConfiguration
  538. {
  539. WebClientId = WebUrl,
  540. RequestIdToken = true
  541. };
  542. var googlesigntask = GoogleSignIn.DefaultInstance.SignIn();
  543. await googlesigntask;
  544. if (googlesigntask.IsFaulted)
  545. {
  546. return;
  547. }
  548. else
  549. {
  550. GoogleSignInUser signInUser = googlesigntask.Result;
  551. string googleIdToken = signInUser.IdToken;
  552. string googleAccessToken = signInUser.AuthCode;
  553. string rdpassword = GenerateRandomPassword();
  554. string password = await CheckAndRegister_google(rdpassword);
  555. string res = await firebaseManager.Google登入(googleIdToken, googleAccessToken,password);
  556. if (res[..1] =="0")
  557. {
  558. 查詢暱稱();
  559. }
  560. else if (res[..1] =="1")
  561. {
  562. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=res[2..];
  563. }
  564. }
  565. }
  566. catch (Exception ex)
  567. {
  568. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=ex.Message;
  569. GoogleSignIn.DefaultInstance?.SignOut();
  570. }
  571. }
  572. public async void 登入(){
  573. //SQL_game_sys.SQL_使用者_是否APPLE登入(密碼.text);
  574. //if(SQL_Module.dr.Read()){
  575. // string str;
  576. // if(SQL_Module.dr[0].ToString()=="True"){
  577. // str = SQL_Module.dr[1].ToString();
  578. // }else{
  579. // str = 密碼.text+"@gcmsmart.com";
  580. // }
  581. // Debug.Log(str);
  582. // string res = await firebaseManager.登入(str,密碼.text);
  583. // if (res[..1] =="0"){
  584. // 查詢暱稱();
  585. // }
  586. // else if (res[..1] =="1"){
  587. // 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=res[2..];
  588. // }
  589. //}
  590. string str = 密碼.text+"@gcmsmart.com";
  591. string res = await firebaseManager.登入(str, 密碼.text);
  592. if (res[..1] =="0")
  593. {
  594. 查詢暱稱();
  595. }
  596. else if (res[..1] =="1")
  597. {
  598. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=res[2..];
  599. }
  600. }
  601. public void ID登入()
  602. {
  603. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="";
  604. 密碼.text="";
  605. 登入1.SetActive(false);
  606. 登入2.SetActive(true);
  607. }
  608. public void ID登入Y()
  609. {
  610. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="";
  611. if (密碼.text != "")
  612. {
  613. SQL_game_sys.SQL_使用者_重複搜尋(密碼.text);
  614. if (SQL_Module.dr.Read())
  615. {
  616. 登入();
  617. }
  618. else
  619. {
  620. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="查無ID";
  621. }
  622. }
  623. else
  624. {
  625. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="未輸入ID";
  626. }
  627. }
  628. public void ID登入N()
  629. {
  630. 登入1.SetActive(true);
  631. 登入2.SetActive(false);
  632. }
  633. public async void 註冊()
  634. {
  635. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text="";
  636. string randomPassword = GenerateRandomPassword();
  637. Debug.Log(randomPassword);
  638. string res = await CheckAndRegister(randomPassword);
  639. if (res[..1] =="0")
  640. {
  641. 查詢暱稱();
  642. }
  643. else if (res[..1] =="1" || res[..1] =="2")
  644. {
  645. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=res[2..];
  646. }
  647. }
  648. public string GenerateRandomPassword()
  649. {
  650. const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  651. var random = new System.Random();
  652. var password = new string(Enumerable.Repeat(chars, 10)
  653. .Select(s => s[random.Next(s.Length)]).ToArray());
  654. return password;
  655. }
  656. public async Task<string> CheckAndRegister(string password)
  657. {
  658. // 檢查密碼是否已存在
  659. SQL_game_sys.SQL_使用者_重複搜尋(password);
  660. if (SQL_Module.dr.Read())
  661. {
  662. // 如果密碼已存在,則生成新的密碼並進行檢查
  663. return await CheckAndRegister(GenerateRandomPassword());
  664. }
  665. else
  666. {
  667. string email = password + "@gcmsmart.com"; // 使用密碼作為電子郵件地址的一部分
  668. return await firebaseManager.Register(email, password);
  669. }
  670. }
  671. public async Task<string> CheckAndRegister_google(string password)
  672. {
  673. // 檢查密碼是否已存在
  674. SQL_game_sys.SQL_使用者_重複搜尋(password);
  675. if (SQL_Module.dr.Read())
  676. {
  677. // 如果密碼已存在,則生成新的密碼並進行檢查
  678. return await CheckAndRegister_google(GenerateRandomPassword());
  679. }
  680. else
  681. {
  682. return password;
  683. }
  684. }
  685. public void 登出使用者()
  686. {
  687. 編輯頁1.SetActive(false);
  688. 編輯頁2.SetActive(true);
  689. 編輯文字.text="是否要登出?";
  690. 編輯判斷 = "登出";
  691. }
  692. public async void 刪除使用者()
  693. {
  694. 編輯頁1.SetActive(false);
  695. 編輯頁2.SetActive(true);
  696. 編輯文字.text="是否要刪除?";
  697. 編輯判斷 = "刪除";
  698. }
  699. private void 查詢暱稱()
  700. {
  701. SQL_game_sys.SQL_使用者_查詢暱稱(firebaseManager.user.UserId);
  702. if (SQL_Module.dr.Read())
  703. {
  704. 姓名.text=SQL_Module.dr["暱稱"].ToString();
  705. 電話.text=SQL_Module.dr["電話"].ToString();
  706. 地址.text=SQL_Module.dr["地址"].ToString();
  707. UID.text=SQL_Module.dr["驗證碼"].ToString();
  708. if (SQL_Module.dr["暱稱"].ToString() == "" || SQL_Module.dr["電話"].ToString() == "" || SQL_Module.dr["地址"].ToString() == "")
  709. {
  710. 檢查 =false;
  711. }
  712. else
  713. {
  714. 檢查 =true;
  715. }
  716. Debug.Log(檢查);
  717. if (SQL_Module.dr["GOOGLE登入"].ToString() == "False")
  718. {
  719. 綁定紐.interactable=true;
  720. }
  721. else
  722. {
  723. 綁定紐.interactable=false;
  724. }
  725. Main.Global.使用者暱稱=SQL_Module.dr["暱稱"].ToString();
  726. Main.Global.使用者ID=SQL_Module.dr["驗證碼"].ToString();
  727. Main.Global.使用者UID=SQL_Module.dr["使用者UID"].ToString();
  728. if (SQL_Module.dr["重設密碼"].ToString() == "False")
  729. {
  730. 綁定紐.interactable=true;
  731. }
  732. else
  733. {
  734. 綁定紐.interactable=false;
  735. }
  736. }
  737. }
  738. public void 使用者_顯示資料(){
  739. Debug.Log(firebaseManager.user.UserId);
  740. Debug.Log(firebaseManager.user.Email);
  741. 信箱_lb.text = firebaseManager.user.Email;
  742. }
  743. public void 使用者_修改暱稱(){
  744. 編輯頁1.SetActive(false);
  745. 編輯頁2.SetActive(true);
  746. 編輯文字.text="是否要修改?";
  747. 編輯判斷 = "修改";
  748. }
  749. public void 修改暱稱返回()
  750. {
  751. 編輯頁1.SetActive(true);
  752. 編輯頁2.SetActive(false);
  753. 編輯頁3.SetActive(false);
  754. }
  755. public void 使用者_ID綁定(){
  756. 編輯頁1.SetActive(false);
  757. 編輯頁2.SetActive(true);
  758. 編輯文字.text="是否要綁定ID?";
  759. 編輯判斷 = "ID綁定";
  760. }
  761. public async void 編輯是(){
  762. if (編輯判斷 == "修改"){
  763. 讀取面板.SetActive(true);
  764. SQL_game_sys.SQL_使用者_修改暱稱(firebaseManager.user.UserId, 姓名.text, 電話.text, 地址.text);
  765. 查詢暱稱();
  766. 編輯頁1.SetActive(true);
  767. 編輯頁2.SetActive(false);
  768. 編輯文字.text="";
  769. 編輯判斷 = "";
  770. 讀取面板.SetActive(false);
  771. }else if (編輯判斷 == "登出"){
  772. firebaseManager.登出();
  773. #if UNITY_ANDROID && !UNITY_EDITOR
  774. GoogleSignIn.DefaultInstance?.SignOut();
  775. #endif
  776. }
  777. else if (編輯判斷 == "刪除"){
  778. 讀取面板.SetActive(true);
  779. SQL_game_sys.SQL_使用者_刪除(firebaseManager.user.UserId);
  780. DataSet ds = new();
  781. SQL_game_sys.SQL_使用者_查詢貼文(firebaseManager.user.UserId);
  782. SQL_Module.da.Fill(ds);
  783. DataTable 貼文 = ds.Tables[0];
  784. for (int i = 0; i<貼文.Rows.Count; i++){
  785. string str = 貼文.Rows[i][0].ToString();
  786. 圖片資料庫名稱=貼文.Rows[i][12].ToString();
  787. 錄音資料庫名稱=貼文.Rows[i][13].ToString();
  788. SQL_CommString.SQL_連線字串_錄音_SQL(Main.Global.伺服器, 錄音資料庫名稱);
  789. SQL_CommString.SQL_連線字串_圖片_SQL(Main.Global.伺服器, 圖片資料庫名稱);
  790. SQL_game_sys.SQL_使用者_刪除圖片(str);
  791. SQL_game_sys.SQL_使用者_刪除錄音(str);
  792. }
  793. SQL_game_sys.SQL_使用者_刪除所有貼文(firebaseManager.user.UserId);
  794. string res = await firebaseManager.刪除使用者();
  795. if (res[..1] =="0"){}else if (res[..1] =="1"){
  796. 錯誤訊息2_lb.SetActive(true);
  797. 錯誤訊息2_lb.GetComponent<TextMeshProUGUI>().text=res[2..];
  798. }
  799. 讀取面板.SetActive(false);
  800. 編輯頁.SetActive(false);
  801. }
  802. else if(編輯判斷=="ID綁定"){
  803. SQL_game_sys.SQL_使用者重設密碼(firebaseManager.user.UserId);
  804. if (SQL_Module.dr.Read())
  805. {
  806. if (SQL_Module.dr[0].ToString()=="False")
  807. {
  808. string res = await firebaseManager.重設EMAIL(Main.Global.使用者ID+"@gcmsmart.com");
  809. firebaseManager.重設密碼(Main.Global.使用者ID);
  810. SQL_game_sys.SQL_使用者重設密碼完畢(firebaseManager.user.UserId);
  811. }
  812. }
  813. 編輯頁1.SetActive(true);
  814. 編輯頁2.SetActive(false);
  815. 編輯文字.text="";
  816. 編輯判斷 = "";
  817. 查詢暱稱();
  818. }
  819. }
  820. public void 輸入確認()
  821. {
  822. if (初姓名.text != "" && 初電話.text != "" && 初地址.text != "" )
  823. {
  824. 登入頁.SetActive(false);
  825. 主畫面.SetActive(true);
  826. 下排紐.SetActive(true);
  827. 編輯_pl.SetActive(true);
  828. 新增_pl.SetActive(false);
  829. 首頁_pl.SetActive(true);
  830. 輸入面.SetActive(false);
  831. SQL_game_sys.SQL_使用者_修改暱稱(firebaseManager.user.UserId, 初姓名.text, 初電話.text, 初地址.text);
  832. 查詢暱稱();
  833. }
  834. else
  835. {
  836. 輸入1.SetActive(false);
  837. 輸入2.SetActive(true);
  838. 輸入問題.text ="輸入資訊皆不能為空白";
  839. }
  840. }
  841. public void 再次輸入()
  842. {
  843. 輸入1.SetActive(true);
  844. 輸入2.SetActive(false);
  845. }
  846. public void 刪除是(){
  847. 讀取面板.SetActive(true);
  848. 貼文面.SetActive(true);
  849. SQL_game_sys.SQL_使用者_刪除貼文(預刪除單號);
  850. SQL_game_sys.SQL_使用者_刪除圖片(預刪除單號);
  851. SQL_game_sys.SQL_使用者_刪除錄音(預刪除單號);
  852. 初始貼文讀取();
  853. 讀取面板.SetActive(false);
  854. 刪除面板.SetActive(false);
  855. }
  856. public void 刪除否(){
  857. 貼文面.SetActive(true);
  858. 刪除面板.SetActive(false);
  859. 預刪除單號="";
  860. }
  861. public void 編輯否(){
  862. 編輯頁1.SetActive(true);
  863. 編輯頁2.SetActive(false);
  864. 編輯文字.text="";
  865. 編輯判斷 = "";
  866. }
  867. public void 開啟編輯(){
  868. 編輯頁.SetActive(true);
  869. 查詢暱稱();
  870. 編輯頁1.SetActive(true);
  871. 編輯頁2.SetActive(false);
  872. 主畫面.SetActive(false);
  873. 編輯文字.text="";
  874. 編輯判斷 = "";
  875. 編輯_pl.SetActive(true);
  876. 新增_pl.SetActive(false);
  877. 首頁_pl.SetActive(true);
  878. }
  879. public void 回主畫面(){
  880. 編輯頁.SetActive(false);
  881. 編輯文字.text="";
  882. 編輯判斷 = "";
  883. 貼文面.SetActive(false);
  884. 主畫面.SetActive(true);
  885. 介紹面.SetActive(false);
  886. 編輯_pl.SetActive(true);
  887. 新增_pl.SetActive(false);
  888. 首頁_pl.SetActive(true);
  889. 介紹 = "";
  890. Main.Global.回貼 =false;
  891. if (webView1 != null)
  892. {
  893. Destroy(webView1.gameObject);
  894. }
  895. }
  896. public void 介紹1()
  897. {
  898. 主畫面.SetActive(false);
  899. 介紹面.SetActive(true);
  900. 介紹 = "協會";
  901. for (int i = 0; i < 網頁.Rows.Count; i++)
  902. {
  903. if (網頁.Rows[i][0].ToString()=="協會")
  904. {
  905. 家網頁(網頁.Rows[i][1].ToString());
  906. }
  907. }
  908. 編輯_pl.SetActive(false);
  909. 新增_pl.SetActive(false);
  910. 首頁_pl.SetActive(true);
  911. }
  912. public void 介紹2()
  913. {
  914. 主畫面.SetActive(false);
  915. 介紹面.SetActive(true);
  916. 介紹 = "行動";
  917. for (int i = 0; i < 網頁.Rows.Count; i++)
  918. {
  919. if (網頁.Rows[i][0].ToString()=="行動")
  920. {
  921. 家網頁(網頁.Rows[i][1].ToString());
  922. }
  923. }
  924. 編輯_pl.SetActive(false);
  925. 新增_pl.SetActive(false);
  926. 首頁_pl.SetActive(true);
  927. }
  928. public void 介紹3()
  929. {
  930. 主畫面.SetActive(false);
  931. 介紹面.SetActive(true);
  932. 介紹 = "分解菌";
  933. for (int i = 0; i < 網頁.Rows.Count; i++)
  934. {
  935. if (網頁.Rows[i][0].ToString()=="分解菌")
  936. {
  937. 家網頁(網頁.Rows[i][1].ToString());
  938. }
  939. }
  940. 編輯_pl.SetActive(false);
  941. 新增_pl.SetActive(false);
  942. 首頁_pl.SetActive(true);
  943. }
  944. public void 開啟貼文()
  945. {
  946. Main.Global.回貼 =true;
  947. 貼文面.SetActive(true);
  948. 主畫面.SetActive(false);
  949. 編輯_pl.SetActive(false);
  950. 新增_pl.SetActive(true);
  951. 首頁_pl.SetActive(true);
  952. 初始貼文讀取();
  953. }
  954. public void 新增貼文(){
  955. Main.change_Secen(1);
  956. }
  957. public void 快速前往()
  958. {
  959. string url = "https://play.google.com/store/apps/details?id=com.gcmsmart.sfj"; // 简单示例中使用相同的URL,你可以根据需要修改为不同的URL
  960. Application.OpenURL(url);
  961. }
  962. void AuthStateChanged(object sender, System.EventArgs eventArgs)
  963. {
  964. if (firebaseManager.user == null)
  965. {
  966. currentIndex = 1;
  967. if (posts != null)
  968. {
  969. DeletetextCopies(posts);
  970. }
  971. UID.text="";
  972. 姓名.text="";
  973. 電話.text="";
  974. 地址.text="";
  975. 檢查 = false;
  976. Main.Global.使用者暱稱="";
  977. Main.Global.使用者ID="";
  978. Main.Global.使用者UID="";
  979. 登入頁.SetActive(true);
  980. 主畫面.SetActive(false);
  981. 登入1.SetActive(true);
  982. 登入2.SetActive(false);
  983. 編輯頁.SetActive(false);
  984. 下排紐.SetActive(false);
  985. }
  986. else
  987. {
  988. UID.text="";
  989. 姓名.text="";
  990. 電話.text="";
  991. 地址.text="";
  992. 讀取面板.SetActive(true);
  993. 查詢暱稱();
  994. if (檢查)
  995. {
  996. 登入頁.SetActive(false);
  997. 主畫面.SetActive(true);
  998. 下排紐.SetActive(true);
  999. 編輯_pl.SetActive(true);
  1000. 新增_pl.SetActive(false);
  1001. 首頁_pl.SetActive(true);
  1002. }
  1003. else
  1004. {
  1005. 登入頁.SetActive(false);
  1006. 主畫面.SetActive(false);
  1007. 輸入面.SetActive(true);
  1008. 輸入1.SetActive(true);
  1009. 輸入2.SetActive(false);
  1010. 初姓名.text = "";
  1011. 初電話.text = "";
  1012. 初地址.text = "";
  1013. 下排紐.SetActive(false);
  1014. }
  1015. Main.Global.使用者UID=firebaseManager.user.UserId;
  1016. 讀取日期();
  1017. string newsURL = "https://www.moa.gov.tw/theme_list.php?theme=news&sub_theme=agri";
  1018. string playlistId = "PL9GjONJu_nMKBwF1g5Uu67qqMiyvwJD94";
  1019. string apiKey = "AIzaSyBx8We1GYl75q5d4yZIaeBA7NcZkNHiByA";
  1020. StartCoroutine(GetPlaylistVideos(playlistId, apiKey));
  1021. StartCoroutine(LoadNews(newsURL));
  1022. 讀取面板.SetActive(false);
  1023. if (Main.Global.回貼)
  1024. {
  1025. 開啟貼文();
  1026. }
  1027. }
  1028. }
  1029. IEnumerator GetPlaylistVideos(string playlistId, string apiKey)
  1030. {
  1031. string url = $"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={playlistId}&key={apiKey}&maxResults=5";
  1032. UnityWebRequest request = UnityWebRequest.Get(url);
  1033. yield return request.SendWebRequest();
  1034. if (request.result == UnityWebRequest.Result.Success)
  1035. {
  1036. string json = request.downloadHandler.text;
  1037. JSONNode response = JSON.Parse(json);
  1038. JSONArray items = response["items"].AsArray;
  1039. int i = 0;
  1040. foreach (JSONNode item in items)
  1041. {
  1042. string title = item["snippet"]["title"].Value;
  1043. if (title == "Deleted video" || title == "Private video")
  1044. {
  1045. continue; // 跳过已删除的视频
  1046. }
  1047. string videoId = item["snippet"]["resourceId"]["videoId"].Value;
  1048. //string thumbnailUrl = item["snippet"]["thumbnails"]["default"]["url"].Value;
  1049. 氣象標題[i].text = title;
  1050. 氣象按鈕[i].onClick.AddListener(() => OpenWebPage1(videoId));
  1051. i++;
  1052. if (i >= 5)
  1053. {
  1054. break; // 达到5个视频项时退出循环
  1055. }
  1056. }
  1057. while (i < 5 && response["nextPageToken"] != null)
  1058. {
  1059. // 继续请求下一页数据
  1060. string nextPageToken = response["nextPageToken"].Value;
  1061. url = $"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={playlistId}&key={apiKey}&maxResults=5&pageToken={nextPageToken}";
  1062. UnityWebRequest nextRequest = UnityWebRequest.Get(url);
  1063. yield return nextRequest.SendWebRequest();
  1064. if (nextRequest.result == UnityWebRequest.Result.Success)
  1065. {
  1066. string nextPageJson = nextRequest.downloadHandler.text;
  1067. JSONNode nextPageResponse = JSON.Parse(nextPageJson);
  1068. JSONArray nextPageItems = nextPageResponse["items"].AsArray;
  1069. // 继续读取视频信息
  1070. foreach (JSONNode item in nextPageItems)
  1071. {
  1072. string title = item["snippet"]["title"].Value;
  1073. if (title == "Deleted video" || title == "Private video")
  1074. {
  1075. continue; // 跳过已删除的视频
  1076. }
  1077. string videoId = item["snippet"]["resourceId"]["videoId"].Value;
  1078. //string thumbnailUrl = item["snippet"]["thumbnails"]["default"]["url"].Value;
  1079. 氣象標題[i].text = title;
  1080. 氣象按鈕[i].onClick.AddListener(() => OpenWebPage1(videoId));
  1081. i++;
  1082. if (i >= 5)
  1083. {
  1084. break; // 达到5个视频项时退出循环
  1085. }
  1086. }
  1087. }
  1088. else
  1089. {
  1090. Debug.LogError("Failed to fetch next page of playlist videos. Error: " + nextRequest.error);
  1091. break; // 如果请求失败,则退出循环
  1092. }
  1093. }
  1094. }
  1095. else
  1096. {
  1097. Debug.LogError("Failed to fetch playlist videos. Error: " + request.error);
  1098. }
  1099. }
  1100. IEnumerator LoadNews(string url)
  1101. {
  1102. UnityWebRequest request = UnityWebRequest.Get(url);
  1103. yield return request.SendWebRequest();
  1104. if (request.result == UnityWebRequest.Result.Success)
  1105. {
  1106. string htmlContent = request.downloadHandler.text;
  1107. // 加载 HTML 内容到 HtmlDocument
  1108. HtmlDocument doc = new HtmlDocument();
  1109. doc.LoadHtml(htmlContent);
  1110. // 解析新闻信息
  1111. ParseNews(doc);
  1112. }
  1113. else
  1114. {
  1115. Debug.LogError("Failed to load news. Error: " + request.error);
  1116. }
  1117. }
  1118. void ParseNews(HtmlDocument doc)
  1119. {
  1120. var newsNodes = doc.DocumentNode.SelectNodes("//tbody/tr");
  1121. if (newsNodes != null)
  1122. {
  1123. int i = 0;
  1124. foreach (var node in newsNodes.Take(5)) // 只获取前5条新闻
  1125. {
  1126. var dateNode = node.SelectSingleNode(".//td[@data-title='發布日期']");
  1127. var titleNode = node.SelectSingleNode(".//td[@data-title='標題']//a");
  1128. if (dateNode != null && titleNode != null)
  1129. {
  1130. string date = dateNode.InnerText.Trim();
  1131. string title = titleNode.InnerText.Trim();
  1132. string href = titleNode.Attributes["href"].Value;
  1133. // 从链接中提取出新闻 ID
  1134. string id = ExtractIDFromURL(href);
  1135. 新聞標題[i].text =title;
  1136. 新聞日期[i].text =date;
  1137. 新聞按鈕[i].onClick.AddListener(() => OpenWebPage(id));
  1138. }
  1139. i+=1;
  1140. }
  1141. }
  1142. }
  1143. void OpenWebPage1(string id)
  1144. {
  1145. // 根据索引打开对应的网页
  1146. string url = "https://www.youtube.com/watch?v="+id; // 简单示例中使用相同的URL,你可以根据需要修改为不同的URL
  1147. //Application.OpenURL(url);
  1148. 家網頁(url);
  1149. 主畫面.SetActive(false);
  1150. 介紹面.SetActive(true);
  1151. 編輯_pl.SetActive(false);
  1152. 新增_pl.SetActive(false);
  1153. 首頁_pl.SetActive(true);
  1154. }
  1155. void OpenWebPage(string id)
  1156. {
  1157. // 根据索引打开对应的网页
  1158. string url = "https://www.moa.gov.tw/theme_data.php?theme=news&sub_theme=agri&id="+id; // 简单示例中使用相同的URL,你可以根据需要修改为不同的URL
  1159. //Application.OpenURL(url);
  1160. 家網頁(url);
  1161. 主畫面.SetActive(false);
  1162. 介紹面.SetActive(true);
  1163. 編輯_pl.SetActive(false);
  1164. 新增_pl.SetActive(false);
  1165. 首頁_pl.SetActive(true);
  1166. }
  1167. private string ExtractIDFromURL(string url)
  1168. {
  1169. // 使用 '/' 符号分割 URL
  1170. string[] parts = url.Split('/');
  1171. // 使用 '=' 符号分割最后一部分,取得 ID 部分
  1172. string[] idParts = parts.Last().Split('=');
  1173. // 返回 ID
  1174. return idParts.Last();
  1175. }
  1176. private void 讀取日期()
  1177. {
  1178. DateTime currentDate = DateTime.Now;
  1179. 今日日期.text = string.Format("今天是 {0} 年 {1:00} 月 {2:00} 日,{3}", currentDate.Year, currentDate.Month, currentDate.Day, currentDate.ToString("dddd"));
  1180. Lunar.Lunar lunar = Lunar.Lunar.FromDate(currentDate);
  1181. string 物侯 = lunar.WuHou;
  1182. string 幾侯 = lunar.Hou;
  1183. // 組合農曆日期
  1184. string lunarDate = $"{lunar.YearInGanZhi}年{lunar.MonthInChinese}月{lunar.DayInChinese}";
  1185. // 獲取宜忌、冲煞、節氣
  1186. string yi = GetListText(lunar.DayYi);
  1187. string ji = GetListText(lunar.DayJi);
  1188. string 冲干支 = lunar.DayChongGan +lunar.DayChong;
  1189. string 冲生肖 = lunar.DayChongShengXiao;
  1190. string age = Main.Global.CalculateAge(lunar.YearInGanZhi, 冲干支);
  1191. string shac = lunar.DaySha;
  1192. Lunar.JieQi asd = lunar.GetPrevJieQi(true);
  1193. 農曆日期.text =$"農曆 {lunarDate}";
  1194. 沖煞.text = $"冲 {冲生肖}({age}) 煞 {shac}方";
  1195. 宜.text = $"{yi}";
  1196. 忌.text = $"{ji}";
  1197. 節氣.text = $"{asd}•{物侯}";
  1198. 侯.text = $"{幾侯}";
  1199. }
  1200. string GetListText(List<string> list)
  1201. {
  1202. string result = "";
  1203. foreach (string item in list)
  1204. {
  1205. result += item + " ";
  1206. }
  1207. return result;
  1208. }
  1209. public void 強制登出()
  1210. {
  1211. firebaseManager.登出();
  1212. 讀取面板.SetActive(false);
  1213. }
  1214. private void 初始貼文讀取()
  1215. {
  1216. if (posts != null)
  1217. {
  1218. DeletetextCopies(posts);
  1219. }
  1220. DataSet ds = new();
  1221. SQL_game_sys.SQL_使用者_查詢貼文(Main.Global.使用者UID);
  1222. SQL_Module.da.Fill(ds);
  1223. 貼文 = ds.Tables[0];
  1224. currentIndex = 1;
  1225. if (GetTotalPostsCount() < 3)
  1226. {
  1227. 讀取貼文();
  1228. }
  1229. else
  1230. {
  1231. 總高=0;
  1232. for (int i = 0; i < 3 && i < GetTotalPostsCount(); i++)
  1233. {
  1234. CreatePost(i, false);
  1235. }
  1236. post計算();
  1237. buttonscroll1.onValueChanged.AddListener(OnScrollValueChanged);
  1238. }
  1239. Vector2 newPosition = buttonscroll1.content.anchoredPosition;
  1240. newPosition.y = 0f;
  1241. buttonscroll1.content.anchoredPosition = newPosition;
  1242. }
  1243. private void 讀取貼文()
  1244. {
  1245. float sum = 0;
  1246. for (int i = 0; i<貼文.Rows.Count; i++)
  1247. {
  1248. GameObject buttonObj = Instantiate(buttonPrefab1) as GameObject;
  1249. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -sum);
  1250. buttonObj.transform.SetParent(buttonParent1, false);
  1251. ScrollRect scroll = buttonObj.transform.Find("相片")?.GetComponent<ScrollRect>();
  1252. RectTransform content = scroll.content;
  1253. Image copy = content.Find("圖")?.GetComponent<Image>();
  1254. 圖片資料庫名稱=貼文.Rows[i][12].ToString();
  1255. 錄音資料庫名稱=貼文.Rows[i][13].ToString();
  1256. Debug.Log(圖片資料庫名稱);
  1257. Debug.Log(錄音資料庫名稱);
  1258. SQL_CommString.SQL_連線字串_錄音_SQL(Main.Global.伺服器, 錄音資料庫名稱);
  1259. SQL_CommString.SQL_連線字串_圖片_SQL(Main.Global.伺服器, 圖片資料庫名稱);
  1260. 圖片讀取(貼文.Rows[i][0].ToString(), copy, content, scroll);
  1261. copy.gameObject.SetActive(false);
  1262. TextMeshProUGUI count = buttonObj.transform.Find("相片數")?.GetComponent<TextMeshProUGUI>();
  1263. PhotoScrollView scrollView = scroll.AddComponent<PhotoScrollView>();
  1264. scrollView.largeScrollView =buttonscroll1;
  1265. scrollView.smallScrollView =scroll; // 设置 ScrollRect 变量
  1266. scrollView.pageIndicatorText = count; // 设置 TextMeshProUGUI 变量
  1267. scrollView.swipeThreshold = 200f; // 设置 swipeThreshold 变量
  1268. scrollView.snapSpeed = 10f; // 设置 snapSpeed 变量
  1269. scrollView.threshold = 100f;
  1270. scrollView.InitAfterMainScriptStarts();
  1271. TextMeshProUGUI name = buttonObj.transform.Find("暱稱")?.GetComponent<TextMeshProUGUI>();
  1272. TextMeshProUGUI place = buttonObj.transform.Find("地點")?.GetComponent<TextMeshProUGUI>();
  1273. TextMeshProUGUI time = buttonObj.transform.Find("時間")?.GetComponent<TextMeshProUGUI>();
  1274. TextMeshProUGUI detail = buttonObj.transform.Find("內容")?.GetComponent<TextMeshProUGUI>();
  1275. Image radio = buttonObj.transform.Find("顯示錄音")?.GetComponent<Image>();
  1276. TextMeshProUGUI rtime = radio.transform.Find("時間")?.GetComponent<TextMeshProUGUI>();
  1277. Button bt = radio.transform.Find("按鈕")?.GetComponent<Button>();
  1278. Slider timeSlider = radio.transform.Find("時間軸")?.GetComponent<Slider>();
  1279. Button bt1 = buttonObj.transform.Find("刪除")?.GetComponent<Button>();
  1280. int iii = i;
  1281. bt1.onClick.AddListener(() =>
  1282. {
  1283. 預刪除單號=貼文.Rows[iii][0].ToString();
  1284. 貼文面.SetActive(false);
  1285. 刪除面板.SetActive(true);
  1286. });
  1287. name.text =Main.Global.使用者暱稱;
  1288. place.text=貼文.Rows[i][9].ToString()+貼文.Rows[i][10].ToString();
  1289. time.text=ConvertToCustomFormat(貼文.Rows[i][1].ToString());
  1290. float contentHeight = 0f;
  1291. if (貼文.Rows[i][8].ToString() != "")
  1292. {
  1293. radio.gameObject.SetActive(true);
  1294. AudioPlayer audio = buttonObj.AddComponent<AudioPlayer>();
  1295. audio.timeText = rtime;
  1296. audio.progressBar = timeSlider;
  1297. audio.playPauseButton = bt;
  1298. audio.playSprite = playSprite;
  1299. audio.pauseSprite = pauseSprite;
  1300. SQL_game_sys.SQL_讀取錄音(貼文.Rows[i][0].ToString());
  1301. if (SQL_Module.dr.Read())
  1302. {
  1303. byte[] imageData = (byte[])SQL_Module.dr["錄音檔"];
  1304. Debug.Log(SQL_Module.dr["錄音流水號"].ToString());
  1305. audio.LoadAudioClip(imageData);
  1306. }
  1307. if (貼文.Rows[i][2].ToString() != "")
  1308. {
  1309. RectTransform contentRectTransform = detail.rectTransform;
  1310. contentRectTransform.anchoredPosition += new Vector2(0, -100);
  1311. }
  1312. contentHeight += 100f;
  1313. }
  1314. else
  1315. {
  1316. radio.gameObject.SetActive(false);
  1317. }
  1318. if (貼文.Rows[i][2].ToString() != "")
  1319. {
  1320. detail.gameObject.SetActive(true);
  1321. detail.text =貼文.Rows[i][2].ToString();
  1322. contentHeight += detail.preferredHeight;
  1323. }
  1324. else
  1325. {
  1326. detail.gameObject.SetActive(false);
  1327. }
  1328. RectTransform panelRectTransform = buttonObj.GetComponent<RectTransform>();
  1329. RectTransform parentRectTransform = 貼文面.GetComponent<RectTransform>();
  1330. // 获取当前预制体的大小
  1331. Vector2 panelSizeDelta = panelRectTransform.sizeDelta;
  1332. // 将预制体的大小设置为与父对象一样宽,高度增加 contentHeight
  1333. panelRectTransform.sizeDelta = new Vector2(parentRectTransform.rect.width, panelSizeDelta.y + contentHeight);
  1334. Debug.Log(parentRectTransform.rect.width);
  1335. // 设置 anchoring 和 pivot,使得预制体顶部对齐父对象的顶部
  1336. panelRectTransform.anchorMin = new Vector2(0, 1);
  1337. panelRectTransform.anchorMax = new Vector2(1, 1);
  1338. panelRectTransform.pivot = new Vector2(0.5f, 1);
  1339. sum+=panelSizeDelta.y + contentHeight;
  1340. posts.Add(buttonObj);
  1341. }
  1342. buttonceneter1.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter1.GetComponent<RectTransform>().sizeDelta.x, sum);
  1343. }
  1344. void OnDestroy()
  1345. {
  1346. firebaseManager.auth.StateChanged -= AuthStateChanged;
  1347. }
  1348. public void DeletetextCopies(List<GameObject> copy)
  1349. {
  1350. // 刪除複製出來的物件
  1351. foreach (GameObject copys in copy)
  1352. {
  1353. Destroy(copys);
  1354. }
  1355. copy.Clear();
  1356. }
  1357. public void 截圖()
  1358. {
  1359. Main.截圖();
  1360. }
  1361. }