Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
Ver 2.1.0-RC4: Now can use "Enter" to login.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiroiame-Kusu committed Jul 22, 2024
1 parent 1d9466c commit a465eb8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 72 deletions.
3 changes: 2 additions & 1 deletion LoCyanFrpDesktop/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
ExtendsContentIntoTitleBar="True"
StateChanged="UiWindow_StateChanged"
Style="{StaticResource UiWindow}"
WindowStyle="None">
WindowStyle="None"
KeyDown="UiWindow_KeyDown">
<Grid>
<Grid.Resources>
<Color x:Key="ShadowColor"></Color>
Expand Down
158 changes: 87 additions & 71 deletions LoCyanFrpDesktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ public MainWindow()
//initialBanner.Show();
//initialBanner.Hide();
InitializeComponent();
if(Random.Shared.Next(0, 100) == Random.Shared.Next(0, 100))
{
if (Random.Shared.Next(0, 100) == Random.Shared.Next(0, 100))
{
throw new Exception("这是一个彩蛋,万分之一的机会");
}

Uri iconUri = new Uri("pack://application:,,,/LoCyanFrpDesktop;component/Resource/favicon.ico", UriKind.RelativeOrAbsolute);
this.Icon = new BitmapImage(iconUri);
if(Global.LoginedByConsole && Properties.Settings.Default.username != null && Properties.Settings.Default.password != null)
if (Global.LoginedByConsole && Properties.Settings.Default.username != null && Properties.Settings.Default.password != null)
{
Login(Properties.Settings.Default.username, Properties.Settings.Default.password);

Check warning on line 69 in LoCyanFrpDesktop/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 69 in LoCyanFrpDesktop/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / BuildForWindows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}
Tips.Text = Global.Tips[Random.Shared.Next(0, Global.Tips.Count - 1)];
CheckNetworkAvailability();

DataContext = this;
Access.MainWindow = this;
Update.Init();
Expand All @@ -90,32 +90,35 @@ private async void CheckNetworkAvailability()
//MessageBox.Show("请检查您的网络连接!");
Environment.Exit(0);
};
using (HttpClient httpClient = new HttpClient()) {
using (HttpClient httpClient = new HttpClient())
{
try
{
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync("https://api.locyanfrp.cn");
var c = httpResponseMessage.StatusCode;
if (!httpResponseMessage.IsSuccessStatusCode)
{
a();

}
else {
else
{
InitializeAutoLogin();
}
}
catch (Exception ignored) {
catch (Exception ignored)
{
a();
}

}
}

public void OpenSnackbar(string title, string message, SymbolRegular icon)
{
Dispatcher.Invoke(() =>
{

Snackbar.Show(title, message, icon);
});
}
Expand Down Expand Up @@ -168,7 +171,7 @@ private async Task<bool> CheckLogined()
string jsonString = await response.Content.ReadAsStringAsync();
var InfoResponseObject = JsonConvert.DeserializeObject<InfoResponseObject>(jsonString);
UserInfo = InfoResponseObject;

if (InfoResponseObject.Status == 0)
{
return true;
Expand All @@ -187,66 +190,71 @@ private async void Login_Click(object sender, RoutedEventArgs e)
SecureString secure_password = Password.SecurePassword;
string password = ConvertToUnsecureString(secure_password);
await Login(username, password);

}
public async Task Login(string username,string password)
public async Task Login(string username, string password)
{
// 使用密码,例如验证或其他操作
if (string.IsNullOrEmpty(password))
if (!string.IsNullOrEmpty(password))
{
Logger.MsgBox("解析密码的过程中发生错误, 请联系开发者!", "错误", 0, 48, 0);
return;
}
//Logger.MsgBox("你确定你输了密码?", "错误", 0, 48, 0);
//return;


if (username != "" && password != "")
{
using (var httpClient = new HttpClient())

if (username != "" && password != "")
{
string url = $"https://api.locyanfrp.cn/User/DoLogin?username={username}&password={password}";
try
using (var httpClient = new HttpClient())
{
// 发起 GET 请求并获取响应
HttpResponseMessage response = await httpClient.GetAsync(url);

// 确保请求成功
response.EnsureSuccessStatusCode();

// 将 JSON 数据读取为字符串
string jsonString = await response.Content.ReadAsStringAsync();

// 将 JSON 字符串反序列化为对象
var responseObject = JsonConvert.DeserializeObject<ResponseObject>(jsonString);

if (responseObject.Status != 0)
string url = $"https://api.locyanfrp.cn/User/DoLogin?username={username}&password={password}";
try
{
Logger.MsgBox("账号或密码错误!", "警告", 0, 48, 0);
// 发起 GET 请求并获取响应
HttpResponseMessage response = await httpClient.GetAsync(url);

// 确保请求成功
response.EnsureSuccessStatusCode();

// 将 JSON 数据读取为字符串
string jsonString = await response.Content.ReadAsStringAsync();

// 将 JSON 字符串反序列化为对象
var responseObject = JsonConvert.DeserializeObject<ResponseObject>(jsonString);

if (responseObject.Status != 0)
{
Logger.MsgBox("账号或密码错误!", "警告", 0, 48, 0);
}
else
{
Logger.MsgBox($"登录成功\n获取到登录Token: {responseObject.Token}", "提示", 0, 47, 0);
Avatar = responseObject.UserData.Avatar;
Inbound = responseObject.UserData.Inbound;
Outbound = responseObject.UserData.Outbound;
Traffic = responseObject.UserData.Traffic;
Properties.Settings.Default.LoginToken = responseObject.Token;
Properties.Settings.Default.username = responseObject.UserData.Username;
Properties.Settings.Default.FrpToken = responseObject.UserData.FrpToken;
string path = ".//session.token";
string text = $"{responseObject.UserData.Username}|{responseObject.Token}";
File.WriteAllText(path, text);
islogin = true;
DashBoard = new DashBoard();
DashBoard.Show();
Close();
Access.DashBoard.CheckIfFrpcInstalled();

}
}
else
catch (HttpRequestException ex)
{
Logger.MsgBox($"登录成功\n获取到登录Token: {responseObject.Token}", "提示", 0, 47, 0);
Avatar = responseObject.UserData.Avatar;
Inbound = responseObject.UserData.Inbound;
Outbound = responseObject.UserData.Outbound;
Traffic = responseObject.UserData.Traffic;
Properties.Settings.Default.LoginToken = responseObject.Token;
Properties.Settings.Default.username = responseObject.UserData.Username;
Properties.Settings.Default.FrpToken = responseObject.UserData.FrpToken;
string path = ".//session.token";
string text = $"{responseObject.UserData.Username}|{responseObject.Token}";
File.WriteAllText(path, text);
islogin = true;
DashBoard = new DashBoard();
DashBoard.Show();
Close();
Access.DashBoard.CheckIfFrpcInstalled();

Logger.MsgBox($"请求API的过程中出错 \n 报错信息: {ex.Message}", "错误", 0, 48, 0);
}
}
catch (HttpRequestException ex)
{
Logger.MsgBox($"请求API的过程中出错 \n 报错信息: {ex.Message}", "错误", 0, 48, 0);
}
}
else
{
Logger.MsgBox("用户名 / 密码不能为空!", "警告", 0, 48, 0);
}
}
else
Expand Down Expand Up @@ -293,17 +301,17 @@ private void ForgetPassword_Navigate(object sender, RequestNavigateEventArgs e)
});
e.Handled = true;
}


