Browse Source

登录功能,自动登录实现

master
Administrator 3 months ago
parent
commit
bb8dfca7a1
  1. 2
      Class/SQLiteConfig.cs
  2. 4
      Class/TcpServer.cs
  3. 44
      Models/AppModels.cs
  4. 4
      ProfilePage.xaml.cs
  5. 48
      View/LogPage.xaml.cs

2
Class/SQLiteConfig.cs

@ -280,7 +280,7 @@ namespace SunlightAggregationTerminal.Class
string Sql = @" string Sql = @"
INSERT INTO Users (User,UserId ,UserName ,UserPassword ,Enterprise ,Department ,ServerID ,Groups ,ProxyID ,LogNo ,LocalAreaNetworkMode ,DarkMode ,MessageNotificationMode ,GUID INSERT INTO Users (User,UserId ,UserName ,UserPassword ,Enterprise ,Department ,ServerID ,Groups ,ProxyID ,LogNo ,LocalAreaNetworkMode ,DarkMode ,MessageNotificationMode ,GUID
)VALUES('SUNLIGHT','SUNLIGHT','SUNLIGHT','SUNLIGHT','SUNLIGHT','ENGINEER','TEST','ENGINEER','',0,0,0,0,'"+ Models.AppModels.GetAppUniqueId() + "');"; )VALUES('SUNLIGHT','SUNLIGHT','SUNLIGHT','SUNLIGHT','SUNLIGHT','ENGINEER','127.0.0.1:7789','ENGINEER','',0,0,0,0,'"+ Models.AppModels.GetAppUniqueId() + "');";
// 复用现有的 ExecuteNonQuery 方法 // 复用现有的 ExecuteNonQuery 方法
SQLitedata.ExecuteNonQuery(Sql, null); SQLitedata.ExecuteNonQuery(Sql, null);

4
Class/TcpServer.cs

@ -47,9 +47,7 @@ namespace SunlightAggregationTerminal.Class
ServerPassword = Password; ServerPassword = Password;
ServerTerminal = Terminal; ServerTerminal = Terminal;
_ = TcpClient(tcpClient, ServerIP); _ = TcpClient(tcpClient, ServerIP);
} }
public static async void TcpTransmit(string Dat) public static async void TcpTransmit(string Dat)
{ {
@ -167,7 +165,7 @@ namespace SunlightAggregationTerminal.Class
} }
catch (Exception ex) catch (Exception ex)
{ {
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "不合法的访问地址", "是"); await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.ToString(), "是");
} }
} }
} }

44
Models/AppModels.cs

