using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; // 帳號:登入 / 註冊 / 信箱驗證 / 個人資訊 // 登入後手機上只存 token(不存密碼);舊版存的密碼會在進入個人資訊時換成 token 並刪除 public class AccountPage : Page { public override bool UsesGrid => false; private RectTransform body; private Text error; private bool busy; public override void Enter() { App.Music.Stop(); SongClock.Stop(); var back = Ui.Button(Top, "返回", () => App.Go(new MainPage()), 40); Ui.Place((RectTransform)back.transform, new Vector2(0, 1), new Vector2(40, -30), new Vector2(200, 100)); if (Prefs.LoggedIn) ShowInfo(); else ShowLogin(); } // 換一個面板:清掉內容,回傳新的 Column RectTransform Panel(string title) { if (body != null) Object.Destroy(body.parent.parent.gameObject); var holder = Ui.Stretch(Ui.Rect(Top, "Panel"), 60, 0, 60, 150); body = Ui.Scroll(holder, 28, 10); var t = Ui.Label(body, title, 64, Color.white); t.fontStyle = FontStyle.Bold; Ui.Height(t, 110); error = Ui.Label(body, "", 36, Ui.Danger); error.transform.SetAsLastSibling(); return body; } InputField Field(string label, string placeholder, InputField.ContentType type = InputField.ContentType.Standard) { Ui.Label(body, label, 36, Ui.Muted, TextAnchor.MiddleLeft); return Ui.Height(Ui.Input(body, placeholder, type), 120); } Button Action(string label, System.Action onClick, bool primary = false) => Ui.Height(Ui.Button(body, label, () => { if (!busy) onClick(); }, 46, primary), 120); void ShowError(string msg) { error.text = msg; error.transform.SetAsLastSibling(); } // ---------- 登入 ---------- void ShowLogin() { Panel("登入帳號"); var id = Field("ID", "輸入你的 ID"); var pw = Field("密碼", "輸入你的密碼", InputField.ContentType.Password); Action("登入", () => App.StartCoroutine(Login(id.text.Trim(), pw.text)), primary: true); Action("註冊帳號", ShowRegister); Ui.Label(body, "曲包網站也使用同一組帳號登入", 32, Ui.Muted); } IEnumerator Login(string name, string password) { if (name == "" || password == "") { ShowError("請輸入 ID 與密碼"); yield break; } busy = true; Api.Response r = null; yield return Api.Game(new Dictionary { ["request"] = "login", ["name"] = name, ["password"] = password }, x => r = x); busy = false; if (r.error != null) { ShowError(r.error == "cant_find_name" || r.error == "password_wrong" ? "ID 或密碼錯誤" : "登入失敗:" + r.error); yield break; } SaveLogin(r.Int("uid"), r.Str("token"), r.Str("email")); ShowInfo(); } static void SaveLogin(int uid, string token, string email) { PlayerPrefs.SetInt(Prefs.Uid, uid); PlayerPrefs.SetString(Prefs.Token, token); PlayerPrefs.SetString(Prefs.Email, email); PlayerPrefs.DeleteKey(Prefs.LegacyPassword); PlayerPrefs.Save(); } // ---------- 註冊 ---------- void ShowRegister() { Panel("註冊帳號"); var email = Field("電子信箱", "輸入你的信箱", InputField.ContentType.EmailAddress); var id = Field("ID", "自訂一個獨一無二 ID"); var pw = Field("密碼", "至少 6 位,包含英文與數字", InputField.ContentType.Password); var pw2 = Field("確認密碼", "再輸入一次密碼", InputField.ContentType.Password); Action("送出", () => App.StartCoroutine(Register(email.text.Trim(), id.text.Trim(), pw.text, pw2.text)), primary: true); Action("已有帳號,登入", ShowLogin); } IEnumerator Register(string email, string id, string password, string password2) { string problem = !Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$") ? "請輸入有效的電子郵件地址" : id.Length < 4 ? "ID 長度不足 4 位" : id.Length > 30 ? "ID 最多 30 字" : password.Length < 6 ? "密碼長度不足 6 位" : !Regex.IsMatch(password, @"^(?=.*[a-zA-Z])(?=.*\d).+$") ? "密碼需包含英文與數字" : password != password2 ? "兩次輸入密碼不一致" : null; if (problem != null) { ShowError(problem); yield break; } busy = true; Api.Response r = null; yield return Api.Game(new Dictionary { ["request"] = "create_user", ["email"] = email, ["name"] = id, ["password"] = password }, x => r = x); busy = false; if (r.error != null) { ShowError(r.error == "email_exists" ? "信箱已被註冊" : r.error == "name_exists" ? "ID 已被使用" : "建立帳號失敗:" + r.error); yield break; } SaveLogin(r.Int("uid"), r.Str("token"), email); App.RefreshFooter(); yield return SendCode(email); } // ---------- 信箱驗證 ---------- IEnumerator SendCode(string email) { busy = true; Api.Response r = null; yield return Api.Verify(new Dictionary { ["game"] = "lunchpad", ["email"] = email, ["code"] = "", ["password"] = "" }, x => r = x); busy = false; if (r.error != null && r.error != "too_many_requests") { ShowError("驗證信寄送失敗:" + r.error); yield break; } ShowVerify(email); if (r.error == "too_many_requests") ShowError("驗證信已寄出,請稍候再重新發送"); } void ShowVerify(string email) { Panel("驗證信箱"); Ui.Label(body, $"驗證信已寄到 {email}\n請檢查收件匣或垃圾郵件,驗證碼 5 分鐘內有效", 36, Ui.Muted); var code = Field("驗證碼", "6 位數字", InputField.ContentType.IntegerNumber); code.characterLimit = 6; Action("驗證", () => App.StartCoroutine(Verify(email, code.text.Trim())), primary: true); Action("重新發送", () => App.StartCoroutine(SendCode(email))); Action("稍後再驗證", ShowInfo); } IEnumerator Verify(string email, string code) { busy = true; Api.Response r = null; yield return Api.Verify(new Dictionary { ["game"] = "lunchpad", ["email"] = email, ["code"] = code, ["password"] = Credential(), }, x => r = x); busy = false; if (r.error != null) { ShowError(r.error switch { "wrong_code" => "驗證碼錯誤", "expired" => "驗證碼已過期,請重新發送", "not_requested" => "請先發送驗證碼", _ => "驗證失敗:" + r.error, }); yield break; } Toast.Show("信箱驗證完成"); ShowInfo(); } // 驗證用憑證:Token 優先,舊版才用密碼 static string Credential() { string token = PlayerPrefs.GetString(Prefs.Token, ""); return token != "" ? token : PlayerPrefs.GetString(Prefs.LegacyPassword, ""); } // ---------- 個人資訊 ---------- void ShowInfo() { App.RefreshFooter(); Panel("個人資訊"); var info = Ui.Label(body, "讀取中…", 40, Ui.TextColor, TextAnchor.UpperLeft); info.lineSpacing = 1.3f; var verifyButton = Action("驗證信箱", () => App.StartCoroutine(SendCode(PlayerPrefs.GetString(Prefs.Email, "")))); verifyButton.gameObject.SetActive(false); Action("重新整理", ShowInfo); Action("登出", LogOut); App.StartCoroutine(LoadInfo(info, verifyButton)); } IEnumerator LoadInfo(Text info, Button verifyButton) { string token = PlayerPrefs.GetString(Prefs.Token, ""); string password = PlayerPrefs.GetString(Prefs.LegacyPassword, ""); Api.Response r = null; yield return Api.Game(new Dictionary { ["request"] = "person_info", ["uid"] = Prefs.UserId, ["token"] = token, ["password"] = token == "" ? password : null, }, x => r = x); if (info == null) yield break; // 已經換面板 if (r.error != null) { info.text = r.error == "auth_failed" ? "登入已過期,請重新登入" : "載入失敗:" + r.error; if (r.error == "auth_failed") ClearLogin(); yield break; } PlayerPrefs.SetString(Prefs.Name, r.Str("name")); PlayerPrefs.SetString(Prefs.CreateTime, r.Str("create_time")); PlayerPrefs.SetString(Prefs.AccountType, r.Str("account_type")); PlayerPrefs.SetString(Prefs.Email, r.Str("email")); PlayerPrefs.SetInt(Prefs.EmailVerified, r.Int("email_v")); PlayerPrefs.Save(); App.RefreshFooter(); // 舊版只存了密碼:重新登入換取 Token,並刪除手機上的密碼 if (token == "" && password != "") { Api.Response login = null; yield return Api.Game(new Dictionary { ["request"] = "login", ["name"] = r.Str("name"), ["password"] = password }, x => login = x); if (login.error == null && login.Str("token") != "") SaveLogin(Prefs.UserId, login.Str("token"), r.Str("email")); } string type = r.Str("account_type") switch { "R" => "玩家", "A" => "Alpha 測試", "B" => "Beta 測試", "D" => "開發者", var t => t }; bool verified = r.Int("email_v") == 1; info.text = $"使用者號碼 {Prefs.UidText()}\n" + $"顯示名稱  {r.Str("name")}\n" + $"帳號類型  {type}\n" + $"建立日期  {r.Str("create_time")}\n" + $"電子信箱  {MaskEmail(r.Str("email"))} {(verified ? "已驗證" : "未驗證")}"; verifyButton.gameObject.SetActive(!verified); } static string MaskEmail(string email) { int at = email.IndexOf('@'); return at < 0 ? email : email.Substring(0, Mathf.Min(2, at)) + "*****" + email.Substring(at); } void LogOut() { string token = PlayerPrefs.GetString(Prefs.Token, ""); if (token != "") App.StartCoroutine(Api.Game(new Dictionary { ["request"] = "logout", ["token"] = token }, _ => { })); ClearLogin(); ShowLogin(); } void ClearLogin() { foreach (var k in new[] { Prefs.Token, Prefs.Uid, Prefs.LegacyPassword, Prefs.Name, Prefs.AccountType, Prefs.CreateTime, Prefs.EmailVerified }) PlayerPrefs.DeleteKey(k); PlayerPrefs.Save(); App.RefreshFooter(); } public override void Back() => App.Go(new MainPage()); }