|
@@ -0,0 +1,311 @@
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Net.Mail;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.UIElements;
|
|
|
+
|
|
|
+/* 本文件控制Reset UI document
|
|
|
+ * 1. 显示和关闭Reset UI
|
|
|
+ * 2. 显示和隐藏输入框
|
|
|
+ * 3. 提交数据到服务器
|
|
|
+ * 4. 显示错误信息
|
|
|
+ */
|
|
|
+
|
|
|
+public class ResetUIController : MonoBehaviour
|
|
|
+{
|
|
|
+ // Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
+ private Label message, errMsg;
|
|
|
+ private TextField email, mobile, verification, password;
|
|
|
+ private Button submit, cancel;
|
|
|
+ private int condition = 0; // 0: 中文初始, 1: 其他语言初始, 2: 输入验证码和新的密码
|
|
|
+ private Dictionary<string, string> errorMessageDict = new();
|
|
|
+ private string errorText = string.Empty; // 错误信息汇总
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ var root = GetComponent<UIDocument>().rootVisualElement;
|
|
|
+ message = root.Q<Label>("message");
|
|
|
+ email = root.Q<TextField>("email");
|
|
|
+ mobile = root.Q<TextField>("mobile");
|
|
|
+ verification = root.Q<TextField>("verification");
|
|
|
+ password = root.Q<TextField>("password");
|
|
|
+ submit = root.Q<Button>("submit");
|
|
|
+ cancel = root.Q<Button>("cancel");
|
|
|
+ errMsg = root.Q<Label>("error_msg");
|
|
|
+
|
|
|
+ InitSetting();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update is called once per frame
|
|
|
+ void Update()
|
|
|
+ {
|
|
|
+ FormatCheck();
|
|
|
+ errMsg.text = errorText;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化语言和显示设定
|
|
|
+ void InitSetting()
|
|
|
+ {
|
|
|
+ // 设置 shopping UI界面里面label和按键的语言显示
|
|
|
+ string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "button", "submit", EnviromentSetting.languageCode });
|
|
|
+ submit.text = textValue;
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "button", "cancel", EnviromentSetting.languageCode });
|
|
|
+ cancel.text = textValue;
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "mobile", EnviromentSetting.languageCode });
|
|
|
+ mobile.label = textValue;
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "email", EnviromentSetting.languageCode });
|
|
|
+ email.label = textValue;
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "verification", EnviromentSetting.languageCode });
|
|
|
+ verification.label = textValue;
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "text_field", "password", EnviromentSetting.languageCode });
|
|
|
+ password.label = textValue;
|
|
|
+
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "password_too_short", EnviromentSetting.languageCode });
|
|
|
+ errorMessageDict.Add("password_too_short", textValue);
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "cannot_find_account", EnviromentSetting.languageCode });
|
|
|
+ errorMessageDict.Add("cannot_find_account", textValue);
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "wrong_verification_code", EnviromentSetting.languageCode });
|
|
|
+ errorMessageDict.Add("wrong_verification_code", textValue);
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "error_msg", "email_format_wrong", EnviromentSetting.languageCode });
|
|
|
+ errorMessageDict.Add("email_format_wrong", textValue);
|
|
|
+
|
|
|
+ cancel.clicked += CancelClick;
|
|
|
+ submit.clicked += SubmitClick;
|
|
|
+
|
|
|
+ if (EnviromentSetting.languageCode == "zh-cn")
|
|
|
+ {
|
|
|
+ condition = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ condition = 1;
|
|
|
+ }
|
|
|
+ ConditionSetting(condition);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据condition的值来显示不同的UI
|
|
|
+ void ConditionSetting(int conditionValue)
|
|
|
+ {
|
|
|
+ switch (conditionValue)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 0", EnviromentSetting.languageCode });
|
|
|
+ message.text = textValue;
|
|
|
+ email.style.display = DisplayStyle.None;
|
|
|
+ mobile.style.display = DisplayStyle.Flex;
|
|
|
+ verification.style.display = DisplayStyle.None;
|
|
|
+ password.style.display = DisplayStyle.None;
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 1", EnviromentSetting.languageCode });
|
|
|
+ message.text = textValue;
|
|
|
+ email.style.display = DisplayStyle.Flex;
|
|
|
+ mobile.style.display = DisplayStyle.None;
|
|
|
+ verification.style.display = DisplayStyle.None;
|
|
|
+ password.style.display = DisplayStyle.None;
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 2", EnviromentSetting.languageCode });
|
|
|
+ message.text = textValue;
|
|
|
+ email.SetEnabled(false);
|
|
|
+ mobile.SetEnabled(false);
|
|
|
+ verification.style.display = DisplayStyle.Flex;
|
|
|
+ password.style.display = DisplayStyle.Flex;
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ break;
|
|
|
+ //case 3:
|
|
|
+ // textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 3", EnviromentSetting.languageCode });
|
|
|
+ // message.text = textValue;
|
|
|
+ // email.SetEnabled(false);
|
|
|
+ // mobile.SetEnabled(false);
|
|
|
+ // verification.style.display = DisplayStyle.Flex;
|
|
|
+ // password.style.display = DisplayStyle.None;
|
|
|
+ // submit.SetEnabled(false);
|
|
|
+ // break;
|
|
|
+ //case 4:
|
|
|
+ // textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "message 4", EnviromentSetting.languageCode });
|
|
|
+ // message.text = textValue;
|
|
|
+ // email.SetEnabled(false);
|
|
|
+ // mobile.SetEnabled(false);
|
|
|
+ // verification.style.display = DisplayStyle.None;
|
|
|
+ // password.style.display = DisplayStyle.Flex;
|
|
|
+ // submit.SetEnabled(false);
|
|
|
+ // break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void SubmitClick()
|
|
|
+ {
|
|
|
+ if (condition <= 1)
|
|
|
+ {
|
|
|
+ // 申请验证码阶段
|
|
|
+ RequestVerificationCodePost();
|
|
|
+ }
|
|
|
+ else if (condition ==2)
|
|
|
+ {
|
|
|
+ // 输入验证码阶段和输入新密码阶段
|
|
|
+ NewPassowrdRequest();
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ void RequestVerificationCodePost()
|
|
|
+ {
|
|
|
+
|
|
|
+ // 申请验证码
|
|
|
+ // POST数据到服务器
|
|
|
+ string url = "/api/reset/password/step1/";
|
|
|
+ Dictionary<string, string> formData = new();
|
|
|
+ WWWForm form = new();
|
|
|
+ form.AddField("UUID", EnviromentSetting.UUID);
|
|
|
+ if (EnviromentSetting.languageCode == "zh-cn")
|
|
|
+ {
|
|
|
+ form.AddField("mobile", mobile.text);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ form.AddField("email", email.text);
|
|
|
+ }
|
|
|
+ form.AddField("UUID", EnviromentSetting.UUID);
|
|
|
+ form.AddField("language_code", EnviromentSetting.languageCode);
|
|
|
+ StartCoroutine(WebController.PostRequest(url, form, callback: RequestVerificationCodeCallback));
|
|
|
+ errorText = string.Empty;
|
|
|
+ }
|
|
|
+
|
|
|
+ void RequestVerificationCodeCallback(string json)
|
|
|
+ {
|
|
|
+ var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|
|
+ if (data != null && data["status"] == "success")
|
|
|
+ {
|
|
|
+ if (data["message"] == "verification sent")
|
|
|
+ {
|
|
|
+ // 发送验证码成功
|
|
|
+ condition = 2;
|
|
|
+ ConditionSetting(condition);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (data != null && data["status"] == "error")
|
|
|
+ {
|
|
|
+ // 找不到对应的账户
|
|
|
+ if (data["message"] == "cannot find account")
|
|
|
+ {
|
|
|
+ errorText += errorMessageDict["cannot_find_account"];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void NewPassowrdRequest()
|
|
|
+ {
|
|
|
+ // 检查验证码
|
|
|
+ string url = "/api/reset/password/step2/";
|
|
|
+ Dictionary<string, string> formData = new();
|
|
|
+ WWWForm form = new();
|
|
|
+ form.AddField("UUID", EnviromentSetting.UUID);
|
|
|
+ if (EnviromentSetting.languageCode == "zh-cn")
|
|
|
+ {
|
|
|
+ form.AddField("mobile", mobile.text);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ form.AddField("email", email.text);
|
|
|
+ }
|
|
|
+ form.AddField("UUID", EnviromentSetting.UUID);
|
|
|
+ form.AddField("language_code", EnviromentSetting.languageCode);
|
|
|
+ form.AddField("verification_code", verification.text);
|
|
|
+ form.AddField("password", password.text);
|
|
|
+ StartCoroutine(WebController.PostRequest(url, form, callback: NewPasswordCallback));
|
|
|
+ errorText = string.Empty;
|
|
|
+ }
|
|
|
+
|
|
|
+ void NewPasswordCallback(string json)
|
|
|
+ {
|
|
|
+ var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
|
|
|
+ if (data != null && data["status"] == "success")
|
|
|
+ {
|
|
|
+ string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "resetUI", "label", "new_password_success", EnviromentSetting.languageCode });
|
|
|
+ MessageBoxController.ShowMessage(msg, () => MaskTransitions.TransitionManager.Instance.LoadLevel("Home"));
|
|
|
+ }
|
|
|
+ else if (data != null && data["status"] == "error")
|
|
|
+ {
|
|
|
+ // 验证码错误
|
|
|
+ if (data["message"] == "wrong verification code")
|
|
|
+ {
|
|
|
+ errorText += errorMessageDict["wrong_verification_code"];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void CancelClick()
|
|
|
+ {
|
|
|
+ var uiPlaceholder = GameObject.Find("Canvas Placeholder");
|
|
|
+ if (uiPlaceholder != null)
|
|
|
+ {
|
|
|
+ var resetUI = uiPlaceholder.transform.Find("Reset Pasword").gameObject;
|
|
|
+ var loginCanvas = uiPlaceholder.transform.Find("Login Canvas").gameObject;
|
|
|
+ resetUI.SetActive(false);
|
|
|
+ loginCanvas.SetActive(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查输入的内容是否为空,输入不为空的时候Submit按钮可用
|
|
|
+
|
|
|
+ void FormatCheck()
|
|
|
+ {
|
|
|
+ switch (condition)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ {
|
|
|
+ if (mobile.value != "")
|
|
|
+ {
|
|
|
+ submit.SetEnabled(true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 1:
|
|
|
+ {
|
|
|
+ if (email.value != "" && IsValidEmail(email.text))
|
|
|
+ {
|
|
|
+ submit.SetEnabled(true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2:
|
|
|
+ {
|
|
|
+ if (verification.value != "" && password.text.Length>=8)
|
|
|
+ {
|
|
|
+ submit.SetEnabled(true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ submit.SetEnabled(false);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 格式验证区
|
|
|
+ // email 格式检测
|
|
|
+ private bool IsValidEmail(string email)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var addr = new MailAddress(email);
|
|
|
+ return addr.Address == email;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+}
|