RetrievePasswordView.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. [SerializeField] GameObject emailObj;
  14. [SerializeField] GameObject phoneObj;
  15. [SerializeField] GameObject tipObj;
  16. void Start()
  17. {
  18. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  19. //InitRetrieveValidate();
  20. //InitSelectUsername();
  21. //InitResetPassword();
  22. //transform.Find("BtnBack").GetComponentInChildren<Button>().onClick.AddListener(() => Destroy(gameObject));
  23. InitInputLimit();
  24. if (CommonConfig.serverIndex == 0)
  25. {
  26. //国内使用手机注册
  27. _retrieveValidateType = RetrieveValidateType.Phone;
  28. phoneObj.SetActive(true);
  29. emailObj.SetActive(false);
  30. }
  31. else
  32. {
  33. //国外使用邮箱注册
  34. _retrieveValidateType = RetrieveValidateType.Email;
  35. phoneObj.SetActive(false);
  36. emailObj.SetActive(true);
  37. }
  38. }
  39. void OnDestroy()
  40. {
  41. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  42. }
  43. public bool OnMenuBack()
  44. {
  45. Destroy(gameObject);
  46. return true;
  47. }
  48. void ActivePage(int pageID)
  49. {
  50. _retrieveValidate.gameObject.SetActive(pageID == 0);
  51. _selectUsername.gameObject.SetActive(pageID == 1);
  52. _resetPassword.gameObject.SetActive(pageID == 2);
  53. }
  54. RectTransform _retrieveValidate;
  55. enum RetrieveValidateType { Email, Phone }
  56. RetrieveValidateType _retrieveValidateType = RetrieveValidateType.Email;
  57. void InitRetrieveValidate()
  58. {
  59. _retrieveValidate = transform.Find("RetrieveValidate") as RectTransform;
  60. var btnByEmail = _retrieveValidate.Find("BtnByEmail").GetComponent<Button>();
  61. var btnByPhone = _retrieveValidate.Find("BtnByPhone").GetComponent<Button>();
  62. btnByEmail.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Email));
  63. btnByPhone.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Phone));
  64. btnByPhone.gameObject.SetActive(CommonConfig.serverIndex == 0);
  65. }
  66. void OnClick_RetrieveValidateType(RetrieveValidateType retrieveValidateType)
  67. {
  68. _retrieveValidateType = retrieveValidateType;
  69. RelateValidateView relateValidateView =
  70. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  71. .GetComponent<RelateValidateView>();
  72. CanvasUtils.PlusSortOrder(gameObject, relateValidateView.gameObject, 1);
  73. if (_retrieveValidateType == RetrieveValidateType.Email) relateValidateView.InitForEmail();
  74. if (_retrieveValidateType == RetrieveValidateType.Phone) relateValidateView.InitForPhone();
  75. relateValidateView.onValidateSuccess = (a, b, c) => {
  76. relateValidateView.BanButtons();
  77. if (_retrieveValidateType == RetrieveValidateType.Email)
  78. {
  79. string email = a; long timestamp = b; string sign = c;
  80. StartCoroutine(LoginController.Instance.ListUsernamesByEmail(email, timestamp, sign, (res) => {
  81. relateValidateView.CloseView();
  82. if (res.code == 0)
  83. {
  84. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  85. GoToSelectUsername(res.data as JArray);
  86. }
  87. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  88. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
  89. }));
  90. }
  91. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  92. {
  93. string phone = a; long timestamp = b; string sign = c;
  94. StartCoroutine(LoginController.Instance.ListUsernamesByPhone(phone, timestamp, sign, (res) => {
  95. relateValidateView.CloseView();
  96. if (res.code == 0)
  97. {
  98. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  99. GoToSelectUsername(res.data as JArray);
  100. }
  101. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  102. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
  103. }));
  104. }
  105. };
  106. }
  107. RectTransform _selectUsername;
  108. string _currentUsername;
  109. long _currentUsernameTimestamp;
  110. string _currentUsernameSign;
  111. void InitSelectUsername()
  112. {
  113. _selectUsername = transform.Find("SelectUsername") as RectTransform;
  114. }
  115. void GoToSelectUsername(JArray jArray)
  116. {
  117. ActivePage(1);
  118. Transform content = _selectUsername.Find("Scroll View/Viewport/Content");
  119. GameObject prefab = content.Find("Username").gameObject;
  120. prefab.SetActive(false);
  121. foreach (var item in jArray)
  122. {
  123. string username = item.Value<string>("username");
  124. long timestamp = item.Value<long>("timestamp");
  125. string sign = item.Value<string>("sign");
  126. var o = Instantiate(prefab, content);
  127. o.GetComponentInChildren<Text>().text = username;
  128. o.GetComponent<Button>().onClick.AddListener(() => {
  129. _currentUsername = username;
  130. _currentUsernameTimestamp = timestamp;
  131. _currentUsernameSign = sign;
  132. GoToResetPassword();
  133. });
  134. o.SetActive(true);
  135. }
  136. }
  137. RectTransform _resetPassword;
  138. Text _page2_text_Username;
  139. InputField _page2_input_Password;
  140. InputField _page2_input_Password2;
  141. Button _page2_btnSubmit;
  142. JCUnityLib.Throttler _throttlerBtnSubmit = new JCUnityLib.Throttler(2000);
  143. void InitResetPassword()
  144. {
  145. _resetPassword = transform.Find("ResetPassword") as RectTransform;
  146. var layout = _resetPassword.Find("Layout");
  147. _page2_text_Username = layout.Find("Username").GetComponentInChildren<Text>();
  148. _page2_input_Password = layout.Find("Password").GetComponentInChildren<InputField>();
  149. _page2_input_Password2 = layout.Find("Password2").GetComponentInChildren<InputField>();
  150. _page2_btnSubmit = layout.Find("BtnSubmit").GetComponent<Button>();
  151. //limitInput
  152. InputField[] inputs = { _page2_input_Password, _page2_input_Password2 };
  153. foreach (var input in inputs) {
  154. input.onValueChanged.AddListener(delegate (string text) {
  155. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  156. if (match.Success) {
  157. input.text = text.Replace(match.Value, "");
  158. }
  159. });
  160. }
  161. //submit
  162. _page2_btnSubmit.onClick.AddListener(() => {
  163. if (_page2_input_Password.text.Length < 6) {
  164. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
  165. return;
  166. }
  167. if (_page2_input_Password.text != _page2_input_Password2.text) {
  168. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
  169. return;
  170. }
  171. if (_throttlerBtnSubmit.CanPass() == false) {
  172. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  173. return;
  174. }
  175. StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, _page2_input_Password.text, (res) => {
  176. if (res.code == 0) {
  177. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
  178. _page2_btnSubmit.interactable = false;
  179. StartCoroutine(DelayDestroy());
  180. } else {
  181. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
  182. }
  183. }));
  184. });
  185. }
  186. void GoToResetPassword()
  187. {
  188. ActivePage(2);
  189. _page2_text_Username.text = _currentUsername;
  190. _page2_input_Password.text = "";
  191. _page2_input_Password2.text = "";
  192. }
  193. IEnumerator DelayDestroy()
  194. {
  195. yield return new WaitForSeconds(1);
  196. Destroy(gameObject);
  197. }
  198. [SerializeField] InputField emailField;
  199. [SerializeField] InputField phoneField;
  200. [SerializeField] InputField codeField;
  201. [SerializeField] InputField newPwdField;
  202. [SerializeField] InputField conPwdField;
  203. string sendCodeAccount;
  204. [SerializeField] GameObject _prefabValidateJigsawView;
  205. static long _throttlerBtnSend_phone_reset = 0;
  206. long _throttlerBtnSend_reset
  207. {
  208. get
  209. {
  210. return _throttlerBtnSend_phone_reset;
  211. }
  212. set
  213. {
  214. _throttlerBtnSend_phone_reset = value;
  215. }
  216. }
  217. void InitInputLimit()
  218. {
  219. InputField[] inputNodes = { newPwdField, conPwdField };
  220. foreach (var inputNode in inputNodes)
  221. {
  222. InputField inputField = inputNode;
  223. inputField.onValueChanged.AddListener(delegate (string text) {
  224. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  225. if (match.Success)
  226. {
  227. inputField.text = text.Replace(match.Value, "");
  228. }
  229. });
  230. }
  231. }
  232. public void OnClickBack() {
  233. Destroy(gameObject);
  234. }
  235. public void OnResetPassword() {
  236. string account = "";
  237. string code = codeField.text;
  238. if (_retrieveValidateType == RetrieveValidateType.Email)
  239. {
  240. account = emailField.text;
  241. if (!ValidateHelper.IsEmail(account))
  242. {
  243. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
  244. return;
  245. }
  246. }
  247. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  248. {
  249. account = phoneField.text;
  250. if (!ValidateHelper.IsMobilePhone(account))
  251. {
  252. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
  253. return;
  254. }
  255. }
  256. //更换账户后请重新发送验证码
  257. if (sendCodeAccount != account)
  258. {
  259. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a11"));
  260. return;
  261. }
  262. if (code.Length != 6)
  263. {
  264. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a3"));
  265. return;
  266. }
  267. if (newPwdField.text.Length < 6)
  268. {
  269. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
  270. return;
  271. }
  272. if (conPwdField.text != newPwdField.text)
  273. {
  274. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
  275. return;
  276. }
  277. if (_throttlerBtnSubmit.CanPass() == false)
  278. {
  279. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  280. return;
  281. }
  282. if (_retrieveValidateType == RetrieveValidateType.Email)
  283. {
  284. StartCoroutine(EmailValidateController.Instance.ValidateEmail(account, code, (res) => {
  285. if (res.code == 0)
  286. {
  287. JObject data = res.data as JObject;
  288. string arg0 = data.Value<string>("email");
  289. long arg1 = data.Value<long>("timestamp");
  290. string arg2 = data.Value<string>("sign");
  291. Debug.Log($"邮箱验证成功 {arg0} {arg1} {arg2}");
  292. string _email = arg0; long timestamp = arg1; string sign = arg2;
  293. StartCoroutine(LoginController.Instance.ListUsernamesByEmail(_email, timestamp, sign, (res) => {
  294. if (res.code == 0)
  295. {
  296. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  297. OnResetAccount(res.data as JArray, _email);
  298. }
  299. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  300. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
  301. }));
  302. }
  303. else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c0"));
  304. }));
  305. }
  306. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  307. {
  308. StartCoroutine(PhoneValidateController.Instance.ValidatePhone(account, code, (res) => {
  309. if (res.code == 0)
  310. {
  311. JObject data = res.data as JObject;
  312. string arg0 = data.Value<string>("phone");
  313. long arg1 = data.Value<long>("timestamp");
  314. string arg2 = data.Value<string>("sign");
  315. Debug.Log($"手机验证成功 {arg0} {arg1} {arg2}");
  316. string _phone = arg0; long timestamp = arg1; string sign = arg2;
  317. StartCoroutine(LoginController.Instance.ListUsernamesByPhone(_phone, timestamp, sign, (res) => {
  318. if (res.code == 0)
  319. {
  320. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
  321. OnResetAccount(res.data as JArray, _phone);
  322. }
  323. else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
  324. else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
  325. }));
  326. }
  327. else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c1"));
  328. }));
  329. }
  330. }
  331. void OnResetAccount(JArray jArray,string account)
  332. {
  333. bool bHasAccount = false;
  334. foreach (var item in jArray)
  335. {
  336. string username = item.Value<string>("username");
  337. long timestamp = item.Value<long>("timestamp");
  338. string sign = item.Value<string>("sign");
  339. Debug.Log("server:"+ username + " = account:"+ account);
  340. if (account.ToLower() == username.ToLower())
  341. {
  342. bHasAccount = true;
  343. Debug.Log("server.ToLower():" + username.ToLower() + " = account.ToLower():" + account.ToLower());
  344. _currentUsername = username;
  345. _currentUsernameTimestamp = timestamp;
  346. _currentUsernameSign = sign;
  347. //修改密码
  348. StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, newPwdField.text, (res) => {
  349. if (res.code == 0)
  350. {
  351. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
  352. StartCoroutine(DelayDestroy());
  353. }
  354. else
  355. {
  356. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
  357. }
  358. }));
  359. }
  360. }
  361. //如果没有账户。提示
  362. if (!bHasAccount) {
  363. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip5"));
  364. }
  365. }
  366. public void onDetectPassword()
  367. {
  368. tipObj.SetActive(newPwdField.text != conPwdField.text);
  369. }
  370. public void OnClick_SendCode()
  371. {
  372. if (_retrieveValidateType == RetrieveValidateType.Email)
  373. {
  374. string email = emailField.text;
  375. sendCodeAccount = email;
  376. if (!ValidateHelper.IsEmail(email))
  377. {
  378. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
  379. return;
  380. }
  381. }
  382. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  383. {
  384. //记录一个发送code时候的号码,下一步时候判定一下是否同一个号码,不是的话提示重新发送,和检测一下合理性
  385. string phone = phoneField.text;
  386. sendCodeAccount = phone;
  387. if (!ValidateHelper.IsMobilePhone(phone))
  388. {
  389. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
  390. return;
  391. }
  392. }
  393. //验证码再次发送需要间隔60秒
  394. long gapTime = TimeUtils.GetTimestamp() - _throttlerBtnSend_reset;
  395. long maxTime = 60 * 1000;
  396. if (gapTime < maxTime)
  397. {
  398. long second = (maxTime - gapTime) / 1000;
  399. if (second <= 0) second = 1;
  400. PopupMgr.ins.ShowTip(string.Format(TextAutoLanguage2.GetTextByKey("RelateValidateView-a2"), second));
  401. return;
  402. }
  403. //打开拼图验证
  404. GameObject gameObjectValidateJigsawView = Instantiate(_prefabValidateJigsawView);
  405. //CanvasUtils.PlusSortOrder(gameObject, gameObjectValidateJigsawView, 1);
  406. ValidateJigsawView validateJigsawView = gameObjectValidateJigsawView.GetComponent<ValidateJigsawView>();
  407. validateJigsawView.onComplete = RequestSendValidateCode;
  408. validateJigsawView.SetTextLabel(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_label"));
  409. validateJigsawView.SetTextTip(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_tip"));
  410. validateJigsawView.SetTextOK(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_ok"));
  411. }
  412. void RequestSendValidateCode()
  413. {
  414. //限流时间点记录
  415. _throttlerBtnSend_reset = TimeUtils.GetTimestamp();
  416. //请求服务端接口
  417. if (_retrieveValidateType == RetrieveValidateType.Email)
  418. {
  419. StartCoroutine(EmailValidateController.Instance.SendEmailValidateCode(sendCodeAccount, (res) => {
  420. if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b0"));
  421. else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b1"));
  422. }));
  423. }
  424. else if (_retrieveValidateType == RetrieveValidateType.Phone)
  425. {
  426. StartCoroutine(PhoneValidateController.Instance.SendPhoneValidateCode(sendCodeAccount, (res) => {
  427. if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b2"));
  428. else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b3"));
  429. }));
  430. }
  431. }
  432. }