RetrievePasswordView.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  47. void OnClick_RetrieveValidateType(RetrieveValidateType retrieveValidateType)
  48. {
  49. _retrieveValidateType = retrieveValidateType;
  50. RelateValidateView relateValidateView =
  51. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  52. .GetComponent<RelateValidateView>();
  53. CanvasUtils.PlusSortOrder(gameObject, relateValidateView.gameObject, 1);
  54. if (_retrieveValidateType == RetrieveValidateType.Email) relateValidateView.InitForEmail();
  55. if (_retrieveValidateType == RetrieveValidateType.Phone) relateValidateView.InitForPhone();
  56. relateValidateView.onValidateSuccess = (a, b, c) => {
  57. relateValidateView.BanButtons();
  58. if (_retrieveValidateType == RetrieveValidateType.Email)
  59. {
  60. string email = a; long timestamp = b; string sign = c;
  61. StartCoroutine(LoginController.Instance.ListUsernamesByEmail(email, timestamp, sign, (res) => {
  62. relateValidateView.CloseView();
  63. if (res.code == 0)
  64. {
  65. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  66. GoToSelectUsername(res.data as JArray);
  67. }
  68. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  69. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
  70. }));
  71. }
  72. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  73. {
  74. string phone = a; long timestamp = b; string sign = c;
  75. StartCoroutine(LoginController.Instance.ListUsernamesByPhone(phone, timestamp, sign, (res) => {
  76. relateValidateView.CloseView();
  77. if (res.code == 0)
  78. {
  79. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  80. GoToSelectUsername(res.data as JArray);
  81. }
  82. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  83. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
  84. }));
  85. }
  86. };
  87. }
  88. RectTransform _selectUsername;
  89. string _currentUsername;
  90. long _currentUsernameTimestamp;
  91. string _currentUsernameSign;
  92. void InitSelectUsername()
  93. {
  94. _selectUsername = transform.Find("SelectUsername") as RectTransform;
  95. }
  96. void GoToSelectUsername(JArray jArray)
  97. {
  98. ActivePage(1);
  99. Transform content = _selectUsername.Find("Scroll View/Viewport/Content");
  100. GameObject prefab = content.Find("Username").gameObject;
  101. prefab.SetActive(false);
  102. foreach (var item in jArray)
  103. {
  104. string username = item.Value<string>("username");
  105. long timestamp = item.Value<long>("timestamp");
  106. string sign = item.Value<string>("sign");
  107. var o = Instantiate(prefab, content);
  108. o.GetComponentInChildren<Text>().text = username;
  109. o.GetComponent<Button>().onClick.AddListener(() => {
  110. _currentUsername = username;
  111. _currentUsernameTimestamp = timestamp;
  112. _currentUsernameSign = sign;
  113. GoToResetPassword();
  114. });
  115. o.SetActive(true);
  116. }
  117. }
  118. RectTransform _resetPassword;
  119. Text _page2_text_Username;
  120. InputField _page2_input_Password;
  121. InputField _page2_input_Password2;
  122. Button _page2_btnSubmit;
  123. JCUnityLib.Throttler _throttlerBtnSubmit = new JCUnityLib.Throttler(2000);
  124. void InitResetPassword()
  125. {
  126. _resetPassword = transform.Find("ResetPassword") as RectTransform;
  127. var layout = _resetPassword.Find("Layout");
  128. _page2_text_Username = layout.Find("Username").GetComponentInChildren<Text>();
  129. _page2_input_Password = layout.Find("Password").GetComponentInChildren<InputField>();
  130. _page2_input_Password2 = layout.Find("Password2").GetComponentInChildren<InputField>();
  131. _page2_btnSubmit = layout.Find("BtnSubmit").GetComponent<Button>();
  132. //limitInput
  133. InputField[] inputs = {_page2_input_Password, _page2_input_Password2};
  134. foreach (var input in inputs) {
  135. input.onValueChanged.AddListener(delegate(string text) {
  136. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  137. if (match.Success) {
  138. input.text = text.Replace(match.Value, "");
  139. }
  140. });
  141. }
  142. //submit
  143. _page2_btnSubmit.onClick.AddListener(() => {
  144. if (_page2_input_Password.text.Length < 6) {
  145. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
  146. return;
  147. }
  148. if (_page2_input_Password.text != _page2_input_Password2.text) {
  149. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
  150. return;
  151. }
  152. if (_throttlerBtnSubmit.CanPass() == false) {
  153. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  154. return;
  155. }
  156. StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, _page2_input_Password.text, (res) => {
  157. if (res.code == 0) {
  158. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
  159. _page2_btnSubmit.interactable = false;
  160. StartCoroutine(DelayDestroy());
  161. } else {
  162. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
  163. }
  164. }));
  165. });
  166. }
  167. void GoToResetPassword()
  168. {
  169. ActivePage(2);
  170. _page2_text_Username.text = _currentUsername;
  171. _page2_input_Password.text = "";
  172. _page2_input_Password2.text = "";
  173. }
  174. IEnumerator DelayDestroy()
  175. {
  176. yield return new WaitForSeconds(1);
  177. Destroy(gameObject);
  178. }
  179. }