Skip to content

Commit

Permalink
数据库密码设置界面
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobus committed Apr 10, 2024
1 parent c65271c commit a890c95
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 20 deletions.
89 changes: 70 additions & 19 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using IPAM_NOTE.DatabaseOperation;
using IPAM_NOTE.UserWindows;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
Expand All @@ -21,12 +23,12 @@ protected override void OnStartup(StartupEventArgs e)

base.OnStartup(e);

//订阅全局异常信息
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
//订阅全局异常信息
this.DispatcherUnhandledException += App_DispatcherUnhandledException;


// 检查 SQLite 数据库文件是否存在
string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"db\";
// 检查 SQLite 数据库文件是否存在
string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"db\";
string dbName = "Address_database.db";

if (!Directory.Exists(dbFilePath))
Expand Down Expand Up @@ -55,22 +57,71 @@ protected override void OnStartup(StartupEventArgs e)
}
}

CheckDatabasePassword();
}


/// <summary>
/// 全局异常处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// 处理异常
// 记录异常信息、显示友好的错误提示框等
Console.WriteLine(e.Exception);
e.Handled = true; // 标记为已处理,防止应用程序终止
}

private DbClass dbClass;


/// <summary>
/// 检查数据库是否加密
/// </summary>
private void CheckDatabasePassword()
{
string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"db\";


string dbName = "Address_database.db";


dbFilePath = dbFilePath + dbName;

dbClass = new DbClass(dbFilePath);

try
{
dbClass.OpenConnection();



// 尝试执行一个简单的查询
SQLiteCommand checkCommand = new SQLiteCommand("SELECT 1;", dbClass.connection);
checkCommand.ExecuteNonQuery();

// 如果没有遇到异常,表明数据库没有加密,显示设置密码窗口
SetPasswordWindow setPasswordWindow = new SetPasswordWindow();
setPasswordWindow.ShowDialog();

}
catch (SQLiteException ex)
{
// 如果遇到异常,表明数据库已经加密,显示验证密码窗口
VerifyPasswordWindow verifyPasswordWindow = new VerifyPasswordWindow();
verifyPasswordWindow.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show($"发生错误: {ex.Message}", "错误");
}
}


}


/// <summary>
/// 全局异常处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// 处理异常
// 记录异常信息、显示友好的错误提示框等
Console.WriteLine(e.Exception);
e.Handled = true; // 标记为已处理,防止应用程序终止
}

}
}
}
18 changes: 17 additions & 1 deletion IPAM-NOTE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@
<Compile Include="UserPages\NetworkManage.xaml.cs">
<DependentUpon>NetworkManage.xaml</DependentUpon>
</Compile>
<Compile Include="UserWindows\SetPasswordWindow.xaml.cs">
<DependentUpon>SetPasswordWindow.xaml</DependentUpon>
</Compile>
<Compile Include="UserWindows\VerifyPasswordWindow.xaml.cs">
<DependentUpon>VerifyPasswordWindow.xaml</DependentUpon>
</Compile>
<Compile Include="ViewMode.cs" />
<Page Include="AddDeviceWindow.xaml">
<SubType>Designer</SubType>
Expand Down Expand Up @@ -246,6 +252,14 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UserWindows\SetPasswordWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UserWindows\VerifyPasswordWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
Expand Down Expand Up @@ -295,7 +309,9 @@
<ItemGroup>
<Resource Include="Resources\ALLOCATION.png" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Resource Include="Resources\LOGIN.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\Devices\SWITCH1.png" />
</ItemGroup>
Expand Down
Binary file added Resources/LOGIN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
151 changes: 151 additions & 0 deletions UserWindows/SetPasswordWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<Window x:Class="IPAM_NOTE.UserWindows.SetPasswordWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:IPAM_NOTE.UserWindows"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Background="{DynamicResource MaterialDesignDarkBackground}"
Foreground="{DynamicResource MaterialDesignDarkForeground}"
TextElement.FontWeight="Medium"
TextElement.FontSize="14"
ResizeMode="NoResize"
FontFamily="SimHei"
mc:Ignorable="d"
Title="设置数据库密码"
Style="{DynamicResource MaterialDesignWindow}"
Height="430"
Width="800"
WindowStartupLocation="CenterScreen"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="350"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<TextBlock
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="5"
Panel.ZIndex="100"
VerticalAlignment="Center"
Style="{StaticResource MaterialDesignHeadline4TextBlock}"
Text="IPAM-NOTE IP地址分配记录系统" />

<Image
Grid.Column="0"
Grid.Row="0"
Grid.ColumnSpan="2"
Grid.RowSpan="2"
Opacity="0.3"
Source="/Resources/LOGIN.png"
Stretch="Uniform"/>
<StackPanel
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
>
<TextBlock
Margin="52,0,0,8"
Style="{StaticResource MaterialDesignHeadline6TextBlock}"
Text="数据库密码设置" />

<StackPanel
x:Name="Tag1Panel"
Margin="5"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
materialDesign:HintAssist.Hint="密码"
>

<materialDesign:PackIcon
Margin="5"
Kind="PasswordOutline"
VerticalAlignment="Center"
Height="32"
Width="32"


/>
<StackPanel
Margin="5"
Orientation="Vertical">
<PasswordBox
Width="250"
materialDesign:HintAssist.Hint="设置数据库密码"

Style="{StaticResource MaterialDesignFilledPasswordBox}" />
</StackPanel>
</StackPanel>
<StackPanel

Margin="5"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
materialDesign:HintAssist.Hint="再次输入密码"
>

<materialDesign:PackIcon
Margin="5"
Kind="PasswordOutline"
VerticalAlignment="Center"
Height="32"
Width="32"


/>

<StackPanel
Margin="5"
Orientation="Vertical">
<PasswordBox
Width="250"
materialDesign:HintAssist.Hint="请再次输入密码"

Style="{StaticResource MaterialDesignFilledPasswordBox}" />
</StackPanel>

</StackPanel>

<StackPanel

Margin="5"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
materialDesign:HintAssist.Hint="端口号"
>

<materialDesign:PackIcon
Margin="5"
Kind="ContentSaveOutline"
VerticalAlignment="Center"
Height="32"
Width="32"


/>
<Button
HorizontalAlignment="Right"
Margin="5"
Width="250"
Height="45"
Content="保存"
Style="{StaticResource MaterialDesignFlatDarkBgButton}"
materialDesign:ButtonAssist.CornerRadius="5"
ToolTip="保存密码" />


</StackPanel>
</StackPanel>

</Grid>
</Window>
27 changes: 27 additions & 0 deletions UserWindows/SetPasswordWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IPAM_NOTE.UserWindows
{
/// <summary>
/// SetPasswordWindow.xaml 的交互逻辑
/// </summary>
public partial class SetPasswordWindow : Window
{
public SetPasswordWindow()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit a890c95

Please sign in to comment.