@ -17,29 +17,31 @@ namespace SunlightAggregationTerminal.Models
if(sqliteHelper != null) if(sqliteHelper != null)
{//获取配置信息 {//获取配置信息
sqliteHelper.Open(); sqliteHelper.Open();
DataSet? userdata = sqliteHelper.ExecuteDataSet("select * from Users ", null); DataSet? userdata = sqliteHelper.ExecuteDataSet("select * from Users where LogNo = 1", null);
sqliteHelper.Close(); sqliteHelper.Close();
if (userdata != null) if (userdata != null)
{ {
DataRow dat = userdata.Tables[0].Rows[0]; if (userdata.Tables[0].Rows.Count > 0)
if (dat != null) {
{ DataRow dat = userdata.Tables[0].Rows[0];
App.GlobalData.UserName = dat.Field<string>("UserName") ?? ""; if (dat != null)
App.GlobalData.UserId = dat.Field<string>("UserID") ?? ""; {
App.GlobalData.User = dat.Field<string>("User") ?? ""; App.GlobalData.UserName = dat.Field<string>("UserName") ?? "";
App.GlobalData.UserPassword = dat.Field<string>("UserPassword") ?? ""; App.GlobalData.UserId = dat.Field<string>("UserID") ?? "";
App.GlobalData.Enterprise = dat.Field<string>("Enterprise") ?? ""; App.GlobalData.User = dat.Field<string>("User") ?? "";
App.GlobalData.Department = dat.Field<string>("Department") ?? ""; App.GlobalData.UserPassword = dat.Field<string>("UserPassword") ?? "";
App.GlobalData.Groups = dat.Field<string>("Groups") ?? ""; App.GlobalData.Enterprise = dat.Field<string>("Enterprise") ?? "";
App.GlobalData.ServerID = dat.Field<string>("ServerID") ?? ""; App.GlobalData.Department = dat.Field<string>("Department") ?? "";
App.GlobalData.ProxyID = dat.Field<string>("ProxyID") ?? ""; App.GlobalData.Groups = dat.Field<string>("Groups") ?? "";
App.GlobalData.LogNo = Convert.ToBoolean(dat.Field<object>("LogNo")); App.GlobalData.ServerID = dat.Field<string>("ServerID") ?? "";
App.GlobalData.LocalAreaNetworkMode = Convert.ToBoolean(dat.Field<object>("LocalAreaNetworkMode")); App.GlobalData.ProxyID = dat.Field<string>("ProxyID") ?? "";
App.GlobalData.DarkMode = Convert.ToBoolean(dat.Field<object>("DarkMode")); App.GlobalData.LogNo = Convert.ToBoolean(dat.Field<object>("LogNo"));
App.GlobalData.MessageNotificationMode = Convert.ToBoolean(dat.Field<object>("MessageNotificationMode")); App.GlobalData.LocalAreaNetworkMode = Convert.ToBoolean(dat.Field<object>("LocalAreaNetworkMode"));
App.GlobalData.DarkMode = Convert.ToBoolean(dat.Field<object>("DarkMode"));
TcpServer.Configuration(App.GlobalData.ServerID, App.GlobalData.User, App.GlobalData.UserPassword, dat.Field<string>("GUID") ?? "Not"); App.GlobalData.MessageNotificationMode = Convert.ToBoolean(dat.Field<object>("MessageNotificationMode"));
TcpServer.Configuration(App.GlobalData.ServerID, App.GlobalData.User, App.GlobalData.UserPassword, dat.Field<string>("GUID") ?? "Not");
}
//启动tcp //启动tcp
// TcpServer.CreateCustomService(); // TcpServer.CreateCustomService();
} }

4
ProfilePage.xaml.cs

@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using SunlightAggregationTerminal.Class;
using SunlightAggregationTerminal.Models; using SunlightAggregationTerminal.Models;
using SunlightAggregationTerminal.View; using SunlightAggregationTerminal.View;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@ -15,10 +16,13 @@ public partial class ProfilePage : ContentPage
private async void Out_Clicked(object sender, EventArgs e) private async void Out_Clicked(object sender, EventArgs e)
{ {
//退出
bool shouldExit = await DisplayAlertAsync("退出确认", "确定要退出吗?", "是", "否"); bool shouldExit = await DisplayAlertAsync("退出确认", "确定要退出吗?", "是", "否");
if (shouldExit) if (shouldExit)
{ {
App.GlobalData.LogNo = false; App.GlobalData.LogNo = false;
await TcpServer.tcpClient.CloseAsync("");
var logpage = new View.LogPage(); var logpage = new View.LogPage();
await Navigation.PushModalAsync(logpage); await Navigation.PushModalAsync(logpage);
} }

48
View/LogPage.xaml.cs

@ -9,13 +9,13 @@ namespace SunlightAggregationTerminal.View;
public class ItemOption public class ItemOption
{ {
public required string? SERVER { get; set; } public required string SERVER { get; set; }
public required string? SERVERIP { get; set; } public required string SERVERIP { get; set; }
} }
public partial class LogPage : ContentPage public partial class LogPage : ContentPage
{ {
public ObservableCollection<ItemOption>? Items { get; } public ObservableCollection<ItemOption>? Items { get; set; }
public LogPage() public LogPage()
{ {
@ -31,22 +31,25 @@ public partial class LogPage : ContentPage
var userdata = AppModels.sqliteHelper.ExecuteDataSet("select * from Server", null); var userdata = AppModels.sqliteHelper.ExecuteDataSet("select * from Server", null);
AppModels.sqliteHelper.Close(); AppModels.sqliteHelper.Close();
Items = new ObservableCollection<ItemOption>();
if (userdata != null) if (userdata != null)
{ {
foreach (DataRow dataRow in userdata.Tables[0].Rows) foreach (DataRow dataRow in userdata.Tables[0].Rows)
{ {
if (dataRow != null) if (dataRow != null)
{ {
Items?.Add(new ItemOption { var dat = new ItemOption
SERVER = dataRow.Field<string>("UserName") ?? "", {
SERVERIP = dataRow.Field<string>("UserName") ?? "" SERVER = dataRow.Field<string>("Server") ?? "",
}); SERVERIP = dataRow.Field<string>("ServerID") ?? ""
};
Items.Add(dat);
} }
} }
} }
// 设置默认选中项
// 可选:设置默认选中项 if (Items.Count>0)
if (Items != null)
{ {
SERVER.ItemsSource = Items; SERVER.ItemsSource = Items;
SERVER.SelectedItem = Items[0]; SERVER.SelectedItem = Items[0];
@ -74,19 +77,17 @@ public partial class LogPage : ContentPage
TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text, TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text,
Models.AppModels.GetAppUniqueId()); Models.AppModels.GetAppUniqueId());
LoadingIndicator.IsVisible = true; LoadingIndicator.IsVisible = true;
//TcpServer.TcpTransmit("Log");
if (await AppModels.LogIn()) if (await AppModels.LogIn())
{ {
var temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;")!.Tables[0]; App.GlobalData.ServerID = SERVERIP.Text;
if (temp_name != null)
int temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;")!.Tables[0].Rows.Count;
if (temp_name >0)
{ {
//存在更新账号状态 //存在更新账号状态
string sql = @"UPDATE Users SET ServerID = '" + SERVERIP.Text string sql = "UPDATE Users SET ServerID = '" + SERVERIP.Text
+ "', UserPassword = '" + PasswordEntry.Text + + "', UserPassword = '" + PasswordEntry.Text +
"', LogNo = 1 WHERE User='" + UsernameEntry.Text + "';"; "', LogNo = 1 WHERE User='" + UsernameEntry.Text + "';";
AppModels.Updata(sql); AppModels.Updata(sql);
@ -104,8 +105,8 @@ public partial class LogPage : ContentPage
AppModels.INSERT(sql); AppModels.INSERT(sql);
} }
var temp_server = AppModels.Select("SELECT * FROM Server WHERE ServerID='" + SERVERIP.Text + "' LIMIT 1;")!.Tables[0]; int temp_server = AppModels.Select("SELECT * FROM Server WHERE ServerID='" + SERVERIP.Text + "' LIMIT 1;")!.Tables[0].Rows.Count;
if (temp_server == null) if (temp_server == 0)
{ {
string sql = @"INSERT INTO Server ( Server, ServerID)VALUES('" + App.GlobalData.Enterprise + "','" + string sql = @"INSERT INTO Server ( Server, ServerID)VALUES('" + App.GlobalData.Enterprise + "','" +
App.GlobalData.ServerID + "');"; App.GlobalData.ServerID + "');";
@ -119,12 +120,5 @@ public partial class LogPage : ContentPage
await DisplayAlertAsync("未登录", "服务器不存在或拒绝访问", "是"); await DisplayAlertAsync("未登录", "服务器不存在或拒绝访问", "是");
} }
LoadingIndicator.IsVisible = false; LoadingIndicator.IsVisible = false;
//await Navigation.PopModalAsync();
} }
} }
Loading…
Cancel
Save