public bool IsDarkThemeEnabled()
{
const string RegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
const string RegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";

// 从注册表中获取“AppsUseLightTheme”值
int value = (int)Registry.GetValue(RegistryKey, "AppsUseLightTheme", 1);
// 从注册表中获取“AppsUseLightTheme”值
int value = (int)Registry.GetValue(RegistryKey, "AppsUseLightTheme", 1);

// 如果值为0,则深色主题已启用
return value == 0;
// 如果值为0,则深色主题已启用
return value == 0;
}
public void UiWindow_Loaded(object sender, RoutedEventArgs e)
{ /*
Expand All @@ -316,7 +324,7 @@ public void UiWindow_Loaded(object sender, RoutedEventArgs e)
DarkThemeEnabled = IsDarkThemeEnabled();
//DarkThemeEnabled = false;
Theme.Apply(DarkThemeEnabled ? ThemeType.Dark : ThemeType.Light, WindowBackdropType = BackgroundType.Mica);

//MainForm.Background = new SolidColorBrush(DarkThemeEnabled ? Colors.LightGray : Colors.WhiteSmoke);
Color newColor = DarkThemeEnabled ? Colors.White : Colors.LightGray;
Resources["ShadowColor"] = newColor;
Expand All @@ -326,16 +334,17 @@ public void UiWindow_Loaded(object sender, RoutedEventArgs e)

public void UiWindow_Closing(object sender, CancelEventArgs e)
{
if (islogin) {
if (islogin)
{
e.Cancel = true;
ShowInTaskbar = true;
Hide();
}
else
{
Exit_Click(sender,null);
Exit_Click(sender, null);
}

}
public void UiWindow_StateChanged(object sender, EventArgs e)
{/*
Expand All @@ -357,7 +366,7 @@ public void Hide_Click(object sender, RoutedEventArgs e)

public void Exit_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
Environment.Exit(0);
}

private void NotifyIcon_LeftClick(Wpf.Ui.Controls.NotifyIcon sender, RoutedEventArgs e)
Expand All @@ -371,8 +380,15 @@ private void NotifyIcon_LeftClick(Wpf.Ui.Controls.NotifyIcon sender, RoutedEvent
Show();
}
}
}

private void UiWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Login_Click(sender, e);
}
}
}
public class InfoResponseObject
{
[JsonProperty("status")]
Expand Down
Binary file modified LoCyanFrpDesktop/resource/net6.0/Updater.dll
Binary file not shown.
Binary file modified LoCyanFrpDesktop/resource/net6.0/Updater.exe
Binary file not shown.

0 comments on commit a465eb8

Please sign in to comment.