| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using JCUnityLib;
- using JCUnityLib.UI;
- using Newtonsoft.Json.Linq;
- using System.Text.RegularExpressions;
- public class RetrievePasswordView : MonoBehaviour, MenuBackInterface
- {
- [SerializeField] GameObject emailObj;
- [SerializeField] GameObject phoneObj;
- [SerializeField] GameObject tipObj;
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- //InitRetrieveValidate();
- //InitSelectUsername();
- //InitResetPassword();
- //transform.Find("BtnBack").GetComponentInChildren<Button>().onClick.AddListener(() => Destroy(gameObject));
- InitInputLimit();
- if (CommonConfig.serverIndex == 0)
- {
- //国内使用手机注册
- _retrieveValidateType = RetrieveValidateType.Phone;
- phoneObj.SetActive(true);
- emailObj.SetActive(false);
- }
- else
- {
- //国外使用邮箱注册
- _retrieveValidateType = RetrieveValidateType.Email;
- phoneObj.SetActive(false);
- emailObj.SetActive(true);
- }
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- public bool OnMenuBack()
- {
- Destroy(gameObject);
- return true;
- }
- void ActivePage(int pageID)
- {
- _retrieveValidate.gameObject.SetActive(pageID == 0);
- _selectUsername.gameObject.SetActive(pageID == 1);
- _resetPassword.gameObject.SetActive(pageID == 2);
- }
- RectTransform _retrieveValidate;
- enum RetrieveValidateType { Email, Phone }
- RetrieveValidateType _retrieveValidateType = RetrieveValidateType.Email;
- void InitRetrieveValidate()
- {
- _retrieveValidate = transform.Find("RetrieveValidate") as RectTransform;
- var btnByEmail = _retrieveValidate.Find("BtnByEmail").GetComponent<Button>();
- var btnByPhone = _retrieveValidate.Find("BtnByPhone").GetComponent<Button>();
- btnByEmail.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Email));
- btnByPhone.onClick.AddListener(() => OnClick_RetrieveValidateType(RetrieveValidateType.Phone));
- btnByPhone.gameObject.SetActive(CommonConfig.serverIndex == 0);
- }
- void OnClick_RetrieveValidateType(RetrieveValidateType retrieveValidateType)
- {
- _retrieveValidateType = retrieveValidateType;
- RelateValidateView relateValidateView =
- Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
- .GetComponent<RelateValidateView>();
- CanvasUtils.PlusSortOrder(gameObject, relateValidateView.gameObject, 1);
- if (_retrieveValidateType == RetrieveValidateType.Email) relateValidateView.InitForEmail();
- if (_retrieveValidateType == RetrieveValidateType.Phone) relateValidateView.InitForPhone();
- relateValidateView.onValidateSuccess = (a, b, c) => {
- relateValidateView.BanButtons();
- if (_retrieveValidateType == RetrieveValidateType.Email)
- {
- string email = a; long timestamp = b; string sign = c;
- StartCoroutine(LoginController.Instance.ListUsernamesByEmail(email, timestamp, sign, (res) => {
- relateValidateView.CloseView();
- if (res.code == 0)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
- GoToSelectUsername(res.data as JArray);
- }
- else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
- else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
- }));
- }
- else if (_retrieveValidateType == RetrieveValidateType.Phone)
- {
- string phone = a; long timestamp = b; string sign = c;
- StartCoroutine(LoginController.Instance.ListUsernamesByPhone(phone, timestamp, sign, (res) => {
- relateValidateView.CloseView();
- if (res.code == 0)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
- GoToSelectUsername(res.data as JArray);
- }
- else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
- else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
- }));
- }
- };
- }
- RectTransform _selectUsername;
- string _currentUsername;
- long _currentUsernameTimestamp;
- string _currentUsernameSign;
- void InitSelectUsername()
- {
- _selectUsername = transform.Find("SelectUsername") as RectTransform;
- }
- void GoToSelectUsername(JArray jArray)
- {
- ActivePage(1);
- Transform content = _selectUsername.Find("Scroll View/Viewport/Content");
- GameObject prefab = content.Find("Username").gameObject;
- prefab.SetActive(false);
- foreach (var item in jArray)
- {
- string username = item.Value<string>("username");
- long timestamp = item.Value<long>("timestamp");
- string sign = item.Value<string>("sign");
- var o = Instantiate(prefab, content);
- o.GetComponentInChildren<Text>().text = username;
- o.GetComponent<Button>().onClick.AddListener(() => {
- _currentUsername = username;
- _currentUsernameTimestamp = timestamp;
- _currentUsernameSign = sign;
- GoToResetPassword();
- });
- o.SetActive(true);
- }
- }
- RectTransform _resetPassword;
- Text _page2_text_Username;
- InputField _page2_input_Password;
- InputField _page2_input_Password2;
- Button _page2_btnSubmit;
- JCUnityLib.Throttler _throttlerBtnSubmit = new JCUnityLib.Throttler(2000);
- void InitResetPassword()
- {
- _resetPassword = transform.Find("ResetPassword") as RectTransform;
- var layout = _resetPassword.Find("Layout");
- _page2_text_Username = layout.Find("Username").GetComponentInChildren<Text>();
- _page2_input_Password = layout.Find("Password").GetComponentInChildren<InputField>();
- _page2_input_Password2 = layout.Find("Password2").GetComponentInChildren<InputField>();
- _page2_btnSubmit = layout.Find("BtnSubmit").GetComponent<Button>();
- //limitInput
- InputField[] inputs = { _page2_input_Password, _page2_input_Password2 };
- foreach (var input in inputs) {
- input.onValueChanged.AddListener(delegate (string text) {
- Match match = new Regex("[^A-Za-z0-9]").Match(text);
- if (match.Success) {
- input.text = text.Replace(match.Value, "");
- }
- });
- }
- //submit
- _page2_btnSubmit.onClick.AddListener(() => {
- if (_page2_input_Password.text.Length < 6) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
- return;
- }
- if (_page2_input_Password.text != _page2_input_Password2.text) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
- return;
- }
- if (_throttlerBtnSubmit.CanPass() == false) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return;
- }
- StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, _page2_input_Password.text, (res) => {
- if (res.code == 0) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
- _page2_btnSubmit.interactable = false;
- StartCoroutine(DelayDestroy());
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
- }
- }));
- });
- }
- void GoToResetPassword()
- {
- ActivePage(2);
- _page2_text_Username.text = _currentUsername;
- _page2_input_Password.text = "";
- _page2_input_Password2.text = "";
- }
- IEnumerator DelayDestroy()
- {
- yield return new WaitForSeconds(1);
- Destroy(gameObject);
- }
- [SerializeField] InputField emailField;
- [SerializeField] InputField phoneField;
- [SerializeField] InputField codeField;
- [SerializeField] InputField newPwdField;
- [SerializeField] InputField conPwdField;
- string sendCodeAccount;
- [SerializeField] GameObject _prefabValidateJigsawView;
- static long _throttlerBtnSend_phone_reset = 0;
- long _throttlerBtnSend_reset
- {
- get
- {
- return _throttlerBtnSend_phone_reset;
- }
- set
- {
- _throttlerBtnSend_phone_reset = value;
- }
- }
- void InitInputLimit()
- {
- InputField[] inputNodes = { newPwdField, conPwdField };
- foreach (var inputNode in inputNodes)
- {
- InputField inputField = inputNode;
- inputField.onValueChanged.AddListener(delegate (string text) {
- Match match = new Regex("[^A-Za-z0-9]").Match(text);
- if (match.Success)
- {
- inputField.text = text.Replace(match.Value, "");
- }
- });
- }
- }
- public void OnClickBack() {
- Destroy(gameObject);
- }
- public void OnResetPassword() {
- string account = "";
- string code = codeField.text;
- if (_retrieveValidateType == RetrieveValidateType.Email)
- {
- account = emailField.text;
- if (!ValidateHelper.IsEmail(account))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
- return;
- }
- }
- else if (_retrieveValidateType == RetrieveValidateType.Phone)
- {
- account = phoneField.text;
- if (!ValidateHelper.IsMobilePhone(account))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
- return;
- }
- }
- //更换账户后请重新发送验证码
- if (sendCodeAccount != account)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a11"));
- return;
- }
- if (code.Length != 6)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a3"));
- return;
- }
- if (newPwdField.text.Length < 6)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
- return;
- }
- if (conPwdField.text != newPwdField.text)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
- return;
- }
- if (_throttlerBtnSubmit.CanPass() == false)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return;
- }
- if (_retrieveValidateType == RetrieveValidateType.Email)
- {
- StartCoroutine(EmailValidateController.Instance.ValidateEmail(account, code, (res) => {
- if (res.code == 0)
- {
- JObject data = res.data as JObject;
- string arg0 = data.Value<string>("email");
- long arg1 = data.Value<long>("timestamp");
- string arg2 = data.Value<string>("sign");
- Debug.Log($"邮箱验证成功 {arg0} {arg1} {arg2}");
- string _email = arg0; long timestamp = arg1; string sign = arg2;
- StartCoroutine(LoginController.Instance.ListUsernamesByEmail(_email, timestamp, sign, (res) => {
- if (res.code == 0)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
- OnResetAccount(res.data as JArray, _email);
- }
- else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
- else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip1"));
- }));
- }
- else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c0"));
- }));
- }
- else if (_retrieveValidateType == RetrieveValidateType.Phone)
- {
- StartCoroutine(PhoneValidateController.Instance.ValidatePhone(account, code, (res) => {
- if (res.code == 0)
- {
- JObject data = res.data as JObject;
- string arg0 = data.Value<string>("phone");
- long arg1 = data.Value<long>("timestamp");
- string arg2 = data.Value<string>("sign");
- Debug.Log($"手机验证成功 {arg0} {arg1} {arg2}");
- string _phone = arg0; long timestamp = arg1; string sign = arg2;
- StartCoroutine(LoginController.Instance.ListUsernamesByPhone(_phone, timestamp, sign, (res) => {
- if (res.code == 0)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass0"));
- OnResetAccount(res.data as JArray, _phone);
- }
- else if (res.code == -1) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip0"));
- else if (res.code == -2) PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip2"));
- }));
- }
- else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c1"));
- }));
- }
-
- }
- void OnResetAccount(JArray jArray,string account)
- {
- bool bHasAccount = false;
- foreach (var item in jArray)
- {
- string username = item.Value<string>("username");
- long timestamp = item.Value<long>("timestamp");
- string sign = item.Value<string>("sign");
- Debug.Log("server:"+ username + " = account:"+ account);
- if (account.ToLower() == username.ToLower())
- {
- bHasAccount = true;
- Debug.Log("server.ToLower():" + username.ToLower() + " = account.ToLower():" + account.ToLower());
- _currentUsername = username;
- _currentUsernameTimestamp = timestamp;
- _currentUsernameSign = sign;
- //修改密码
- StartCoroutine(LoginController.Instance.ResetPassword(_currentUsernameSign, _currentUsernameTimestamp, _currentUsername, newPwdField.text, (res) => {
- if (res.code == 0)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip3"));
- StartCoroutine(DelayDestroy());
- }
- else
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip4"));
- }
- }));
- }
- }
- //如果没有账户。提示
- if (!bHasAccount) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RetrievePasswordView-tip5"));
- }
- }
- public void onDetectPassword()
- {
- tipObj.SetActive(newPwdField.text != conPwdField.text);
- }
- public void OnClick_SendCode()
- {
- if (_retrieveValidateType == RetrieveValidateType.Email)
- {
- string email = emailField.text;
- sendCodeAccount = email;
- if (!ValidateHelper.IsEmail(email))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
- return;
- }
- }
- else if (_retrieveValidateType == RetrieveValidateType.Phone)
- {
- //记录一个发送code时候的号码,下一步时候判定一下是否同一个号码,不是的话提示重新发送,和检测一下合理性
- string phone = phoneField.text;
- sendCodeAccount = phone;
- if (!ValidateHelper.IsMobilePhone(phone))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
- return;
- }
- }
- //验证码再次发送需要间隔60秒
- long gapTime = TimeUtils.GetTimestamp() - _throttlerBtnSend_reset;
- long maxTime = 60 * 1000;
- if (gapTime < maxTime)
- {
- long second = (maxTime - gapTime) / 1000;
- if (second <= 0) second = 1;
- PopupMgr.ins.ShowTip(string.Format(TextAutoLanguage2.GetTextByKey("RelateValidateView-a2"), second));
- return;
- }
- //打开拼图验证
- GameObject gameObjectValidateJigsawView = Instantiate(_prefabValidateJigsawView);
- //CanvasUtils.PlusSortOrder(gameObject, gameObjectValidateJigsawView, 1);
- ValidateJigsawView validateJigsawView = gameObjectValidateJigsawView.GetComponent<ValidateJigsawView>();
- validateJigsawView.onComplete = RequestSendValidateCode;
- validateJigsawView.SetTextLabel(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_label"));
- validateJigsawView.SetTextTip(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_tip"));
- validateJigsawView.SetTextOK(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_ok"));
- }
- void RequestSendValidateCode()
- {
- //限流时间点记录
- _throttlerBtnSend_reset = TimeUtils.GetTimestamp();
- //请求服务端接口
- if (_retrieveValidateType == RetrieveValidateType.Email)
- {
- StartCoroutine(EmailValidateController.Instance.SendEmailValidateCode(sendCodeAccount, (res) => {
- if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b0"));
- else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b1"));
- }));
- }
- else if (_retrieveValidateType == RetrieveValidateType.Phone)
- {
- StartCoroutine(PhoneValidateController.Instance.SendPhoneValidateCode(sendCodeAccount, (res) => {
- if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b2"));
- else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b3"));
- }));
- }
- }
- }
|