LoginPanel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections;
  3. using Chiva.Toolkit.Function.NoBorder;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using QFramework;
  7. using UnityEngine.SceneManagement;
  8. namespace QFramework
  9. {
  10. public class LoginPanelData : UIPanelData
  11. {
  12. }
  13. public partial class LoginPanel : UIPanel
  14. {
  15. /// <summary>
  16. /// 记录错误次数
  17. /// </summary>
  18. private int m_RecordErrorCount = 0;
  19. /// <summary>
  20. /// 缓存账号信息
  21. /// </summary>
  22. private string m_CacheAccountInfo = String.Empty;
  23. protected override void ProcessMsg(int eventId, QMsg msg)
  24. {
  25. }
  26. protected override void OnInit(IUIData uiData = null)
  27. {
  28. mData = uiData as LoginPanelData ?? new LoginPanelData();
  29. // please add init code here
  30. m_RecordErrorCount = 0;
  31. Init();
  32. }
  33. #region 内置函数
  34. protected override void OnOpen(IUIData uiData = null)
  35. {
  36. WindowsSetting.Instance.SetBorderModel(true, 600, 353);
  37. Input_Name.text = Input_Password.text = string.Empty;
  38. }
  39. protected override void OnShow()
  40. {
  41. }
  42. protected override void OnHide()
  43. {
  44. WindowsSetting.Instance.SetBorderModel(true, 1920, 1080);
  45. }
  46. protected override void OnClose()
  47. {
  48. WindowsSetting.Instance.SetBorderModel(true, 1920, 1080);
  49. }
  50. #endregion
  51. private void Init()
  52. {
  53. MinBtn.onClick.AddListener(() => WindowsSetting.Instance.OnMinBtnClick());
  54. CloseBtn.onClick.AddListener(() => WindowsSetting.Instance.ShutDownFunction());
  55. LoginBtn.onClick.AddListener(() =>
  56. {
  57. LoginDetection();
  58. });
  59. Input_Name.onValueChanged.AddListener((input) =>
  60. {
  61. if (!string.IsNullOrEmpty(input) && PlayerPrefs.HasKey(input))
  62. {
  63. Input_Password.text = PlayerPrefs.GetString(input);
  64. Toggle_Rember.isOn = true;
  65. }
  66. });
  67. }
  68. void Update()
  69. {
  70. if (Input.GetKeyDown(KeyCode.Tab))
  71. {
  72. if (Input_Name.isFocused)
  73. {
  74. Input_Password.Select();
  75. }
  76. else if (Input_Password.isFocused)
  77. {
  78. Input_Name.Select();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// 登录检测
  84. /// </summary>
  85. private void LoginDetection()
  86. {
  87. if (!string.IsNullOrEmpty(Input_Name.text) && !string.IsNullOrEmpty(Input_Password.text))
  88. {
  89. UserProxy userProxy = DAL.Instance.Get<UserProxy>();
  90. if (userProxy != null)
  91. {
  92. StudentLoginResponse loginResponse = userProxy.UserLogin(Input_Name.text,
  93. Input_Password.text.Encrypt("chivatech"));
  94. if (loginResponse.Result)
  95. {
  96. m_CacheAccountInfo = Input_Name.text;
  97. m_RecordErrorCount = 0;
  98. if (Toggle_Rember.isOn)
  99. {
  100. PlayerPrefs.SetString(Input_Name.text, Input_Password.text);
  101. }
  102. else
  103. {
  104. if (PlayerPrefs.HasKey(Input_Name.text))
  105. {
  106. PlayerPrefs.DeleteKey(Input_Name.text);
  107. }
  108. }
  109. UIKit.ClosePanel<LoginPanel>();
  110. // 记载场景
  111. SceneManager.LoadSceneAsync("Select");
  112. }
  113. else
  114. {
  115. StopAllCoroutines();
  116. if (m_CacheAccountInfo.Equals(Input_Name.text))
  117. {
  118. m_RecordErrorCount++;
  119. }
  120. else
  121. {
  122. m_CacheAccountInfo = Input_Name.text;
  123. m_RecordErrorCount = 1;
  124. }
  125. if (m_RecordErrorCount >= 3)
  126. {
  127. StartCoroutine(ShowPopText("如果忘记账号或密码,请联系管理员"));
  128. }
  129. else
  130. {
  131. StartCoroutine(ShowPopText(loginResponse.Message));
  132. }
  133. }
  134. }
  135. else
  136. {
  137. StartCoroutine(ShowPopText("账号存在问题,请联系管理员"));
  138. }
  139. }
  140. else
  141. {
  142. StartCoroutine(ShowPopText("账号或密码不能为空"));
  143. }
  144. }
  145. IEnumerator ShowPopText(string message)
  146. {
  147. PromptTxt.text = message;
  148. yield return new WaitForSeconds(2f);
  149. PromptTxt.text = "";
  150. }
  151. }
  152. }