RegisterView.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. [SerializeField] Text registerTip;
  25. void OnEnable()
  26. {
  27. InitPage();
  28. }
  29. void Start()
  30. {
  31. InitInputLimit();
  32. }
  33. void InitInputLimit() {
  34. GameObject[] inputNodes = {registerInUser, registerInPWD1, registerInPWD2};
  35. foreach (var inputNode in inputNodes) {
  36. InputField inputField = GetInputField(inputNode);
  37. inputField.onValueChanged.AddListener(delegate(string text) {
  38. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  39. if (match.Success) {
  40. inputField.text = text.Replace(match.Value, "");
  41. }
  42. });
  43. }
  44. }
  45. void InitPage(bool isNext = false) {
  46. registerInUser.SetActive(!isNext);
  47. registerInPWD1.SetActive(!isNext);
  48. registerInPWD2.SetActive(!isNext);
  49. registerInCaptcha.SetActive(!isNext);
  50. btnNext.SetActive(!isNext);
  51. registerInNickname.SetActive(isNext);
  52. registerInGender.SetActive(isNext);
  53. registerInBirthday.SetActive(isNext);
  54. registerInLocation.SetActive(isNext);
  55. btnSave.SetActive(isNext);
  56. if (!isNext) {
  57. if (CaptchaController.ins.captcha_Register < 0) {
  58. StartCoroutine(CaptchaController.ins.GetCaptcha(
  59. CaptchaController.CaptchaType.Register,
  60. registerInCaptcha.transform.Find("CodeImage").GetComponent<Image>()
  61. ));
  62. }
  63. }
  64. }
  65. InputField GetInputField(GameObject inputNode) {
  66. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  67. }
  68. private string usrRecord = "";
  69. private string pwdRecord = "";
  70. public void RegisterNext()
  71. {
  72. InputField user = GetInputField(registerInUser);
  73. if (user.text.Length < 6) {
  74. registerTip.color = Color.red;
  75. registerTip.GetComponent<TextAutoLanguage>().SetText(46);
  76. return;
  77. }
  78. InputField pwd1 = GetInputField(registerInPWD1);
  79. if (pwd1.text.Length < 6) {
  80. registerTip.color = Color.red;
  81. registerTip.GetComponent<TextAutoLanguage>().SetText(47);
  82. return;
  83. }
  84. InputField pwd2 = GetInputField(registerInPWD2);
  85. if (pwd1.text != pwd2.text) {
  86. registerTip.color = Color.red;
  87. registerTip.GetComponent<TextAutoLanguage>().SetText(48);
  88. return;
  89. }
  90. InputField captcha = GetInputField(registerInCaptcha);
  91. if (!captcha.text.Equals(CaptchaController.ins.captcha_Register.ToString())) {
  92. Debug.Log("验证码错误");
  93. return;
  94. }
  95. usrRecord = user.text;
  96. pwdRecord = pwd1.text;
  97. StartCoroutine(LoginController.ins.Register(
  98. usrRecord, pwdRecord, (res) => {
  99. Debug.Log(res.msg);
  100. if (res.code == 0) {
  101. InitPage(true); //前往完善用户信息
  102. }
  103. }
  104. ));
  105. // InputField nickname = GetInputField(registerInNickname);
  106. // if (nickname.text.Trim().Length == 0) {
  107. // registerTip.color = Color.yellow;
  108. // registerTip.GetComponent<TextAutoLanguage>().SetText(49);
  109. // return;
  110. // }
  111. // UserInfos userInfos = GetUserInfos();
  112. // foreach (var userInfo in userInfos.list)
  113. // {
  114. // if (userInfo.user == user.text) {
  115. // registerTip.color = Color.yellow;
  116. // registerTip.GetComponent<TextAutoLanguage>().SetText(50);
  117. // return;
  118. // }
  119. // }
  120. // UserInfo userInfo1 = new UserInfo();
  121. // userInfo1.user = user.text;
  122. // userInfo1.pwd = pwd1.text;
  123. // userInfo1.nickname = nickname.text;
  124. // Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
  125. // for (int i = 0; i < toggleGroup.childCount; i++) {
  126. // if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
  127. // userInfo1.gender = i == 0 ? 1 : 2;
  128. // break;
  129. // }
  130. // }
  131. // userInfos.list.Add(userInfo1);
  132. // SetUserInfos(userInfos);
  133. // registerTip.color = Color.green;
  134. // registerTip.GetComponent<TextAutoLanguage>().SetText(51);
  135. // //自动跳转到登录并填写登录信息
  136. // GetInputField(loginInUser).text = userInfo1.user;
  137. // GetInputField(loginInPWD).text = userInfo1.pwd;
  138. // SelectLoginMode(1);
  139. // Invoke("showLoginView", 0.5f);
  140. }
  141. public void RegisterSave() {
  142. InputField nickname = GetInputField(registerInNickname);
  143. if (nickname.text.Trim().Length == 0) {
  144. registerTip.color = Color.yellow;
  145. registerTip.GetComponent<TextAutoLanguage>().SetText(49);
  146. return;
  147. }
  148. int gender = 0;
  149. Transform toggleGroup = registerInGender.transform.Find("ToggleGroup");
  150. for (int i = 0; i < toggleGroup.childCount; i++) {
  151. if (toggleGroup.GetChild(i).GetComponent<Toggle>().isOn) {
  152. gender = i == 0 ? 1 : 2;
  153. break;
  154. }
  155. }
  156. InputField birthday = GetInputField(registerInBirthday);
  157. if (birthday.text.Length != 10) {
  158. Debug.Log("未填写出生日期");
  159. return;
  160. }
  161. InputField location = GetInputField(registerInLocation);
  162. if (location.text.Length == 0) {
  163. Debug.Log("未填写所在地区");
  164. return;
  165. }
  166. StartCoroutine(LoginController.ins.CompleteUserInfo(
  167. usrRecord, pwdRecord, nickname.text, gender, birthday.text,
  168. countryCode, stateCode, cityCode
  169. ,(res) => {
  170. Debug.Log(res.msg);
  171. if (res.code == 0) {
  172. GameObject.FindObjectOfType<LoginMgr>().showLoginView();
  173. }
  174. }
  175. ));
  176. }
  177. #region Picker
  178. [SerializeField] GameObject datePickerPrefab;
  179. public void OpenDatePicker() {
  180. GameObject o = GameObject.Instantiate(datePickerPrefab);
  181. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  182. GetInputField(registerInBirthday).text = picker.GetSelectDateStr();
  183. };
  184. }
  185. [SerializeField] GameObject locationPickerPrefab;
  186. private string countryCode = "", stateCode = "", cityCode = "";
  187. public void OpenLocationPicker() {
  188. GameObject o = GameObject.Instantiate(locationPickerPrefab);
  189. o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  190. countryCode = info.GetCountryRegion().Item2;
  191. stateCode = info.GetState().Item2;
  192. cityCode = info.GetCity().Item2;
  193. GetInputField(registerInLocation).text =
  194. info.GetCountryRegion().Item1 + " " +
  195. info.GetState().Item1 + " " +
  196. info.GetCity().Item1;
  197. };
  198. }
  199. #endregion
  200. }