Files

295 lines
13 KiB
C#
Raw Permalink Normal View History

2026-01-27 17:27:59 +08:00
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System;
2026-01-07 13:42:17 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using ;
using .DATA;
2026-01-27 17:27:59 +08:00
using ;
2026-01-07 13:42:17 +08:00
namespace
{
internal static class Program
{
2026-01-26 18:47:27 +08:00
2026-01-07 13:42:17 +08:00
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
2026-01-26 18:47:27 +08:00
static void Main(string[] args)
{
2026-01-07 13:42:17 +08:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2026-01-27 17:27:59 +08:00
PasswordExpirationService expirationService = new PasswordExpirationService();
2026-03-14 13:39:48 +08:00
bool isExpired = expirationService.IsPasswordExpired(DateTime.Parse("3000-2-6"));
bool isExpiringSoon = expirationService.IsPasswordExpiringSoon(DateTime.Parse("3000-2-6"));
//if (isExpired)
//{
// MessageBox.Show("授权已过期,请联系经销商处理");
// Application.Run(new ChangPassword());
//}
2026-01-27 17:27:59 +08:00
if (isExpiringSoon && !isExpired)
{
MessageBox.Show("授权即将过期,请联系经销商处理");
try
{
// 测试密码加密功能
System.Diagnostics.Debug.WriteLine("\n========================================");
System.Diagnostics.Debug.WriteLine("程序启动 - 密码加密测试");
System.Diagnostics.Debug.WriteLine("========================================");
PasswordHelper.TestPasswordEncryption();
// 检查是否有重置参数
bool forceReset = args.Length > 0 && args[0].ToLower() == "--reset-users";
if (forceReset)
{
var result = MessageBox.Show(
"确定要重置默认用户吗?\n\n这将删除并重新创建\n• admin / admin123 (管理员)\n• csi / 123456 (普通用户)\n\n其他用户不受影响。",
"重置默认用户",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
DatabaseInitializer.Initialize();
DatabaseInitializer.ForceResetDefaultUsers();
MessageBox.Show("默认用户已重置!\n\n管理员: admin / admin123\n普通用户: csi / 123456",
"重置成功", 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());
}
}
// 登录失败或取消,直接退出
}
}
if (!isExpired && !isExpiringSoon)
{
try
2026-01-26 18:47:27 +08:00
{
// 测试密码加密功能
System.Diagnostics.Debug.WriteLine("\n========================================");
System.Diagnostics.Debug.WriteLine("程序启动 - 密码加密测试");
System.Diagnostics.Debug.WriteLine("========================================");
PasswordHelper.TestPasswordEncryption();
// 检查是否有重置参数
bool forceReset = args.Length > 0 && args[0].ToLower() == "--reset-users";
if (forceReset)
{
var result = MessageBox.Show(
2026-01-27 09:29:23 +08:00
"确定要重置默认用户吗?\n\n这将删除并重新创建\n• admin / admin123 (管理员)\n• csi / 123456 (普通用户)\n\n其他用户不受影响。",
2026-01-26 18:47:27 +08:00
"重置默认用户",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
DatabaseInitializer.Initialize();
DatabaseInitializer.ForceResetDefaultUsers();
2026-01-27 09:29:23 +08:00
MessageBox.Show("默认用户已重置!\n\n管理员: admin / admin123\n普通用户: csi / 123456",
2026-01-26 18:47:27 +08:00
"重置成功", 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)
{
2026-01-27 13:39:10 +08:00
MessageBox.Show($"数据库初始化失败:{ex.Message}\n\n请检查\n1. MySQL服务是否启动\n2. 数据库连接配置是否正确\n3. 数据库 fullautowaterpressure 是否存在\n\n应用程序将退出。",
2026-01-26 18:47:27 +08:00
"初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2026-01-27 13:39:10 +08:00
// 显示登录窗体
using (var loginForm = new LoginForm())
2026-01-26 18:47:27 +08:00
{
2026-01-27 13:39:10 +08:00
if (loginForm.ShowDialog() == DialogResult.OK)
2026-01-26 18:47:27 +08:00
{
2026-01-27 13:39:10 +08:00
// 登录成功,根据用户权限显示对应窗体
if (LoginData.IsAdmin())
{
// 管理员登录,显示 ScanImport 页面
Application.Run(new ScanImport());
}
else
{
// 普通用户登录,显示 NormalTemperatureMode 页面
Application.Run(new NormalTemperatureMode());
}
2026-01-26 18:47:27 +08:00
}
2026-01-27 13:39:10 +08:00
// 登录失败或取消,直接退出
2026-01-26 18:47:27 +08:00
}
2026-01-27 17:27:59 +08:00
2026-01-26 18:47:27 +08:00
}
2026-01-27 17:27:59 +08:00
}
2026-01-26 18:47:27 +08:00
/// <summary>
/// 检查并修复损坏的密码数据
/// </summary>
private static void CheckAndFixPasswordData()
{
try
{
var userRepo = new UserRepository();
// 检查默认用户
var adminUser = userRepo.GetUserByUsername("admin");
2026-01-27 09:29:23 +08:00
var csiUser = userRepo.GetUserByUsername("csi");
2026-01-26 18:47:27 +08:00
bool needsReset = false;
2026-01-07 13:42:17 +08:00
2026-01-26 18:47:27 +08:00
// 检查 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;
}
2026-01-07 13:42:17 +08:00
2026-01-26 18:47:27 +08:00
// 检查 cgx 用户
2026-01-27 09:29:23 +08:00
if (csiUser != null)
2026-01-26 18:47:27 +08:00
{
2026-01-27 09:29:23 +08:00
string storedPassword = csiUser.PasswordHash ?? "";
2026-01-26 18:47:27 +08:00
2026-01-27 09:29:23 +08:00
System.Diagnostics.Debug.WriteLine($"\n检查 csi 用户:");
2026-01-26 18:47:27 +08:00
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
{
2026-01-27 09:29:23 +08:00
System.Diagnostics.Debug.WriteLine($"\ncsi 用户不存在");
2026-01-26 18:47:27 +08:00
needsReset = true;
}
2026-01-07 13:42:17 +08:00
2026-01-26 18:47:27 +08:00
if (needsReset)
{
System.Diagnostics.Debug.WriteLine("\n检测到密码数据异常自动重置默认用户...");
DatabaseInitializer.ForceResetDefaultUsers();
System.Diagnostics.Debug.WriteLine("密码数据已修复!");
System.Diagnostics.Debug.WriteLine(" admin 密码: admin123");
2026-01-27 09:29:23 +08:00
System.Diagnostics.Debug.WriteLine(" csi 密码: 123456");
2026-01-26 18:47:27 +08:00
}
else
{
System.Diagnostics.Debug.WriteLine("\n密码数据检查通过 ✓");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"密码数据检查失败: {ex.Message}");
System.Diagnostics.Debug.WriteLine($"详细信息: {ex}");
// 不影响程序启动,继续运行
}
2026-01-07 13:42:17 +08:00
}
}
}