using System; using System.Windows.Forms; using 報到系統客戶端.Data.Repositories; using 報到系統客戶端.Models; using 報到系統客戶端.Services; namespace 報到系統客戶端 { /// /// Session 狀態改變事件委派 /// public delegate void OnModeChangeEventHandler(object sender, ModeChangeEventArgs e); /// /// Session 狀態改變事件參數 /// public class ModeChangeEventArgs : EventArgs { public bool OnlineMode { get; set; } public string UserID { get; set; } public string UserName { get; set; } public DateTime ChangeTime { get; set; } } /// /// 客戶端 Session 管理 /// 使用新的 ApiService 進行 API 呼叫 /// public class ClientSession { private static ClientSession instance = null; private static readonly object lockObject = new object(); private string userID = string.Empty; private string userName = string.Empty; private bool onlineMode = false; private DateTime loginTime = DateTime.MinValue; /// /// Session 狀態改變事件 /// public static event OnModeChangeEventHandler OnModeChange; private ClientSession() { } private static void InitInstance() { if (instance == null) { lock (lockObject) { if (instance == null) instance = new ClientSession(); } } } /// /// 觸發 Session 狀態改變事件 /// private static void RaiseModeChangeEvent() { OnModeChange?.Invoke(null, new ModeChangeEventArgs { OnlineMode = instance.onlineMode, UserID = instance.userID, UserName = instance.userName, ChangeTime = DateTime.Now }); } /// /// 程式版本號 /// public static string Version { get { var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; return string.Format("版本 {0}.{1}.{2}", version.Major, version.Minor, version.Build); // Build: 8 // Major: 2026 // MajorRevision: 0 // Minor: 2 // MinorRevision: 0 // Revision: 0 } } /// /// 取得目前登入的使用者ID /// public static string UserID { get { InitInstance(); return instance.userID; } } /// /// 取得目前登入的使用者名稱 /// public static string UserName { get { InitInstance(); return instance.userName; } } /// /// 取得是否為連線模式 /// public static bool OnlineMode { get { InitInstance(); return instance.onlineMode; } } /// /// 取得登入時間 /// public static DateTime LoginTime { get { InitInstance(); return instance.loginTime; } } /// /// 設定登入資訊 /// internal static void SetLoginInfo(string userID, string userName, bool onLineMode) { InitInstance(); instance.userID = userID; instance.userName = userName; instance.onlineMode = onLineMode; instance.loginTime = DateTime.Now; RaiseModeChangeEvent(); } /// /// 檢查是否已登入,若未登入則導向登入畫面 /// public static void CheckLogin() { InitInstance(); BaseResponse response = LoginService.CheckSession(); if (!response.Success) { ShowLoginForm(); } } /// /// 進行心跳檢測 /// public static void HeartBeat() { InitInstance(); LoginService.CheckSession(); } /// /// 進行通訊測試 /// public static bool Ping() { InitInstance(); return LoginService.Ping(); } /// /// 執行登入(非同步版本) /// public static async System.Threading.Tasks.Task<(bool, string)> LoginAsync(string username, string password) { BaseResponse response = await LoginService.LoginAsync(username, password); if (response.Success) { try { // 解析登入回應資料 dynamic data = response.Data; string returnedUserID = data?.UserID ?? username; string returnedUsername = data?.UserName ?? username; SetLoginInfo(returnedUserID, returnedUsername, true); return (true, string.Empty); } catch (Exception ex) { SetLoginInfo(string.Empty, string.Empty, false); return (false, string.Format("登入錯誤:{0}", ex.Message)); } } return (false, response.Message); } /// /// 執行登入(同步版本) /// public static bool Login(string username, string password, out string message) { message = string.Empty; BaseResponse response = LoginService.Login(username, password); if (response.Success) { try { // 解析登入回應資料 dynamic data = response.Data; string returnedUserID = data?.UserID ?? username; string returnedUsername = data?.UserName ?? username; SetLoginInfo(returnedUserID, returnedUsername, true); return true; } catch (Exception ex) { SetLoginInfo(string.Empty, string.Empty, false); message = string.Format("登入錯誤:{0}", ex.Message); return false; } } message = response.Message; return false; } /// /// 執行登出 /// public static void Logout() { InitInstance(); LoginService.Logout(); LoginService.ClearSession(); instance.userID = string.Empty; instance.userName = string.Empty; instance.onlineMode = false; instance.loginTime = DateTime.MinValue; RaiseModeChangeEvent(); } /// /// 顯示登入表單 /// public static void ShowLoginForm() { var loginForm = new LoginForm(); if (loginForm.ShowDialog() == DialogResult.Cancel) { Application.Exit(); } } public static (bool, string) LoginOffline(string bpeno) { try { BPSNRepository bpsnRepo = new BPSNRepository(); var bpsn = bpsnRepo.GetByBPENO(bpeno); if (bpsn == null) { return (false, "登入帳號錯誤"); } SetLoginInfo(bpsn.BPENO, bpsn.BPNME, false); // 離線模式不驗證密碼 // string combined = bpsn.BPENO + password; // using (SHA1 sha1 = SHA1.Create()) // { // byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(combined)); // if (string.Compare(bpsn.BPAWD, Convert.ToBase64String(hash)) != 0) // { // return (false, "帳號或密碼錯誤"); // } // } return (true, string.Empty); } catch (Exception ex) { SetLoginInfo(string.Empty, string.Empty, false); return (false, string.Format("登入錯誤:{0}", ex.Message)); } } } }