| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JCUnityLib;
- using JCUnityLib.UI;
- using UnityEngine.Events;
- using Newtonsoft.Json.Linq;
- public class RelateValidateView : MonoBehaviour, MenuBackInterface
- {
- Text _textTitle;
- Button _btnClose;
- InputField _inputRelateAccount;
- InputField _inputValidateCode;
- Button _btnSend;
- Button _btnSubmit;
- [SerializeField] GameObject _prefabValidateJigsawView;
- public UnityAction<string, long, string> onValidateSuccess;
- void Awake()
- {
- _textTitle = transform.Find("Frame/Title").GetComponent<Text>();
- _btnClose = transform.Find("Frame/BtnClose").GetComponent<Button>();
- _inputRelateAccount = transform.Find("Frame/Layout/InputRelateAccount").GetComponent<InputField>();
- _inputValidateCode = transform.Find("Frame/Layout/InputValidateCode").GetComponent<InputField>();
- _btnSend = transform.Find("Frame/Layout/InputValidateCode/BtnSend").GetComponent<Button>();
- _btnSubmit = transform.Find("Frame/Layout/GameObject/BtmSubmit").GetComponent<Button>();
- }
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- _btnClose.onClick.AddListener(OnClick_BtnClose);
- _btnSend.onClick.AddListener(OnClick_BtnSend);
- _btnSubmit.onClick.AddListener(OnClick_BtnSubmit);
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- void Update()
- {
- #if UNITY_EDITOR
- if (Input.GetKeyDown(KeyCode.F3))
- {
- if (_validateType == ValidateType.Email) _inputRelateAccount.text = "lvjincheng9831@163.com";
- if (_validateType == ValidateType.Phone) _inputRelateAccount.text = "17875300045";
- }
- #endif
- }
- public bool OnMenuBack()
- {
- return false;
- }
- void OnClick_BtnClose()
- {
- CloseView();
- }
- public void CloseView()
- {
- Destroy(gameObject);
- }
- public void BanButtons()
- {
- _btnSend.interactable = false;
- _btnSubmit.interactable = false;
- }
- static long _throttlerBtnSend_email = 0;
- static long _throttlerBtnSend_phone = 0;
- long _throttlerBtnSend {
- get {
- if (_validateType == ValidateType.Email) return _throttlerBtnSend_email;
- if (_validateType == ValidateType.Phone) return _throttlerBtnSend_phone;
- return 0;
- }
- set {
- if (_validateType == ValidateType.Email) _throttlerBtnSend_email = value;
- if (_validateType == ValidateType.Phone) _throttlerBtnSend_phone = value;
- }
- }
- void OnClick_BtnSend()
- {
- if (_validateType == ValidateType.Email)
- {
- string email = _inputRelateAccount.text;
- if (!ValidateHelper.IsEmail(email))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
- return;
- }
-
- }
- else if (_validateType == ValidateType.Phone)
- {
- string phone = _inputRelateAccount.text;
- if (!ValidateHelper.IsMobilePhone(phone))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
- return;
- }
-
- }
- //验证码再次发送需要间隔60秒
- long gapTime = TimeUtils.GetTimestamp() - _throttlerBtnSend;
- 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 = TimeUtils.GetTimestamp();
- //请求服务端接口
- if (_validateType == ValidateType.Email)
- {
- string email = _inputRelateAccount.text;
- StartCoroutine(EmailValidateController.Instance.SendEmailValidateCode(email, (res) => {
- if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b0"));
- else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b1"));
- }));
- }
- else if (_validateType == ValidateType.Phone)
- {
- string phone = _inputRelateAccount.text;
- StartCoroutine(PhoneValidateController.Instance.SendPhoneValidateCode(phone, (res) => {
- if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b2"));
- else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b3"));
- }));
- }
- }
- Throttler _throttlerBtnSubmit = new Throttler(3000);
- void OnClick_BtnSubmit()
- {
- if (_validateType == ValidateType.Email)
- {
- string email = _inputRelateAccount.text;
- if (!ValidateHelper.IsEmail(email))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
- return;
- }
- }
- else if (_validateType == ValidateType.Phone)
- {
- string phone = _inputRelateAccount.text;
- if (!ValidateHelper.IsMobilePhone(phone))
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
- return;
- }
- }
- string code = _inputValidateCode.text;
- if (code.Length != 6)
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a3"));
- return;
- }
- if (!_throttlerBtnSubmit.CanPass())
- {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return;
- }
- RequestSubmit();
- }
- void RequestSubmit()
- {
- if (_validateType == ValidateType.Email)
- {
- string email = _inputRelateAccount.text;
- string code = _inputValidateCode.text;
- StartCoroutine(EmailValidateController.Instance.ValidateEmail(email, 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}");
- onValidateSuccess?.Invoke(arg0, arg1, arg2);
- }
- else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c0"));
- }));
- }
- else if (_validateType == ValidateType.Phone)
- {
- string phone = _inputRelateAccount.text;
- string code = _inputValidateCode.text;
- StartCoroutine(PhoneValidateController.Instance.ValidatePhone(phone, 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}");
- onValidateSuccess?.Invoke(arg0, arg1, arg2);
- }
- else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c1"));
- }));
- }
- }
- public enum ValidateType { Email, Phone }
- private ValidateType _validateType = ValidateType.Email;
- public void SetValidateType(ValidateType validateType)
- {
- _validateType = validateType;
- if (_validateType == ValidateType.Email)
- {
- _inputRelateAccount.contentType = InputField.ContentType.EmailAddress;
- _inputRelateAccount.characterLimit = 32;
- }
- else if (_validateType == ValidateType.Phone)
- {
- _inputRelateAccount.contentType = InputField.ContentType.IntegerNumber;
- _inputRelateAccount.characterLimit = 11;
- }
- }
- public void SetTextKeys(TextKeys textKeys)
- {
- if (textKeys.title != null)
- _textTitle.text = TextAutoLanguage2.GetTextByKey(textKeys.title);
- if (textKeys.inputRelateAccount != null)
- _inputRelateAccount.transform.Find("Placeholder").GetComponent<Text>().text
- = TextAutoLanguage2.GetTextByKey(textKeys.inputRelateAccount);
- if (textKeys.inputValidateCode != null)
- _inputValidateCode.transform.Find("Placeholder").GetComponent<Text>().text
- = TextAutoLanguage2.GetTextByKey(textKeys.inputValidateCode);
- if (textKeys.btnSend != null)
- _btnSend.GetComponentInChildren<Text>().text = TextAutoLanguage2.GetTextByKey(textKeys.btnSend);
- if (textKeys.btnSubmit != null)
- _btnSubmit.GetComponentInChildren<Text>().text = TextAutoLanguage2.GetTextByKey(textKeys.btnSubmit);
- }
- public class TextKeys
- {
- public string title;
- public string inputRelateAccount;
- public string inputValidateCode;
- public string btnSend;
- public string btnSubmit;
- }
- //初始化-邮箱验证
- public void InitForEmail()
- {
- TextKeys textKeys = new TextKeys();
- textKeys.title = "RelateValidateView-email0";
- textKeys.inputRelateAccount = "RelateValidateView-email1";
- textKeys.inputValidateCode = "RelateValidateView-email2";
- textKeys.btnSend = "RelateValidateView-email3";
- textKeys.btnSubmit = "RelateValidateView-email4";
- SetTextKeys(textKeys);
- SetValidateType(ValidateType.Email);
- }
- public void InitForEmail2()
- {
- TextKeys textKeys = new TextKeys();
- textKeys.title = "RelateValidateView-email00";
- textKeys.inputRelateAccount = "RelateValidateView-email1";
- textKeys.inputValidateCode = "RelateValidateView-email2";
- textKeys.btnSend = "RelateValidateView-email3";
- textKeys.btnSubmit = "RelateValidateView-email4";
- SetTextKeys(textKeys);
- SetValidateType(ValidateType.Email);
- }
- //初始化-手机验证
- public void InitForPhone()
- {
- TextKeys textKeys = new TextKeys();
- textKeys.title = "RelateValidateView-phone0";
- textKeys.inputRelateAccount = "RelateValidateView-phone1";
- textKeys.inputValidateCode = "RelateValidateView-phone2";
- textKeys.btnSend = "RelateValidateView-phone3";
- textKeys.btnSubmit = "RelateValidateView-phone4";
- SetTextKeys(textKeys);
- SetValidateType(ValidateType.Phone);
- }
- public void InitForPhone2()
- {
- TextKeys textKeys = new TextKeys();
- textKeys.title = "RelateValidateView-phone00";
- textKeys.inputRelateAccount = "RelateValidateView-phone1";
- textKeys.inputValidateCode = "RelateValidateView-phone2";
- textKeys.btnSend = "RelateValidateView-phone3";
- textKeys.btnSubmit = "RelateValidateView-phone4";
- SetTextKeys(textKeys);
- SetValidateType(ValidateType.Phone);
- }
- }
|