RegisterView.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. JC.CS.Throttler throttlerRegisterNext = new JC.CS.Throttler(2000);
  80. public void RegisterNext()
  81. {
  82. InputField user = GetInputField(registerInUser);
  83. if (user.text.Length < 6) {
  84. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("用户名长度至少6位"));
  85. return;
  86. }
  87. InputField pwd1 = GetInputField(registerInPWD1);
  88. if (pwd1.text.Length < 6) {
  89. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("密码长度至少6位"));
  90. return;
  91. }
  92. InputField pwd2 = GetInputField(registerInPWD2);
  93. if (pwd1.text != pwd2.text) {
  94. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("两次输入的密码不一致"));
  95. return;
  96. }
  97. InputField captcha = GetInputField(registerInCaptcha);
  98. if (!captcha.text.Equals(captcha_Register.ToString())) {
  99. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  100. return;
  101. }
  102. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  103. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  104. return;
  105. }
  106. if (throttlerRegisterNext.CanPass() == false) {
  107. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  108. return;
  109. }
  110. usrRecord = user.text;
  111. pwdRecord = pwd1.text;
  112. StartCoroutine(LoginController.ins.Register(
  113. usrRecord, pwdRecord, (res) => {
  114. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  115. if (res.code == 0) {
  116. InitPage(true); //前往完善用户信息
  117. LoginView.ins.FillLoginInput(usrRecord, pwdRecord);
  118. }
  119. }
  120. ));
  121. }
  122. JC.CS.Throttler throttlerRegisterSave = new JC.CS.Throttler(2000);
  123. public void RegisterSave() {
  124. InputField nickname = GetInputField(registerInNickname);
  125. if (nickname.text.Trim().Length == 0) {
  126. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入游戏昵称"));
  127. return;
  128. }
  129. int gender = 0;
  130. Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
  131. for (int i = 0; i < toggleGroup.childCount; i++) {
  132. if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
  133. gender = i == 0 ? 1 : 2;
  134. break;
  135. }
  136. }
  137. InputField birthday = GetInputField(registerInBirthday);
  138. if (birthday.text.Length != 10) {
  139. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("未填写出生日期"));
  140. return;
  141. }
  142. InputField location = GetInputField(registerInLocation);
  143. if (location.text.Length == 0) {
  144. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("未填写所在地区"));
  145. return;
  146. }
  147. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  148. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  149. return;
  150. }
  151. if (throttlerRegisterSave.CanPass() == false) {
  152. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  153. return;
  154. }
  155. StartCoroutine(LoginController.ins.CompleteUserInfo(
  156. usrRecord, pwdRecord, nickname.text, gender, birthday.text,
  157. countryCode, stateCode, cityCode
  158. ,(res) => {
  159. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  160. if (res.code == 0) {
  161. GameObject.FindObjectOfType<LoginMgr>().showLoginView();
  162. }
  163. }
  164. ));
  165. }
  166. #region Picker
  167. [SerializeField] GameObject datePickerPrefab;
  168. public void OpenDatePicker() {
  169. GameObject o = GameObject.Instantiate(datePickerPrefab);
  170. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  171. GetInputField(registerInBirthday).text = picker.GetSelectDateStr();
  172. };
  173. }
  174. [SerializeField] GameObject locationPickerPrefab;
  175. private string countryCode = "", stateCode = "", cityCode = "";
  176. public void OpenLocationPicker() {
  177. GameObject o = GameObject.Instantiate(locationPickerPrefab);
  178. o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  179. countryCode = info.GetCountryRegion().Item2;
  180. stateCode = info.GetState().Item2;
  181. cityCode = info.GetCity().Item2;
  182. GetInputField(registerInLocation).text =
  183. info.GetCountryRegion().Item1 + " " +
  184. info.GetState().Item1 + " " +
  185. info.GetCity().Item1;
  186. };
  187. }
  188. #endregion
  189. }