ResetUIController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using Newtonsoft.Json;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. /* 本文件控制Reset UI document
  6. * 1. 显示和关闭Reset UI
  7. * 2. 显示和隐藏输入框
  8. * 3. 提交数据到服务器
  9. * 4. 显示错误信息
  10. */
  11. public class ResetUIController : MonoBehaviour
  12. {
  13. // Start is called once before the first execution of Update after the MonoBehaviour is created
  14. private Label message, errMsg;
  15. private TextField email, mobile, verification, password;
  16. private Button submit, cancel;
  17. private SceneCondition sceneCondition = SceneCondition.ChineseInitial;
  18. private Dictionary<string, string> errorMessageDict = new();
  19. private string errorText = string.Empty; // 错误信息汇总
  20. void OnEnable()
  21. {
  22. var root = GetComponent<UIDocument>().rootVisualElement;
  23. message = root.Q<Label>("message");
  24. email = root.Q<TextField>("email");
  25. mobile = root.Q<TextField>("mobile");
  26. verification = root.Q<TextField>("verification");
  27. password = root.Q<TextField>("password");
  28. submit = root.Q<Button>("submit");
  29. cancel = root.Q<Button>("cancel");
  30. errMsg = root.Q<Label>("error_msg");
  31. InitSetting();
  32. }
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. FormatCheck();
  37. errMsg.text = errorText;
  38. }
  39. // 初始化语言和显示设定
  40. void InitSetting()
  41. {
  42. // 设置 reset UI界面里面label和按键的语言显示
  43. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "button", "submit", EnviromentSetting.languageCode });
  44. submit.text = textValue;
  45. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "button", "cancel", EnviromentSetting.languageCode });
  46. cancel.text = textValue;
  47. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "mobile", EnviromentSetting.languageCode });
  48. mobile.label = textValue;
  49. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "email", EnviromentSetting.languageCode });
  50. email.label = textValue;
  51. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "verification", EnviromentSetting.languageCode });
  52. verification.label = textValue;
  53. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "password", EnviromentSetting.languageCode });
  54. password.label = textValue;
  55. errorMessageDict.Clear();
  56. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "password_too_short", EnviromentSetting.languageCode });
  57. errorMessageDict.Add("password_too_short", textValue);
  58. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "cannot_find_account", EnviromentSetting.languageCode });
  59. errorMessageDict.Add("cannot_find_account", textValue);
  60. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "wrong_verification_code", EnviromentSetting.languageCode });
  61. errorMessageDict.Add("wrong_verification_code", textValue);
  62. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "email_format_wrong", EnviromentSetting.languageCode });
  63. errorMessageDict.Add("email_format_wrong", textValue);
  64. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "too_many_attempts", EnviromentSetting.languageCode });
  65. errorMessageDict.Add("too_many_attempts", textValue);
  66. cancel.clicked += CancelClick;
  67. submit.clicked += SubmitClick;
  68. if (EnviromentSetting.languageCode == "zh-cn")
  69. {
  70. sceneCondition = SceneCondition.ChineseInitial;
  71. }
  72. else
  73. {
  74. sceneCondition = SceneCondition.OtherLanguageInitial;
  75. }
  76. ConditionSetting(sceneCondition.GetHashCode());
  77. }
  78. // 根据condition的值来显示不同的UI
  79. void ConditionSetting(int conditionValue)
  80. {
  81. switch (conditionValue)
  82. {
  83. case 0:
  84. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 0", EnviromentSetting.languageCode });
  85. message.text = textValue;
  86. email.style.display = DisplayStyle.None;
  87. mobile.style.display = DisplayStyle.Flex;
  88. verification.style.display = DisplayStyle.None;
  89. password.style.display = DisplayStyle.None;
  90. submit.SetEnabled(false);
  91. break;
  92. case 1:
  93. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 1", EnviromentSetting.languageCode });
  94. message.text = textValue;
  95. email.style.display = DisplayStyle.Flex;
  96. mobile.style.display = DisplayStyle.None;
  97. verification.style.display = DisplayStyle.None;
  98. password.style.display = DisplayStyle.None;
  99. submit.SetEnabled(false);
  100. break;
  101. case 2:
  102. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 2", EnviromentSetting.languageCode });
  103. message.text = textValue;
  104. email.SetEnabled(false);
  105. mobile.SetEnabled(false);
  106. verification.style.display = DisplayStyle.Flex;
  107. password.style.display = DisplayStyle.Flex;
  108. submit.SetEnabled(false);
  109. break;
  110. }
  111. }
  112. void SubmitClick()
  113. {
  114. if (sceneCondition.GetHashCode() <= 1)
  115. {
  116. // 申请验证码阶段
  117. RequestVerificationCodePost();
  118. }
  119. else if (sceneCondition.GetHashCode() == 2)
  120. {
  121. // 输入验证码阶段和输入新密码阶段
  122. NewPasswordRequest();
  123. }
  124. }
  125. void RequestVerificationCodePost()
  126. {
  127. // 申请验证码
  128. // POST数据到服务器
  129. string url = "/api/reset/password/step1/";
  130. Dictionary<string, string> formData = new();
  131. WWWForm form = new();
  132. form.AddField("UUID", EnviromentSetting.UUID);
  133. if (EnviromentSetting.languageCode == "zh-cn")
  134. {
  135. form.AddField("mobile", mobile.text);
  136. }
  137. else
  138. {
  139. form.AddField("email", email.text);
  140. }
  141. form.AddField("UUID", EnviromentSetting.UUID);
  142. form.AddField("language_code", EnviromentSetting.languageCode);
  143. StartCoroutine(WebController.PostRequest(url, form, callback: RequestVerificationCodeCallback));
  144. errorText = string.Empty;
  145. }
  146. void RequestVerificationCodeCallback(string json)
  147. {
  148. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  149. if (data != null && data["status"] == "success")
  150. {
  151. if (data["message"] == "verification sent")
  152. {
  153. // 发送验证码成功
  154. sceneCondition = SceneCondition.InputVerificationCodeAndNewPassword;
  155. ConditionSetting(sceneCondition.GetHashCode());
  156. }
  157. }
  158. else if (data != null && data["status"] == "fail")
  159. {
  160. int errorCode = int.Parse(data["code"]);
  161. // 找不到对应的账户
  162. if (errorCode == 610)
  163. {
  164. errorText += errorMessageDict["cannot_find_account"];
  165. }
  166. // 尝试太多次数
  167. if (errorCode == 612)
  168. {
  169. errorText += errorMessageDict["too_many_attempts"];
  170. }
  171. }
  172. }
  173. void NewPasswordRequest()
  174. {
  175. // 检查验证码
  176. string url = "/api/reset/password/step2/";
  177. Dictionary<string, string> formData = new();
  178. WWWForm form = new();
  179. form.AddField("UUID", EnviromentSetting.UUID);
  180. if (EnviromentSetting.languageCode == "zh-cn")
  181. {
  182. form.AddField("mobile", mobile.text);
  183. }
  184. else
  185. {
  186. form.AddField("email", email.text);
  187. }
  188. form.AddField("UUID", EnviromentSetting.UUID);
  189. form.AddField("language_code", EnviromentSetting.languageCode);
  190. form.AddField("verification_code", verification.text);
  191. form.AddField("password", password.text);
  192. StartCoroutine(WebController.PostRequest(url, form, callback: NewPasswordCallback));
  193. errorText = string.Empty;
  194. }
  195. void NewPasswordCallback(string json)
  196. {
  197. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  198. if (data != null && data["status"] == "success")
  199. {
  200. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "new_password_success", EnviromentSetting.languageCode });
  201. MessageBoxController.ShowMessage(msg, () => MaskTransitions.TransitionManager.Instance.LoadLevel("Home"));
  202. }
  203. else if (data != null && data["status"] == "error")
  204. {
  205. int errorCode = int.Parse(data["code"]);
  206. // 验证码错误
  207. if (errorCode == 611)
  208. {
  209. errorText += errorMessageDict["wrong_verification_code"];
  210. }
  211. }
  212. }
  213. void CancelClick()
  214. {
  215. var uiPlaceholder = GameObject.Find("Canvas Placeholder");
  216. if (uiPlaceholder != null)
  217. {
  218. var resetUI = uiPlaceholder.transform.Find("Reset Pasword").gameObject;
  219. var loginCanvas = uiPlaceholder.transform.Find("Login Canvas").gameObject;
  220. resetUI.SetActive(false);
  221. loginCanvas.SetActive(true);
  222. }
  223. }
  224. // 检查输入的内容是否为空,输入不为空的时候Submit按钮可用
  225. void FormatCheck()
  226. {
  227. switch (sceneCondition.GetHashCode())
  228. {
  229. case 0:
  230. {
  231. if (mobile.value != "")
  232. {
  233. submit.SetEnabled(true);
  234. }
  235. else
  236. {
  237. submit.SetEnabled(false);
  238. }
  239. break;
  240. }
  241. case 1:
  242. {
  243. if (email.value != "" && GameTool.IsValidEmail(email.text))
  244. {
  245. submit.SetEnabled(true);
  246. }
  247. else
  248. {
  249. submit.SetEnabled(false);
  250. }
  251. break;
  252. }
  253. case 2:
  254. {
  255. if (verification.value != "" && password.text.Length >= 8)
  256. {
  257. submit.SetEnabled(true);
  258. }
  259. else
  260. {
  261. submit.SetEnabled(false);
  262. }
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. public enum SceneCondition
  269. {
  270. // 0: 中文初始, 1: 其他语言初始, 2: 输入验证码和新的密码
  271. ChineseInitial = 0,
  272. OtherLanguageInitial = 1,
  273. InputVerificationCodeAndNewPassword = 2,
  274. }