Files
VacuumPressureMembranePoreS…/Views/LoginWindow.xaml.cs
2026-04-12 16:46:55 +08:00

168 lines
6.7 KiB
C#

using System;
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using MembranePoreTester.Models;
namespace MembranePoreTester.Views
{
public partial class LoginWindow : Window
{
private const string PasswordFile = "passwords.json";
public LoginWindow()
{
InitializeComponent();
EnsurePasswordFile();
}
private void EnsurePasswordFile()
{
if (!File.Exists(PasswordFile))
{
var defaultPasswords = new
{
AdminPassword = "admin123",
OperatorPassword = "oper123"
};
string json = JsonSerializer.Serialize(defaultPasswords, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(PasswordFile, json);
}
}
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
string role = ((ComboBoxItem)cmbRole.SelectedItem).Tag.ToString();
string password = txtPassword.Password;
string expectedPassword = GetPassword(role);
if (password == expectedPassword)
{
App.CurrentUserRole = role == "Admin" ? UserRole.Admin : UserRole.Operator;
DialogResult = true;
Close();
}
else
{
MessageBox.Show("密码错误", "登录失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnChangePwd_Click(object sender, RoutedEventArgs e)
{
string role = ((ComboBoxItem)cmbRole.SelectedItem).Tag.ToString();
string title = role == "Admin" ? "修改管理员密码" : "修改操作员密码";
var dialog = new Window
{
Title = title,
Width = 350,
Height = 250,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = this,
ResizeMode = ResizeMode.NoResize,
Content = new StackPanel
{
Margin = new Thickness(20),
Children =
{
new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0,10,0,10),
Children = { new Label { Width = 80, Content = "新密码:" }, new PasswordBox { Name = "newPwd", Width = 180 } } },
new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0,10,0,10),
Children = { new Label { Width = 80, Content = "确认密码:" }, new PasswordBox { Name = "confirmPwd", Width = 180 } } },
new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0,20,0,0),
Children = { new Button { Content = "确定", Width = 80, Margin = new Thickness {Left= 5} }, new Button { Content = "取消", Width = 80, Margin = new Thickness {Left= 5} } } }
}
}
};
var confirmBtn = ((StackPanel)((StackPanel)dialog.Content).Children[2]).Children[0] as Button;
var cancelBtn = ((StackPanel)((StackPanel)dialog.Content).Children[2]).Children[1] as Button;
var newPwdBox = ((StackPanel)((StackPanel)dialog.Content).Children[0]).Children[1] as PasswordBox;
var confirmPwdBox = ((StackPanel)((StackPanel)dialog.Content).Children[1]).Children[1] as PasswordBox;
confirmBtn.Click += (s, ev) =>
{
string newPwd = newPwdBox.Password;
string confirmPwd = confirmPwdBox.Password;
if (string.IsNullOrEmpty(newPwd))
{
MessageBox.Show("密码不能为空", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (newPwd != confirmPwd)
{
MessageBox.Show("两次输入的密码不一致", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (UpdatePassword(role, newPwd))
{
MessageBox.Show("密码修改成功,下次登录生效", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
dialog.Close();
}
else
{
MessageBox.Show("密码修改失败,请检查文件权限", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
};
cancelBtn.Click += (s, ev) => dialog.Close();
dialog.ShowDialog();
}
private void BtnExit_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private string GetPassword(string role)
{
if (!File.Exists(PasswordFile)) return role == "Admin" ? "admin123" : "oper123";
try
{
string json = File.ReadAllText(PasswordFile);
using JsonDocument doc = JsonDocument.Parse(json);
string key = role == "Admin" ? "AdminPassword" : "OperatorPassword";
if (doc.RootElement.TryGetProperty(key, out JsonElement value))
return value.GetString();
return role == "Admin" ? "admin123" : "oper123";
}
catch
{
return role == "Admin" ? "admin123" : "oper123";
}
}
private bool UpdatePassword(string role, string newPassword)
{
try
{
Dictionary<string, string> passwords;
if (File.Exists(PasswordFile))
{
string json = File.ReadAllText(PasswordFile);
passwords = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
}
else
{
passwords = new Dictionary<string, string>();
}
string key = role == "Admin" ? "AdminPassword" : "OperatorPassword";
passwords[key] = newPassword;
string newJson = JsonSerializer.Serialize(passwords, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(PasswordFile, newJson);
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"修改密码失败: {ex.Message}");
return false;
}
}
}
}