410 lines
12 KiB
C#
410 lines
12 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine.Networking;
|
|
using System.Collections;
|
|
|
|
public class AccountUIManager : MonoBehaviour
|
|
{
|
|
[Header("Panels")]
|
|
public GameObject LoginPanel;
|
|
public GameObject RegisterPanel;
|
|
public GameObject VerifyPanel;
|
|
public GameObject AccountPanel;
|
|
|
|
[Header("Login")]
|
|
public InputField LoginIDInput;
|
|
public InputField LoginPasswordInput;
|
|
|
|
[Header("Register")]
|
|
public InputField EmailInput;
|
|
public InputField IDInput;
|
|
public InputField PasswordInput;
|
|
public InputField PasswordVerifyInput;
|
|
public Text ErrorLabel;
|
|
|
|
[Header("Verify")]
|
|
public InputField VerifyInput;
|
|
|
|
private const string GameServerUrl = ServerConfig.GameServerUrl;
|
|
private const string VerifyServerUrl = ServerConfig.VerifyServerUrl;
|
|
|
|
[System.Serializable]
|
|
public class VerifyRequest
|
|
{
|
|
public string game;
|
|
public string email;
|
|
public string code;
|
|
public string password;
|
|
}
|
|
|
|
public class Request
|
|
{
|
|
public string request;
|
|
public string uid;
|
|
public string email;
|
|
public string name;
|
|
public string password;
|
|
public string token;
|
|
}
|
|
|
|
public class Response
|
|
{
|
|
public string status;
|
|
public string reason;
|
|
public string token;
|
|
public int uid;
|
|
public string name;
|
|
public string create_time;
|
|
public string account_type;
|
|
public string email = "";
|
|
public int email_v;
|
|
public string phone = "";
|
|
public int phone_v;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
int uid = PlayerPrefs.GetInt("UID", 0);
|
|
// 新版存 Token;舊版存 Password(進帳號頁時會自動換成 Token)
|
|
bool hasCredential = PlayerPrefs.HasKey("Token") || PlayerPrefs.HasKey("Password");
|
|
|
|
if (uid == 0 || !hasCredential)
|
|
{
|
|
ShowLoginPanel();
|
|
}
|
|
else
|
|
ShowAccountPanel();
|
|
}
|
|
|
|
public void ShowAccountPanel()
|
|
{
|
|
StartCoroutine(AccountFlow());
|
|
|
|
AccountPanel.SetActive(true);
|
|
LoginPanel.SetActive(false);
|
|
RegisterPanel.SetActive(false);
|
|
VerifyPanel.SetActive(false);
|
|
}
|
|
|
|
public void ShowLoginPanel()
|
|
{
|
|
AccountPanel.SetActive(false);
|
|
LoginPanel.SetActive(true);
|
|
RegisterPanel.SetActive(false);
|
|
VerifyPanel.SetActive(false);
|
|
}
|
|
|
|
public void ShowRegisterPanel()
|
|
{
|
|
AccountPanel.SetActive(false);
|
|
LoginPanel.SetActive(false);
|
|
RegisterPanel.SetActive(true);
|
|
VerifyPanel.SetActive(false);
|
|
}
|
|
|
|
public void ShowVerifyPanel()
|
|
{
|
|
AccountPanel.SetActive(false);
|
|
LoginPanel.SetActive(false);
|
|
RegisterPanel.SetActive(false);
|
|
VerifyPanel.SetActive(true);
|
|
}
|
|
|
|
public void LogOut()
|
|
{
|
|
string token = PlayerPrefs.GetString("Token", "");
|
|
if (!string.IsNullOrEmpty(token))
|
|
StartCoroutine(PostJson(GameServerUrl, JsonUtility.ToJson(new Request { request = "logout", token = token }), _ => { }));
|
|
PlayerPrefs.DeleteKey("Token");
|
|
PlayerPrefs.DeleteKey("UID");
|
|
PlayerPrefs.DeleteKey("Password");
|
|
PlayerPrefs.Save();
|
|
|
|
ShowLoginPanel();
|
|
}
|
|
|
|
private void ClearError()
|
|
{
|
|
if (ErrorLabel != null)
|
|
ErrorLabel.text = "";
|
|
}
|
|
|
|
private void ShowError(string message)
|
|
{
|
|
if (ErrorLabel != null)
|
|
ErrorLabel.text = message;
|
|
|
|
Debug.LogError(message);
|
|
}
|
|
|
|
public void SendRegisterData()
|
|
{
|
|
StartCoroutine(RegisterFlow());
|
|
}
|
|
|
|
public void SendVerifyData()
|
|
{
|
|
ClearError();
|
|
StartCoroutine(SendVerifyRequest(PlayerPrefs.GetString("Email", ""), VerifyInput.text.Trim(), Credential()));
|
|
}
|
|
|
|
public void SendLoginData()
|
|
{
|
|
StartCoroutine(LoginFlow());
|
|
}
|
|
|
|
// 驗證用憑證:Token 優先,舊版才用密碼
|
|
private static string Credential()
|
|
{
|
|
string token = PlayerPrefs.GetString("Token", "");
|
|
return string.IsNullOrEmpty(token) ? PlayerPrefs.GetString("Password", "") : token;
|
|
}
|
|
|
|
private IEnumerator SendVerifyRequest(string email, string code = "", string password = "")
|
|
{
|
|
string jsonData = JsonUtility.ToJson(new VerifyRequest
|
|
{
|
|
game = "lunchpad",
|
|
email = email,
|
|
code = code,
|
|
password = password
|
|
});
|
|
|
|
Response response = null;
|
|
yield return PostJson(VerifyServerUrl, jsonData, r => response = r);
|
|
if (response == null) yield break;
|
|
|
|
if (response.status == "send")
|
|
{
|
|
ShowVerifyPanel();
|
|
}
|
|
else if (response.status == "success")
|
|
{
|
|
ShowAccountPanel();
|
|
}
|
|
else if (response.status == "failed" || response.status == "fail")
|
|
{
|
|
if (response.reason == "too_many_requests")
|
|
ShowError("驗證次數過多,請稍後再試");
|
|
else if (response.reason == "game_not_supported")
|
|
ShowError("驗證伺服器錯誤,請稍後再試");
|
|
else if (response.reason == "wrong_code")
|
|
ShowError("驗證碼錯誤");
|
|
else if (response.reason == "expired")
|
|
ShowError("驗證碼已過期,請重新發送");
|
|
else
|
|
ShowError("未知錯誤: " + response.reason);
|
|
}
|
|
else
|
|
{
|
|
ShowError("驗證伺服器回傳異常");
|
|
}
|
|
}
|
|
|
|
// 送出 JSON POST,失敗時顯示錯誤並回傳 null
|
|
private IEnumerator PostJson(string url, string json, System.Action<Response> onDone)
|
|
{
|
|
using (UnityWebRequest www = new UnityWebRequest(url, "POST"))
|
|
{
|
|
www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(json));
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
www.timeout = 10;
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
// 伺服器回 4xx/5xx 時 body 仍是 JSON,嘗試解析
|
|
Response res = null;
|
|
try { res = JsonUtility.FromJson<Response>(www.downloadHandler.text); }
|
|
catch { res = null; }
|
|
|
|
if (res == null || string.IsNullOrEmpty(res.status))
|
|
{
|
|
ShowError(www.result == UnityWebRequest.Result.ConnectionError
|
|
? "無法連線到伺服器" : "伺服器回傳異常");
|
|
onDone(null);
|
|
yield break;
|
|
}
|
|
onDone(res);
|
|
}
|
|
}
|
|
|
|
private IEnumerator RegisterFlow()
|
|
{
|
|
string email = EmailInput.text.Trim();
|
|
string id = IDInput.text.Trim();
|
|
string password = PasswordInput.text;
|
|
string passwordVerify = PasswordVerifyInput.text;
|
|
|
|
ClearError();
|
|
|
|
if (email.Length < 10 || !Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
|
|
{
|
|
ShowError("請輸入有效的電子郵件地址");
|
|
yield break;
|
|
}
|
|
|
|
if(id.Length < 4)
|
|
{
|
|
ShowError("ID 長度不足 4 位");
|
|
yield break;
|
|
}
|
|
|
|
if (password.Length < 6)
|
|
{
|
|
ShowError("密碼長度不足 6 位");
|
|
yield break;
|
|
}
|
|
|
|
if (!Regex.IsMatch(password, @"^(?=.*[a-zA-Z])(?=.*\d).+$"))
|
|
{
|
|
ShowError("密碼需包含英文與數字");
|
|
yield break;
|
|
}
|
|
|
|
if (password != passwordVerify)
|
|
{
|
|
ShowError("兩次輸入密碼不一致");
|
|
yield break;
|
|
}
|
|
|
|
// 檢查 email 是否存在
|
|
var findEmailObj = new Request {
|
|
request = "find_email",
|
|
email = email
|
|
};
|
|
Response res1 = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(findEmailObj), r => res1 = r);
|
|
if (res1 == null) yield break;
|
|
if (res1.status == "found")
|
|
{
|
|
ShowError("信箱已被註冊!");
|
|
yield break;
|
|
}
|
|
|
|
// 檢查 name 是否存在
|
|
var findNameObj = new Request {
|
|
request = "find_name",
|
|
name = id
|
|
};
|
|
Response res2 = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(findNameObj), r => res2 = r);
|
|
if (res2 == null) yield break;
|
|
if (res2.status == "found")
|
|
{
|
|
ShowError("ID 已被使用!");
|
|
yield break;
|
|
}
|
|
|
|
// 開始 create_user
|
|
var createUserObj = new Request {
|
|
request = "create_user",
|
|
email = email,
|
|
name = id,
|
|
password = password
|
|
};
|
|
Response res3 = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(createUserObj), r => res3 = r);
|
|
if (res3 == null) yield break;
|
|
|
|
if (res3.status != "success")
|
|
{
|
|
ShowError("創建帳號失敗: " + res3.reason);
|
|
yield break;
|
|
}
|
|
|
|
// 儲存 email / token → 進入驗證流程(不再儲存密碼)
|
|
PlayerPrefs.SetInt("UID", res3.uid);
|
|
PlayerPrefs.SetString("Email", email);
|
|
PlayerPrefs.SetString("Token", res3.token);
|
|
PlayerPrefs.DeleteKey("Password");
|
|
PlayerPrefs.Save();
|
|
|
|
StartCoroutine(SendVerifyRequest(email));
|
|
}
|
|
|
|
private IEnumerator LoginFlow()
|
|
{
|
|
string id = LoginIDInput.text.Trim();
|
|
string password = LoginPasswordInput.text;
|
|
|
|
ClearError();
|
|
|
|
var loginObj = new Request
|
|
{
|
|
request = "login",
|
|
name = id,
|
|
password = password
|
|
};
|
|
Response res = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(loginObj), r => res = r);
|
|
if (res == null) yield break;
|
|
|
|
if (res.status != "success")
|
|
{
|
|
ShowError("登入失敗: " + res.reason);
|
|
yield break;
|
|
}
|
|
|
|
// 儲存
|
|
PlayerPrefs.SetInt("UID", res.uid);
|
|
PlayerPrefs.SetString("Email", res.email);
|
|
PlayerPrefs.SetString("Token", res.token);
|
|
PlayerPrefs.DeleteKey("Password");
|
|
PlayerPrefs.Save();
|
|
|
|
ShowAccountPanel();
|
|
}
|
|
|
|
private IEnumerator AccountFlow()
|
|
{
|
|
string uid = PlayerPrefs.GetInt("UID", 0).ToString();
|
|
string token = PlayerPrefs.GetString("Token", "");
|
|
string password = PlayerPrefs.GetString("Password", "");
|
|
ClearError();
|
|
|
|
var personObj = new Request
|
|
{
|
|
request = "person_info",
|
|
uid = uid,
|
|
token = token,
|
|
password = string.IsNullOrEmpty(token) ? password : null
|
|
};
|
|
Response res = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(personObj), r => res = r);
|
|
if (res == null) yield break;
|
|
|
|
if (res.status != "success")
|
|
{
|
|
ShowError("載入失敗: " + res.reason);
|
|
yield break;
|
|
}
|
|
|
|
PlayerPrefs.SetString("Name", res.name);
|
|
PlayerPrefs.SetString("CreateTime", res.create_time);
|
|
PlayerPrefs.SetString("Type", res.account_type);
|
|
PlayerPrefs.SetString("Email", res.email);
|
|
PlayerPrefs.SetInt("EmailVerified", res.email_v);
|
|
PlayerPrefs.SetString("Phone", res.phone);
|
|
PlayerPrefs.SetInt("PhoneVerified", res.phone_v);
|
|
PlayerPrefs.Save();
|
|
|
|
// 舊版只存了密碼:重新登入換取 Token,並刪除手機上的密碼
|
|
if (string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(password))
|
|
{
|
|
Response login = null;
|
|
yield return PostJson(GameServerUrl, JsonUtility.ToJson(new Request { request = "login", name = res.name, password = password }), r => login = r);
|
|
if (login != null && login.status == "success" && !string.IsNullOrEmpty(login.token))
|
|
{
|
|
PlayerPrefs.SetString("Token", login.token);
|
|
PlayerPrefs.DeleteKey("Password");
|
|
PlayerPrefs.Save();
|
|
}
|
|
}
|
|
|
|
var display = FindAnyObjectByType<AccountDisplay>();
|
|
if (display != null) display.ShowAccountInfo();
|
|
}
|
|
}
|