feat: add login
This commit is contained in:
@@ -10,29 +10,193 @@ namespace 全自动水压检测仪
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序的主入口点。
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
|
||||
static void Main()
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
|
||||
//Coeffiicientsetting coeffiicientsetting = new Coeffiicientsetting();
|
||||
//HighTemperatureMode highTemperatureMode = new HighTemperatureMode();
|
||||
//NormalTemperatureMode normalTemperatureMode = new NormalTemperatureMode();
|
||||
try
|
||||
{
|
||||
// 测试密码加密功能
|
||||
System.Diagnostics.Debug.WriteLine("\n========================================");
|
||||
System.Diagnostics.Debug.WriteLine("程序启动 - 密码加密测试");
|
||||
System.Diagnostics.Debug.WriteLine("========================================");
|
||||
PasswordHelper.TestPasswordEncryption();
|
||||
|
||||
//FormManager.Instance
|
||||
// .RegisterForm(coeffiicientsetting)
|
||||
// .RegisterForm(highTemperatureMode)
|
||||
// .RegisterForm(normalTemperatureMode);
|
||||
// 检查是否有重置参数
|
||||
bool forceReset = args.Length > 0 && args[0].ToLower() == "--reset-users";
|
||||
|
||||
if (forceReset)
|
||||
{
|
||||
var result = MessageBox.Show(
|
||||
"确定要重置默认用户吗?\n\n这将删除并重新创建:\n• admin / admin123 (管理员)\n• cgx / 123 (普通用户)\n\n其他用户不受影响。",
|
||||
"重置默认用户",
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Question);
|
||||
|
||||
//Application.Run(normalTemperatureMode);
|
||||
Application.Run(new ScanImport());
|
||||
if (result == DialogResult.Yes)
|
||||
{
|
||||
DatabaseInitializer.Initialize();
|
||||
DatabaseInitializer.ForceResetDefaultUsers();
|
||||
MessageBox.Show("默认用户已重置!\n\n管理员: admin / admin123\n普通用户: cgx / 123",
|
||||
"重置成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化数据库表和默认管理员账户
|
||||
System.Diagnostics.Debug.WriteLine("\n========================================");
|
||||
System.Diagnostics.Debug.WriteLine("初始化数据库");
|
||||
System.Diagnostics.Debug.WriteLine("========================================");
|
||||
DatabaseInitializer.Initialize();
|
||||
|
||||
// 检查并修复损坏的密码数据
|
||||
System.Diagnostics.Debug.WriteLine("\n========================================");
|
||||
System.Diagnostics.Debug.WriteLine("检查密码数据完整性");
|
||||
System.Diagnostics.Debug.WriteLine("========================================");
|
||||
CheckAndFixPasswordData();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"数据库初始化失败:{ex.Message}\n\n请检查:\n1. MySQL服务是否启动\n2. 数据库连接配置是否正确\n3. 数据库 fullautowaterpressure 是否存在\n\n应用程序将退出。",
|
||||
"初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示登录窗体
|
||||
using (var loginForm = new LoginForm())
|
||||
{
|
||||
if (loginForm.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
// 登录成功,根据用户权限显示对应窗体
|
||||
if (LoginData.IsAdmin())
|
||||
{
|
||||
// 管理员登录,显示 ScanImport 页面
|
||||
Application.Run(new ScanImport());
|
||||
}
|
||||
else
|
||||
{
|
||||
// 普通用户登录,显示 NormalTemperatureMode 页面
|
||||
Application.Run(new NormalTemperatureMode());
|
||||
}
|
||||
}
|
||||
// 登录失败或取消,直接退出
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查并修复损坏的密码数据
|
||||
/// </summary>
|
||||
private static void CheckAndFixPasswordData()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userRepo = new UserRepository();
|
||||
|
||||
// 检查默认用户
|
||||
var adminUser = userRepo.GetUserByUsername("admin");
|
||||
var cgxUser = userRepo.GetUserByUsername("cgx");
|
||||
|
||||
bool needsReset = false;
|
||||
|
||||
// 检查 admin 用户
|
||||
if (adminUser != null)
|
||||
{
|
||||
string storedPassword = adminUser.PasswordHash ?? "";
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"\n检查 admin 用户:");
|
||||
System.Diagnostics.Debug.WriteLine($" 当前密码值: '{storedPassword}'");
|
||||
System.Diagnostics.Debug.WriteLine($" 密码长度: {storedPassword.Length}");
|
||||
|
||||
// 检查是否为空
|
||||
if (string.IsNullOrEmpty(storedPassword))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 密码为空");
|
||||
needsReset = true;
|
||||
}
|
||||
// 检查是否是旧的加密格式(包含 Base64 特殊字符)
|
||||
else if (storedPassword.Length > 20 && (storedPassword.Contains("+") || storedPassword.Contains("/") || storedPassword.Contains("=")))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 旧的加密格式");
|
||||
needsReset = true;
|
||||
}
|
||||
// 检查是否是正确的明文密码
|
||||
else if (storedPassword != "admin123")
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 密码不正确(应该是 'admin123')");
|
||||
needsReset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: ✓ 正确");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"\nadmin 用户不存在");
|
||||
needsReset = true;
|
||||
}
|
||||
|
||||
// 检查 cgx 用户
|
||||
if (cgxUser != null)
|
||||
{
|
||||
string storedPassword = cgxUser.PasswordHash ?? "";
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"\n检查 cgx 用户:");
|
||||
System.Diagnostics.Debug.WriteLine($" 当前密码值: '{storedPassword}'");
|
||||
System.Diagnostics.Debug.WriteLine($" 密码长度: {storedPassword.Length}");
|
||||
|
||||
if (string.IsNullOrEmpty(storedPassword))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 密码为空");
|
||||
needsReset = true;
|
||||
}
|
||||
else if (storedPassword.Length > 20 && (storedPassword.Contains("+") || storedPassword.Contains("/") || storedPassword.Contains("=")))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 旧的加密格式");
|
||||
needsReset = true;
|
||||
}
|
||||
else if (storedPassword != "123")
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: 密码不正确(应该是 '123')");
|
||||
needsReset = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($" 状态: ✓ 正确");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"\ncgx 用户不存在");
|
||||
needsReset = true;
|
||||
}
|
||||
|
||||
if (needsReset)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("\n检测到密码数据异常,自动重置默认用户...");
|
||||
DatabaseInitializer.ForceResetDefaultUsers();
|
||||
System.Diagnostics.Debug.WriteLine("密码数据已修复!");
|
||||
System.Diagnostics.Debug.WriteLine(" admin 密码: admin123");
|
||||
System.Diagnostics.Debug.WriteLine(" cgx 密码: 123");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("\n密码数据检查通过 ✓");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"密码数据检查失败: {ex.Message}");
|
||||
System.Diagnostics.Debug.WriteLine($"详细信息: {ex}");
|
||||
// 不影响程序启动,继续运行
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user