RetrievePasswordView.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Events;
  7. using JCUnityLib;
  8. using JCUnityLib.UI;
  9. using Newtonsoft.Json.Linq;
  10. using System.Text.RegularExpressions;
  11. public class RetrievePasswordView : MonoBehaviour, MenuBackInterface
  12. {
  13. void Start()
  14. {
  15. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  16. InitRetrieveValidate();
  17. InitSelectUsername();
  18. InitResetPassword();
  19. transform.Find("BtnBack").GetComponentInChildren<Button>().onClick.AddListener(() => Destroy(gameObject));
  20. }
  21. void OnDestroy()
  22. {
  23. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  24. }
  25. public bool OnMenuBack()
  26. {
  27. Destroy(gameObject);
  28. return true;
  29. }
  30. void ActivePage(int pageID)
  31. {
  32. _retrieveValidate.gameObject.SetActive(pageID == 0);
  33. _selectUsername.gameObject.SetActive(pageID == 1);
  34. _resetPassword.gameObject.SetActive(pageID == 2);
  35. }
  36. RectTransform _retrieveValidate;
  37. enum RetrieveValidateType { Email, Phone }
  38. RetrieveValidateType _retrieveValidateType = RetrieveValidateType.Email;
  39. void InitRetrieveValidate()
  40. {
  41. _retrieveValidate = transform.Find("RetrieveValidate") as RectTransform;
  42. var btnByEmail = _retrieveValidate.Find("BtnByEmail").GetComponent<Button>();
  43. var btnByPhone = _retrieveValidate.Find("BtnByPhone").GetComponent<Button>();
  44. btnByEmail.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Email));
  45. btnByPhone.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Phone));
  46. btnByPhone.gameObject.SetActive(CommonConfig.serverIndex == 0);
  47. }
  48. void OnClick_RetrieveValidateType(RetrieveValidateType retrieveValidateType)
  49. {
  50. _retrieveValidateType = retrieveValidateType;
  51. RelateValidateView relateValidateView =
  52. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  53. .GetComponent<RelateValidateView>();
  54. CanvasUtils.PlusSortOrder(gameObject, relateValidateView.gameObject, 1);
  55. if (_retrieveValidateType == RetrieveValidateType.Email) relateValidateView.InitForEmail();
  56. if (_retrieveValidateType == RetrieveValidateType.Phone) relateValidateView.InitForPhone();
  57. relateValidateView.onValidateSuccess = (a, b, c) => {
  58. relateValidateView.BanButtons();
  59. if (_retrieveValidateType == RetrieveValidateType.Email)
  60. {
  61. string email = a; long timestamp = b; string sign = c;
  62. StartCoroutine(LoginController.Instance.ListUsernamesByEmail(email, timestamp, sign, (res) => {
  63. relateValidateView.CloseView();
  64. if (res.code == 0)
  65. {
  66. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  67. GoToSelectUsername(res.data as JArray);
  68. }
  69. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  70. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
  71. }));
  72. }
  73. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  74. {
  75. string phone = a; long timestamp = b; string sign = c;
  76. StartCoroutine(LoginController.Instance.ListUsernamesByPhone(phone, timestamp, sign, (res) => {
  77. relateValidateView.CloseView();
  78. if (res.code == 0)
  79. {
  80. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  81. GoToSelectUsername(res.data as JArray);
  82. }
  83. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  84. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
  85. }));
  86. }
  87. };
  88. }
  89. RectTransform _selectUsername;
  90. string _currentUsername;
  91. long _currentUsernameTimestamp;
  92. string _currentUsernameSign;
  93. void InitSelectUsername()
  94. {
  95. _selectUsername = transform.Find("SelectUsername") as RectTransform;
  96. }
  97. void GoToSelectUsername(JArray jArray)
  98. {
  99. ActivePage(1);
  100. Transform content = _selectUsername.Find("Scroll View/Viewport/Content");
  101. GameObject prefab = content.Find("Username").gameObject;
  102. prefab.SetActive(false);
  103. foreach (var item in jArray)
  104. {
  105. string username = item.Value<string>("username");
  106. long timestamp = item.Value<long>("timestamp");
  107. string sign = item.Value<string>("sign");
  108. var o = Instantiate(prefab, content);
  109. o.GetComponentInChildren<Text>().text = username;
  110. o.GetComponent<Button>().onClick.AddListener(() => {
  111. _currentUsername = username;
  112. _currentUsernameTimestamp = timestamp;
  113. _currentUsernameSign = sign;
  114. GoToResetPassword();
  115. });
  116. o.SetActive(true);
  117. }
  118. }
  119. RectTransform _resetPassword;
  120. Text _page2_text_Username;
  121. InputField _page2_input_Password;
  122. InputField _page2_input_Password2;
  123. Button _page2_btnSubmit;
  124. JCUnityLib.Throttler _throttlerBtnSubmit = new JCUnityLib.Throttler(2000);
  125. void InitResetPassword()
  126. {
  127. _resetPassword = transform.Find("ResetPassword") as RectTransform;
  128. var layout = _resetPassword.Find("Layout");
  129. _page2_text_Username = layout.Find("Username").GetComponentInChildren<Text>();
  130. _page2_input_Password = layout.Find("Password").GetComponentInChildren<InputField>();
  131. _page2_input_Password2 = layout.Find("Password2").GetComponentInChildren<InputField>();
  132. _page2_btnSubmit = layout.Find("BtnSubmit").GetComponent<Button>();
  133. //limitInput
  134. InputField[] inputs = {_page2_input_Password, _page2_input_Password2};
  135. foreach (var input in inputs) {
  136. input.onValueChanged.AddListener(delegate(string text) {
  137. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  138. if (match.Success) {
  139. input.text = text.Replace(match.Value, "");
  140. }
  141. });
  142. }
  143. //submit
  144. _page2_btnSubmit.onClick.AddListener(() => {
  145. if (_page2_input_Password.text.Length < 6) {
  146. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
  147. return;
  148. }
  149. if (_page2_input_Password.text != _page2_input_Password2.text) {
  150. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
  151. return;
  152. }
  153. if (_throttlerBtnSubmit.CanPass() == false) {
  154. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  155. return;
  156. }
  157. StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, _page2_input_Password.text, (res) => {
  158. if (res.code == 0) {
  159. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
  160. _page2_btnSubmit.interactable = false;
  161. StartCoroutine(DelayDestroy());
  162. } else {
  163. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
  164. }
  165. }));
  166. });
  167. }
  168. void GoToResetPassword()
  169. {
  170. ActivePage(2);
  171. _page2_text_Username.text = _currentUsername;
  172. _page2_input_Password.text = "";
  173. _page2_input_Password2.text = "";
  174. }
  175. IEnumerator DelayDestroy()
  176. {
  177. yield return new WaitForSeconds(1);
  178. Destroy(gameObject);
  179. }
  180. }