StudyCenterForm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using GameFramework.Event;
  2. using GameFramework.WebRequest;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Serialization;
  7. using System.Text;
  8. using BestHTTP;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using UnityGameFramework.Runtime;
  12. using System;
  13. using UnityEngine.SceneManagement;
  14. public class StudyCenterForm : UIFormLogic
  15. {
  16. bool remenberMe = false;
  17. public Sprite[] loginSelectionTypes;
  18. InputField userName;
  19. InputField userPassword;
  20. GameObject loginObj;
  21. GameObject entryObj;
  22. Toggle remenber;
  23. Button loginSelection1;
  24. Button loginSelection2;
  25. Button loginBtn;
  26. private Button quitBtn;
  27. GameObject navigationBtn1;
  28. GameObject navigationBtn2;
  29. GameObject navigationBtn3;
  30. GameObject nb1;
  31. GameObject nb2;
  32. GameObject nb3;
  33. GameObject nb4;
  34. GameObject nb5;
  35. GameObject nb6;
  36. Text userNameText;
  37. GameObject navigation2;
  38. protected override void OnInit(object userData)
  39. {
  40. base.OnInit(userData);
  41. InItUiComponent();
  42. }
  43. protected override void OnOpen(object userData)
  44. {
  45. base.OnOpen(userData);
  46. GameMain.Event.Subscribe(ChangeGameModeTypeEvent.EventId, OnChangeGameMode);
  47. if (PlayerPrefs.HasKey("LastUserName") && userName != null && userPassword != null && remenber != null)
  48. {
  49. userName.text = PlayerPrefs.GetString("LastUserName");
  50. userPassword.text = PlayerPrefs.GetString("LastPassword");
  51. remenber.isOn = true;
  52. remenberMe = true;
  53. }
  54. else {
  55. userName.text = "";
  56. userPassword.text = "";
  57. remenber.isOn = false;
  58. remenberMe = false;
  59. }
  60. }
  61. private void InItUiComponent()
  62. {
  63. loginObj = this.transform.Find("Login").gameObject;
  64. entryObj = this.transform.Find("Entry").gameObject;
  65. loginSelection1 = this.transform.Find("Login/Content/Section1").GetComponent<Button>();
  66. loginSelection2 = this.transform.Find("Login/Content/Section2").GetComponent<Button>();
  67. remenber = this.transform.Find("Login/Content/Remenber").GetComponent<Toggle>();
  68. loginBtn = this.transform.Find("Login/Content/Login").GetComponent<Button>();
  69. userName = this.transform.Find("Login/Content/Account/InputField").GetComponent<InputField>();
  70. userPassword = this.transform.Find("Login/Content/Password/InputField").GetComponent<InputField>();
  71. quitBtn=this.transform.Find("BtnQuit").GetComponent<Button>();
  72. quitBtn.onClick.AddListener(() =>
  73. {
  74. Application.Quit();
  75. });
  76. loginSelection1.onClick.AddListener(() =>
  77. {
  78. UserData.currentLogType = LoginType.Teacher;
  79. loginSelection1.GetComponent<Image>().sprite = loginSelectionTypes[0];
  80. loginSelection2.GetComponent<Image>().sprite = loginSelectionTypes[1];
  81. });
  82. loginSelection2.onClick.AddListener(() =>
  83. {
  84. UserData.currentLogType = LoginType.Student;
  85. loginSelection1.GetComponent<Image>().sprite = loginSelectionTypes[1];
  86. loginSelection2.GetComponent<Image>().sprite = loginSelectionTypes[0];
  87. });
  88. remenber.onValueChanged.AddListener((bool change) =>
  89. {
  90. remenberMe = change;
  91. });
  92. loginBtn.onClick.AddListener(() =>
  93. {
  94. //#if UNITY_EDITOR
  95. // loginObj.gameObject.SetActive(false);
  96. // entryObj.gameObject.SetActive(true);
  97. // userNameText.text = UserData.realName;
  98. // GameMain.Event.Fire(this, LoginResultEvent.Create(LoginResultEvent.LoginResult.success));
  99. // return;
  100. //#endif
  101. BestHTTP.HTTPRequest request = new BestHTTP.HTTPRequest(new System.Uri(HttpAddress.loginAddress), BestHTTP.HTTPMethods.Post, (HTTPRequest request1,HTTPResponse response) =>
  102. {
  103. try
  104. {
  105. Log.Debug(response.DataAsText);
  106. LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
  107. if (loginInfo.code == 200 && loginInfo.data != null)
  108. {
  109. UserData.token = loginInfo.data.token;
  110. UserData.userName = loginInfo.data.userName;
  111. UserData.realName = loginInfo.data.realName;
  112. LoginType tempType = loginInfo.data.roles.Length > 1 ? LoginType.Teacher : LoginType.Student;
  113. if (tempType != UserData.currentLogType)
  114. {
  115. GameMain.UI.OpenUIForm("Tips", "Pop", new TipsData("", "登录信息有误,请检查登录账户类型!"));
  116. }
  117. else
  118. {
  119. if (remenberMe)
  120. {
  121. PlayerPrefs.SetString("LastUserName", userName.text.Trim());
  122. PlayerPrefs.SetString("LastPassword", userPassword.text.Trim());
  123. PlayerPrefs.Save();
  124. }
  125. else
  126. {
  127. if (PlayerPrefs.HasKey("LastUserName"))
  128. {
  129. PlayerPrefs.DeleteKey("LastUserName");
  130. }
  131. if (PlayerPrefs.HasKey("LastPassword"))
  132. {
  133. PlayerPrefs.DeleteKey("LastPassword");
  134. }
  135. PlayerPrefs.Save();
  136. }
  137. HttpGlobal.SendPingToServer();
  138. GameMain.beginHeart = true;
  139. Debug.Log("登录成功开始统计在线时长");
  140. loginObj.gameObject.SetActive(false);
  141. entryObj.gameObject.SetActive(true);
  142. userNameText.text = UserData.realName;
  143. GameMain.Event.Fire(this, LoginResultEvent.Create(LoginResultEvent.LoginResult.success));
  144. }
  145. }
  146. else
  147. {
  148. GameMain.UI.OpenUIForm("Tips", "Pop", new TipsData("", "登录信息有误,账户或密码错误!"));
  149. }
  150. //Log.Debug(response.DataAsText);
  151. //LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
  152. //if (loginInfo.code == 200 && loginInfo.data != null)
  153. //{
  154. // UserData.token = loginInfo.data.token;
  155. // UserData.userName = loginInfo.data.userName;
  156. // UserData.realName = loginInfo.data.realName;
  157. // LoginType tempType = loginInfo.data.roles.Length > 1 ? LoginType.Teacher : LoginType.Student;
  158. // if (tempType != UserData.currentLogType)
  159. // {
  160. // GameMain.UI.OpenUIForm("Tips", "Pop", new TipsData("", "登录信息有误,请检查登录账户类型!"));
  161. // }
  162. // else
  163. // {
  164. // if (remenberMe)
  165. // {
  166. // PlayerPrefs.SetString("LastUserName", userName.text.Trim());
  167. // PlayerPrefs.SetString("LastPassword", userPassword.text.Trim());
  168. // PlayerPrefs.Save();
  169. // }
  170. // else
  171. // {
  172. // if (PlayerPrefs.HasKey("LastUserName"))
  173. // {
  174. // PlayerPrefs.DeleteKey("LastUserName");
  175. // }
  176. // if (PlayerPrefs.HasKey("LastPassword"))
  177. // {
  178. // PlayerPrefs.DeleteKey("LastPassword");
  179. // }
  180. // PlayerPrefs.Save();
  181. // }
  182. // loginObj.gameObject.SetActive(false);
  183. // entryObj.gameObject.SetActive(true);
  184. // userNameText.text = UserData.realName;
  185. // GameMain.Event.Fire(this, LoginResultEvent.Create(LoginResultEvent.LoginResult.success));
  186. // }
  187. //}
  188. //else
  189. //{
  190. // GameMain.UI.OpenUIForm("Tips", "Pop", new TipsData("", "登录信息有误,账户或密码错误!"));
  191. //}
  192. }
  193. catch (Exception e) {
  194. GameMain.UI.OpenUIForm("Tips", "Pop", new TipsData("", string.Format("服务器异常,请联系管理员!\n{0}",e.ToString())));
  195. }
  196. });
  197. string jsonContent = JsonConvert.SerializeObject(new UserLoginInfo(userName.text.Trim(), userPassword.text.Trim(),"",""));
  198. request.RawData = Encoding.UTF8.GetBytes(jsonContent);
  199. request.AddHeader("Content-Type", "application/json");
  200. request.Send();
  201. });
  202. userNameText = this.transform.Find("Entry/LoginUser").GetComponent<Text>();
  203. navigationBtn1 = this.transform.Find("Entry/Navigation/B1").gameObject;
  204. navigationBtn2 = this.transform.Find("Entry/Navigation/B2").gameObject;
  205. navigationBtn3 = this.transform.Find("Entry/Navigation/B3").gameObject;
  206. nb1 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB1").gameObject;
  207. nb2 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB2").gameObject;
  208. nb3 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB3").gameObject;
  209. nb4 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB4").gameObject;
  210. nb5 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB5").gameObject;
  211. nb6 = this.transform.Find("Entry/Navigation/B2/Navigation2/NB6").gameObject;
  212. navigation2 = this.transform.Find("Entry/Navigation/B2/Navigation2").gameObject;
  213. navigationBtn1.gameObject.GetComponent<Button>().onClick.AddListener(()=> {
  214. if (SceneManager.GetActiveScene().name == "Main")
  215. {
  216. return;
  217. }
  218. navigationBtn1.gameObject.GetComponent<Image>().color = Color.white;
  219. navigationBtn2.gameObject.GetComponent<Image>().color = Color.clear;
  220. navigationBtn3.gameObject.GetComponent<Image>().color = Color.clear;
  221. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(navigationBtn1.gameObject.GetComponent<Button>()));
  222. //navigation2.gameObject.SetActive(false);
  223. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(0));
  224. });
  225. navigationBtn2.gameObject.GetComponent<Button>().onClick.AddListener(() => {
  226. if (SceneManager.GetActiveScene().name == "Main") {
  227. return;
  228. }
  229. navigationBtn1.gameObject.GetComponent<Image>().color = Color.clear;
  230. navigationBtn2.gameObject.GetComponent<Image>().color = Color.white;
  231. navigationBtn3.gameObject.GetComponent<Image>().color = Color.clear;
  232. navigation2.gameObject.SetActive(!navigation2.gameObject.activeSelf);
  233. //GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(1));
  234. });
  235. nb1.GetComponent<Button>().onClick.AddListener(() =>
  236. {
  237. navigation2.gameObject.SetActive(false);
  238. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(1));
  239. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb1.GetComponent<Button>()));
  240. HttpGlobal.SendLogToServer("进入了IT系统业务训练");
  241. });
  242. nb2.GetComponent<Button>().onClick.AddListener(() =>
  243. {
  244. navigation2.gameObject.SetActive(false);
  245. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(3));
  246. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb2.GetComponent<Button>()));
  247. HttpGlobal.SendLogToServer("进入了网络系统业务训练");
  248. });
  249. nb3.GetComponent<Button>().onClick.AddListener(() =>
  250. {
  251. navigation2.gameObject.SetActive(false);
  252. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(4));
  253. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb3.GetComponent<Button>()));
  254. HttpGlobal.SendLogToServer("进入了电力系统业务训练");
  255. });
  256. nb4.GetComponent<Button>().onClick.AddListener(() =>
  257. {
  258. navigation2.gameObject.SetActive(false);
  259. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(5));
  260. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb4.GetComponent<Button>()));
  261. HttpGlobal.SendLogToServer("进入了冷却系统业务训练");
  262. });
  263. nb5.GetComponent<Button>().onClick.AddListener(() =>
  264. {
  265. navigation2.gameObject.SetActive(false);
  266. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(7));
  267. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb5.GetComponent<Button>()));
  268. HttpGlobal.SendLogToServer("进入了监控系统业务训练");
  269. });
  270. nb6.GetComponent<Button>().onClick.AddListener(() =>
  271. {
  272. navigation2.gameObject.SetActive(false);
  273. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(6));
  274. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(nb6.GetComponent<Button>()));
  275. HttpGlobal.SendLogToServer("进入了应用程序系统业务训练");
  276. });
  277. navigationBtn3.gameObject.GetComponent<Button>().onClick.AddListener(() =>
  278. {
  279. if (SceneManager.GetActiveScene().name == "Main")
  280. {
  281. return;
  282. }
  283. navigationBtn1.gameObject.GetComponent<Image>().color = Color.clear;
  284. navigationBtn2.gameObject.GetComponent<Image>().color = Color.clear;
  285. navigationBtn3.gameObject.GetComponent<Image>().color = Color.white;
  286. //navigation2.gameObject.SetActive(false);
  287. GameMain.Event.Fire(this, ChangeGameModeTypeEvent.Create(2));
  288. GameMain.Event.Fire(this, AnyButtomClickEvent.Create(navigationBtn3.gameObject.GetComponent<Button>()));
  289. });
  290. }
  291. private void OnChangeGameMode(object sender, GameEventArgs e)
  292. {
  293. navigation2.gameObject.SetActive(false);
  294. }
  295. protected override void OnClose(bool isShutdown, object userData)
  296. {
  297. base.OnClose(isShutdown, userData);
  298. GameMain.Event.Unsubscribe(ChangeGameModeTypeEvent.EventId, OnChangeGameMode);
  299. }
  300. }