RelateValidateView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using JCUnityLib;
  7. using JCUnityLib.UI;
  8. using UnityEngine.Events;
  9. using Newtonsoft.Json.Linq;
  10. public class RelateValidateView : MonoBehaviour, MenuBackInterface
  11. {
  12. Text _textTitle;
  13. Button _btnClose;
  14. InputField _inputRelateAccount;
  15. InputField _inputValidateCode;
  16. Button _btnSend;
  17. Button _btnSubmit;
  18. [SerializeField] GameObject _prefabValidateJigsawView;
  19. public UnityAction<string, long, string> onValidateSuccess;
  20. void Awake()
  21. {
  22. _textTitle = transform.Find("Frame/Title").GetComponent<Text>();
  23. _btnClose = transform.Find("Frame/BtnClose").GetComponent<Button>();
  24. _inputRelateAccount = transform.Find("Frame/Layout/InputRelateAccount").GetComponent<InputField>();
  25. _inputValidateCode = transform.Find("Frame/Layout/InputValidateCode").GetComponent<InputField>();
  26. _btnSend = transform.Find("Frame/Layout/InputValidateCode/BtnSend").GetComponent<Button>();
  27. _btnSubmit = transform.Find("Frame/Layout/GameObject/BtmSubmit").GetComponent<Button>();
  28. }
  29. void Start()
  30. {
  31. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  32. _btnClose.onClick.AddListener(OnClick_BtnClose);
  33. _btnSend.onClick.AddListener(OnClick_BtnSend);
  34. _btnSubmit.onClick.AddListener(OnClick_BtnSubmit);
  35. }
  36. void OnDestroy()
  37. {
  38. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  39. }
  40. void Update()
  41. {
  42. #if UNITY_EDITOR
  43. if (Input.GetKeyDown(KeyCode.F3))
  44. {
  45. if (_validateType == ValidateType.Email) _inputRelateAccount.text = "lvjincheng9831@163.com";
  46. if (_validateType == ValidateType.Phone) _inputRelateAccount.text = "17875300045";
  47. }
  48. #endif
  49. }
  50. public bool OnMenuBack()
  51. {
  52. return false;
  53. }
  54. void OnClick_BtnClose()
  55. {
  56. CloseView();
  57. }
  58. public void CloseView()
  59. {
  60. Destroy(gameObject);
  61. }
  62. public void BanButtons()
  63. {
  64. _btnSend.interactable = false;
  65. _btnSubmit.interactable = false;
  66. }
  67. static long _throttlerBtnSend_email = 0;
  68. static long _throttlerBtnSend_phone = 0;
  69. long _throttlerBtnSend {
  70. get {
  71. if (_validateType == ValidateType.Email) return _throttlerBtnSend_email;
  72. if (_validateType == ValidateType.Phone) return _throttlerBtnSend_phone;
  73. return 0;
  74. }
  75. set {
  76. if (_validateType == ValidateType.Email) _throttlerBtnSend_email = value;
  77. if (_validateType == ValidateType.Phone) _throttlerBtnSend_phone = value;
  78. }
  79. }
  80. void OnClick_BtnSend()
  81. {
  82. if (_validateType == ValidateType.Email)
  83. {
  84. string email = _inputRelateAccount.text;
  85. if (!ValidateHelper.IsEmail(email))
  86. {
  87. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
  88. return;
  89. }
  90. }
  91. else if (_validateType == ValidateType.Phone)
  92. {
  93. string phone = _inputRelateAccount.text;
  94. if (!ValidateHelper.IsMobilePhone(phone))
  95. {
  96. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
  97. return;
  98. }
  99. }
  100. //验证码再次发送需要间隔60秒
  101. long gapTime = TimeUtils.GetTimestamp() - _throttlerBtnSend;
  102. long maxTime = 60 * 1000;
  103. if (gapTime < maxTime)
  104. {
  105. long second = (maxTime - gapTime) / 1000;
  106. if (second <= 0) second = 1;
  107. PopupMgr.ins.ShowTip(string.Format(TextAutoLanguage2.GetTextByKey("RelateValidateView-a2"), second));
  108. return;
  109. }
  110. //打开拼图验证
  111. GameObject gameObjectValidateJigsawView = Instantiate(_prefabValidateJigsawView);
  112. CanvasUtils.PlusSortOrder(gameObject, gameObjectValidateJigsawView, 1);
  113. ValidateJigsawView validateJigsawView = gameObjectValidateJigsawView.GetComponent<ValidateJigsawView>();
  114. validateJigsawView.onComplete = RequestSendValidateCode;
  115. validateJigsawView.SetTextLabel(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_label"));
  116. validateJigsawView.SetTextTip(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_tip"));
  117. validateJigsawView.SetTextOK(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_ok"));
  118. }
  119. void RequestSendValidateCode()
  120. {
  121. //限流时间点记录
  122. _throttlerBtnSend = TimeUtils.GetTimestamp();
  123. //请求服务端接口
  124. if (_validateType == ValidateType.Email)
  125. {
  126. string email = _inputRelateAccount.text;
  127. StartCoroutine(EmailValidateController.Instance.SendEmailValidateCode(email, (res) => {
  128. if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b0"));
  129. else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b1"));
  130. }));
  131. }
  132. else if (_validateType == ValidateType.Phone)
  133. {
  134. string phone = _inputRelateAccount.text;
  135. StartCoroutine(PhoneValidateController.Instance.SendPhoneValidateCode(phone, (res) => {
  136. if (res.code == 0) PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b2"));
  137. else PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("RelateValidateView-b3"));
  138. }));
  139. }
  140. }
  141. Throttler _throttlerBtnSubmit = new Throttler(3000);
  142. void OnClick_BtnSubmit()
  143. {
  144. if (_validateType == ValidateType.Email)
  145. {
  146. string email = _inputRelateAccount.text;
  147. if (!ValidateHelper.IsEmail(email))
  148. {
  149. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a0"));
  150. return;
  151. }
  152. }
  153. else if (_validateType == ValidateType.Phone)
  154. {
  155. string phone = _inputRelateAccount.text;
  156. if (!ValidateHelper.IsMobilePhone(phone))
  157. {
  158. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a1"));
  159. return;
  160. }
  161. }
  162. string code = _inputValidateCode.text;
  163. if (code.Length != 6)
  164. {
  165. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-a3"));
  166. return;
  167. }
  168. if (!_throttlerBtnSubmit.CanPass())
  169. {
  170. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  171. return;
  172. }
  173. RequestSubmit();
  174. }
  175. void RequestSubmit()
  176. {
  177. if (_validateType == ValidateType.Email)
  178. {
  179. string email = _inputRelateAccount.text;
  180. string code = _inputValidateCode.text;
  181. StartCoroutine(EmailValidateController.Instance.ValidateEmail(email, code, (res) => {
  182. if (res.code == 0)
  183. {
  184. JObject data = res.data as JObject;
  185. string arg0 = data.Value<string>("email");
  186. long arg1 = data.Value<long>("timestamp");
  187. string arg2 = data.Value<string>("sign");
  188. Debug.Log($"邮箱验证成功 {arg0} {arg1} {arg2}");
  189. onValidateSuccess?.Invoke(arg0, arg1, arg2);
  190. }
  191. else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c0"));
  192. }));
  193. }
  194. else if (_validateType == ValidateType.Phone)
  195. {
  196. string phone = _inputRelateAccount.text;
  197. string code = _inputValidateCode.text;
  198. StartCoroutine(PhoneValidateController.Instance.ValidatePhone(phone, code, (res) => {
  199. if (res.code == 0)
  200. {
  201. JObject data = res.data as JObject;
  202. string arg0 = data.Value<string>("phone");
  203. long arg1 = data.Value<long>("timestamp");
  204. string arg2 = data.Value<string>("sign");
  205. Debug.Log($"手机验证成功 {arg0} {arg1} {arg2}");
  206. onValidateSuccess?.Invoke(arg0, arg1, arg2);
  207. }
  208. else PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-c1"));
  209. }));
  210. }
  211. }
  212. public enum ValidateType { Email, Phone }
  213. private ValidateType _validateType = ValidateType.Email;
  214. public void SetValidateType(ValidateType validateType)
  215. {
  216. _validateType = validateType;
  217. if (_validateType == ValidateType.Email)
  218. {
  219. _inputRelateAccount.contentType = InputField.ContentType.EmailAddress;
  220. _inputRelateAccount.characterLimit = 32;
  221. }
  222. else if (_validateType == ValidateType.Phone)
  223. {
  224. _inputRelateAccount.contentType = InputField.ContentType.IntegerNumber;
  225. _inputRelateAccount.characterLimit = 11;
  226. }
  227. }
  228. public void SetTextKeys(TextKeys textKeys)
  229. {
  230. if (textKeys.title != null)
  231. _textTitle.text = TextAutoLanguage2.GetTextByKey(textKeys.title);
  232. if (textKeys.inputRelateAccount != null)
  233. _inputRelateAccount.transform.Find("Placeholder").GetComponent<Text>().text
  234. = TextAutoLanguage2.GetTextByKey(textKeys.inputRelateAccount);
  235. if (textKeys.inputValidateCode != null)
  236. _inputValidateCode.transform.Find("Placeholder").GetComponent<Text>().text
  237. = TextAutoLanguage2.GetTextByKey(textKeys.inputValidateCode);
  238. if (textKeys.btnSend != null)
  239. _btnSend.GetComponentInChildren<Text>().text = TextAutoLanguage2.GetTextByKey(textKeys.btnSend);
  240. if (textKeys.btnSubmit != null)
  241. _btnSubmit.GetComponentInChildren<Text>().text = TextAutoLanguage2.GetTextByKey(textKeys.btnSubmit);
  242. }
  243. public class TextKeys
  244. {
  245. public string title;
  246. public string inputRelateAccount;
  247. public string inputValidateCode;
  248. public string btnSend;
  249. public string btnSubmit;
  250. }
  251. //初始化-邮箱验证
  252. public void InitForEmail()
  253. {
  254. TextKeys textKeys = new TextKeys();
  255. textKeys.title = "RelateValidateView-email0";
  256. textKeys.inputRelateAccount = "RelateValidateView-email1";
  257. textKeys.inputValidateCode = "RelateValidateView-email2";
  258. textKeys.btnSend = "RelateValidateView-email3";
  259. textKeys.btnSubmit = "RelateValidateView-email4";
  260. SetTextKeys(textKeys);
  261. SetValidateType(ValidateType.Email);
  262. }
  263. public void InitForEmail2()
  264. {
  265. TextKeys textKeys = new TextKeys();
  266. textKeys.title = "RelateValidateView-email00";
  267. textKeys.inputRelateAccount = "RelateValidateView-email1";
  268. textKeys.inputValidateCode = "RelateValidateView-email2";
  269. textKeys.btnSend = "RelateValidateView-email3";
  270. textKeys.btnSubmit = "RelateValidateView-email4";
  271. SetTextKeys(textKeys);
  272. SetValidateType(ValidateType.Email);
  273. }
  274. //初始化-手机验证
  275. public void InitForPhone()
  276. {
  277. TextKeys textKeys = new TextKeys();
  278. textKeys.title = "RelateValidateView-phone0";
  279. textKeys.inputRelateAccount = "RelateValidateView-phone1";
  280. textKeys.inputValidateCode = "RelateValidateView-phone2";
  281. textKeys.btnSend = "RelateValidateView-phone3";
  282. textKeys.btnSubmit = "RelateValidateView-phone4";
  283. SetTextKeys(textKeys);
  284. SetValidateType(ValidateType.Phone);
  285. }
  286. public void InitForPhone2()
  287. {
  288. TextKeys textKeys = new TextKeys();
  289. textKeys.title = "RelateValidateView-phone00";
  290. textKeys.inputRelateAccount = "RelateValidateView-phone1";
  291. textKeys.inputValidateCode = "RelateValidateView-phone2";
  292. textKeys.btnSend = "RelateValidateView-phone3";
  293. textKeys.btnSubmit = "RelateValidateView-phone4";
  294. SetTextKeys(textKeys);
  295. SetValidateType(ValidateType.Phone);
  296. }
  297. }