RegisterView.cs 7.9 KB

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