RegisterView.cs 7.1 KB

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