RegisterView.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.Networking;
  8. using System.Text.RegularExpressions;
  9. using DG.Tweening;
  10. using Newtonsoft.Json;
  11. /* 注册界面 */
  12. public class RegisterView : MonoBehaviour
  13. {
  14. [SerializeField] GameObject registerInUser;
  15. [SerializeField] GameObject registerInPWD1;
  16. [SerializeField] GameObject registerInPWD2;
  17. [SerializeField] GameObject registerInCaptcha;
  18. [SerializeField] GameObject registerInNickname;
  19. [SerializeField] GameObject registerInGender;
  20. [SerializeField] GameObject registerInBirthday;
  21. [SerializeField] GameObject registerInLocation;
  22. [SerializeField] GameObject btnNext;
  23. [SerializeField] GameObject btnSave;
  24. //状态记录
  25. public int captcha_Register = -222222222;
  26. void OnEnable()
  27. {
  28. InitPage();
  29. }
  30. void Start()
  31. {
  32. InitInputLimit();
  33. }
  34. void InitInputLimit() {
  35. GameObject[] inputNodes = {registerInUser, registerInPWD1, registerInPWD2};
  36. foreach (var inputNode in inputNodes) {
  37. InputField inputField = GetInputField(inputNode);
  38. inputField.onValueChanged.AddListener(delegate(string text) {
  39. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  40. if (match.Success) {
  41. inputField.text = text.Replace(match.Value, "");
  42. }
  43. });
  44. }
  45. }
  46. void InitPage(bool isNext = false) {
  47. registerInUser.SetActive(!isNext);
  48. registerInPWD1.SetActive(!isNext);
  49. registerInPWD2.SetActive(!isNext);
  50. registerInCaptcha.SetActive(!isNext);
  51. btnNext.SetActive(!isNext);
  52. registerInNickname.SetActive(isNext);
  53. registerInGender.SetActive(isNext);
  54. registerInBirthday.SetActive(isNext);
  55. registerInLocation.SetActive(isNext);
  56. btnSave.SetActive(isNext);
  57. if (!isNext) {
  58. ChnageCaptcha();
  59. }
  60. }
  61. InputField GetInputField(GameObject inputNode) {
  62. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  63. }
  64. public void ChnageCaptcha() {
  65. StartCoroutine(CaptchaController.ins.GetCaptcha(
  66. registerInCaptcha.transform.Find("CodeImage").GetComponent<Image>(),
  67. (code) => { captcha_Register = code; }
  68. ));
  69. }
  70. private string usrRecord = "";
  71. private string pwdRecord = "";
  72. long _lastRegisterNextTime = 0;
  73. public void RegisterNext()
  74. {
  75. InputField user = GetInputField(registerInUser);
  76. if (user.text.Length < 6) {
  77. PopupMgr.ins.ShowTip("用户名长度至少6位");
  78. return;
  79. }
  80. InputField pwd1 = GetInputField(registerInPWD1);
  81. if (pwd1.text.Length < 6) {
  82. PopupMgr.ins.ShowTip("密码长度至少6位");
  83. return;
  84. }
  85. InputField pwd2 = GetInputField(registerInPWD2);
  86. if (pwd1.text != pwd2.text) {
  87. PopupMgr.ins.ShowTip("两次输入的密码不一致");
  88. return;
  89. }
  90. InputField captcha = GetInputField(registerInCaptcha);
  91. if (!captcha.text.Equals(captcha_Register.ToString())) {
  92. PopupMgr.ins.ShowTip("验证码错误");
  93. return;
  94. }
  95. if (JC.CS.Utility.GetTimestamp() - _lastRegisterNextTime < 2000) {
  96. PopupMgr.ins.ShowTip("操作过于频繁");
  97. return;
  98. } else {
  99. _lastRegisterNextTime = JC.CS.Utility.GetTimestamp();
  100. }
  101. usrRecord = user.text;
  102. pwdRecord = pwd1.text;
  103. StartCoroutine(LoginController.ins.Register(
  104. usrRecord, pwdRecord, (res) => {
  105. PopupMgr.ins.ShowTip(res.msg);
  106. if (res.code == 0) {
  107. InitPage(true); //前往完善用户信息
  108. LoginView.ins.FillLoginInput(usrRecord, pwdRecord);
  109. }
  110. }
  111. ));
  112. }
  113. long _lastRegisterSaveTime = 0;
  114. public void RegisterSave() {
  115. InputField nickname = GetInputField(registerInNickname);
  116. if (nickname.text.Trim().Length == 0) {
  117. PopupMgr.ins.ShowTip("请输入游戏昵称");
  118. return;
  119. }
  120. int gender = 0;
  121. Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
  122. for (int i = 0; i < toggleGroup.childCount; i++) {
  123. if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
  124. gender = i == 0 ? 1 : 2;
  125. break;
  126. }
  127. }
  128. InputField birthday = GetInputField(registerInBirthday);
  129. if (birthday.text.Length != 10) {
  130. PopupMgr.ins.ShowTip("未填写出生日期");
  131. return;
  132. }
  133. InputField location = GetInputField(registerInLocation);
  134. if (location.text.Length == 0) {
  135. PopupMgr.ins.ShowTip("未填写所在地区");
  136. return;
  137. }
  138. if (JC.CS.Utility.GetTimestamp() - _lastRegisterSaveTime < 2000) {
  139. PopupMgr.ins.ShowTip("操作过于频繁");
  140. return;
  141. } else {
  142. _lastRegisterSaveTime = JC.CS.Utility.GetTimestamp();
  143. }
  144. StartCoroutine(LoginController.ins.CompleteUserInfo(
  145. usrRecord, pwdRecord, nickname.text, gender, birthday.text,
  146. countryCode, stateCode, cityCode
  147. ,(res) => {
  148. PopupMgr.ins.ShowTip(res.msg);
  149. if (res.code == 0) {
  150. GameObject.FindObjectOfType<LoginMgr>().showLoginView();
  151. }
  152. }
  153. ));
  154. }
  155. #region Picker
  156. [SerializeField] GameObject datePickerPrefab;
  157. public void OpenDatePicker() {
  158. GameObject o = GameObject.Instantiate(datePickerPrefab);
  159. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  160. GetInputField(registerInBirthday).text = picker.GetSelectDateStr();
  161. };
  162. }
  163. [SerializeField] GameObject locationPickerPrefab;
  164. private string countryCode = "", stateCode = "", cityCode = "";
  165. public void OpenLocationPicker() {
  166. GameObject o = GameObject.Instantiate(locationPickerPrefab);
  167. o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  168. countryCode = info.GetCountryRegion().Item2;
  169. stateCode = info.GetState().Item2;
  170. cityCode = info.GetCity().Item2;
  171. GetInputField(registerInLocation).text =
  172. info.GetCountryRegion().Item1 + " " +
  173. info.GetState().Item1 + " " +
  174. info.GetCity().Item1;
  175. };
  176. }
  177. #endregion
  178. }