Browse Source

登录连接信息

master
sc 6 days ago
parent
commit
5c51e96f67
  1. 11
      AppShell.xaml.cs
  2. 14
      Class/SQLiteConfig.cs
  3. 50
      Models/AppModels.cs
  4. 13
      QueryPage.xaml.cs
  5. 45
      View/LogPage.xaml
  6. 102
      View/LogPage.xaml.cs

11
AppShell.xaml.cs

@ -13,22 +13,21 @@ namespace SunlightAggregationTerminal
} }
private void Shell_Loaded(object sender, EventArgs e) private async void Shell_Loaded(object sender, EventArgs e)
{ {
//打开登录 //打开登录
if (!App.GlobalData.LogNo) if (!App.GlobalData.LogNo)
{ {
var logpage = new View.LogPage(); var logpage = new View.LogPage();
Navigation.PushModalAsync(new NavigationPage(logpage)); await Navigation.PushModalAsync(new NavigationPage(logpage));
} }
else else
{//发送登录请求 {//发送登录请求
/* var dat = TcpServer.Query(""); if (await AppModels.LogIn())
if (dat != null)
{//拒绝登陆时打开登录页面 {//拒绝登陆时打开登录页面
var logpage = new View.LogPage(); var logpage = new View.LogPage();
Navigation.PushModalAsync(logpage); await Navigation.PushModalAsync(logpage);
}*/ }
} }
} }
} }

14
Class/SQLiteConfig.cs

@ -234,10 +234,15 @@ namespace SunlightAggregationTerminal.Class
reader.Close(); // 记得关闭 Reader reader.Close(); // 记得关闭 Reader
// 检查并添加缺失字段 (直接拼接 SQL) // 检查并添加缺失字段 (直接拼接 SQL)
// 检查 Enterprise if (!existingColumns.Contains("START"))
if (!existingColumns.Contains("Enterprise"))
{ {
string sql = "ALTER TABLE Server ADD COLUMN Enterprise TEXT NULL;"; string sql = "ALTER TABLE Server ADD COLUMN START BOOL DEFAULT (0);";
SQLitedata.ExecuteNonQuery(sql, null);
}
// 检查 Server
if (!existingColumns.Contains("Server"))
{
string sql = "ALTER TABLE Server ADD COLUMN Server TEXT NULL;";
SQLitedata.ExecuteNonQuery(sql, null); SQLitedata.ExecuteNonQuery(sql, null);
} }
// 检查 ServerID // 检查 ServerID
@ -300,7 +305,8 @@ namespace SunlightAggregationTerminal.Class
// 建表语句 // 建表语句
string createSql = @" string createSql = @"
CREATE TABLE Server ( CREATE TABLE Server (
Enterprise TEXT, START BOOL DEFAULT (0),
Server TEXT,
ServerID TEXT ServerID TEXT
);"; );";

50
Models/AppModels.cs

@ -66,6 +66,36 @@ namespace SunlightAggregationTerminal.Models
return userdata; return userdata;
} }
public async static Task<bool> LogIn()
{
App.GlobalData.LogNo = false;
//发生登录请求
// 创建一个 5 秒的延时任务
Task delayTask = Task.Delay(15000);
// 创建一个监测任务,不断检查变量
Task monitorTask = Task.Run(async () =>
{
while (!App.GlobalData.LogNo)
{
// 每隔 100 毫秒检查一次,避免 CPU 占用过高
await Task.Delay(100);
}
});
// 使用 Task.WhenAny 等待任意一个任务完成
await Task.WhenAny(delayTask, monitorTask);
if (App.GlobalData.LogNo)
{
return true;
}
else
{
return false;
}
}
public static string GetAppUniqueId() public static string GetAppUniqueId()
{ {
@ -85,5 +115,25 @@ namespace SunlightAggregationTerminal.Models
return id; return id;
} }
public static string? GetSERVER()
{
const string key = "sunlight_server";
// 尝试从安全存储中读取
string? dat = SecureStorage.GetAsync(key).Result;
return dat;
}
public static void GetSERVERWait(string dat)
{
const string key = "sunlight_server";
if (string.IsNullOrEmpty(dat))
{
// 如果不存在,生成一个新的 GUID
dat = Guid.NewGuid().ToString();
// 保存到安全存储
SecureStorage.SetAsync(key, dat).Wait();
}
}
} }
} }

13
QueryPage.xaml.cs

@ -8,14 +8,13 @@ public partial class QueryPage : ContentPage
public static Action<string>? OnScanQuery; public static Action<string>? OnScanQuery;
public QueryPage() public QueryPage()
{ {
InitializeComponent(); InitializeComponent();
WeakReferenceMessenger.Default.Register<string>(this, (r, scannedValue) => WeakReferenceMessenger.Default.Register<string>(this, (r, scannedValue) =>
{ {
ResultEntry.Text = scannedValue; ResultEntry.Text = scannedValue;
}); });
};
} }
private async void OnScanButtonClicked(object sender, EventArgs e) private async void OnScanButtonClicked(object sender, EventArgs e)
{ {

45
View/LogPage.xaml

@ -3,25 +3,29 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SunlightAggregationTerminal.View.LogPage" x:Class="SunlightAggregationTerminal.View.LogPage"
Title="登录"> Title="登录">
<VerticalStackLayout Padding="30" Spacing="20" <VerticalStackLayout Padding="30" Spacing="10"
VerticalOptions="Center" VerticalOptions="Center"
HorizontalOptions="Center" HorizontalOptions="Center"
MaximumWidthRequest="600"> MaximumWidthRequest="550">
<Grid RowDefinitions="Auto, *" ColumnDefinitions="*, Auto"> <Grid>
<HorizontalStackLayout VerticalOptions="Center" Spacing="15"> <VerticalStackLayout Spacing="-20" >
<Grid RowDefinitions="Auto, *" ColumnDefinitions="*, Auto" >
<HorizontalStackLayout VerticalOptions="Center" Spacing="15">
<!-- 信息 --> <!-- 信息 -->
<VerticalStackLayout Grid.Row="0" <VerticalStackLayout Grid.Row="0" Grid.Column="0" VerticalOptions="Center">
Grid.Column="0"
VerticalOptions="Center">
<HorizontalStackLayout VerticalOptions="Center"> <HorizontalStackLayout VerticalOptions="Center">
<Entry Text="服务器:" FontSize="26" TextColor="Black" /> <Entry Text="服务器:" FontSize="26" TextColor="Black" IsReadOnly="True"/>
<Entry x:Name="Enterprise" <Picker x:Name="SERVER"
Text="IP" FontSize="24"
FontSize="26" TextColor="Gray"
TextColor="Gray" WidthRequest="200"
WidthRequest="200" MaximumWidthRequest="200"
MaximumWidthRequest="200"/> SelectedIndexChanged="SERVER_SelectedIndexChanged">
</HorizontalStackLayout> <Picker.ItemDisplayBinding>
<Binding Path="SERVER"/>
</Picker.ItemDisplayBinding>
</Picker>
</HorizontalStackLayout>
</VerticalStackLayout> </VerticalStackLayout>
</HorizontalStackLayout> </HorizontalStackLayout>
<Button Grid.Row="0" Grid.Column="1" <Button Grid.Row="0" Grid.Column="1"
@ -34,9 +38,15 @@
<FileImageSource File="lucidescanline.png" /> <FileImageSource File="lucidescanline.png" />
</Button.ImageSource> </Button.ImageSource>
</Button> </Button>
</Grid>
<Entry x:Name="SERVERIP" FontSize="26" TextColor="Gray"
WidthRequest="350" MaximumWidthRequest="350"/>
</VerticalStackLayout>
</Grid> </Grid>
<HorizontalStackLayout VerticalOptions="Center"> <HorizontalStackLayout VerticalOptions="Center">
<Entry Text="用户:" FontSize="26" TextColor="Black" /> <Entry Text="用户:" FontSize="26" TextColor="Black"
IsReadOnly="True"/>
<!-- 用户名输入 --> <!-- 用户名输入 -->
<Entry x:Name="UsernameEntry" <Entry x:Name="UsernameEntry"
Placeholder="请输入用户名" Placeholder="请输入用户名"
@ -45,7 +55,8 @@
MaximumWidthRequest="250"/> MaximumWidthRequest="250"/>
</HorizontalStackLayout> </HorizontalStackLayout>
<HorizontalStackLayout VerticalOptions="Center"> <HorizontalStackLayout VerticalOptions="Center">
<Entry Text="密码:" FontSize="26" TextColor="Black"/> <Entry Text="密码:" FontSize="26" TextColor="Black"
IsReadOnly="True"/>
<!-- 密码输入 --> <!-- 密码输入 -->
<Entry x:Name="PasswordEntry" <Entry x:Name="PasswordEntry"
Placeholder="请输入密码" Placeholder="请输入密码"

102
View/LogPage.xaml.cs

@ -1,21 +1,64 @@
using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging;
using SunlightAggregationTerminal.Class; using SunlightAggregationTerminal.Class;
using SunlightAggregationTerminal.Models; using SunlightAggregationTerminal.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data; using System.Data;
namespace SunlightAggregationTerminal.View; namespace SunlightAggregationTerminal.View;
public class ItemOption
{
public required string SERVER { get; set; }
public required string SERVERIP { get; set; }
}
public partial class LogPage : ContentPage public partial class LogPage : ContentPage
{ {
public static Action<string>? OnScanProfile; public ObservableCollection<ItemOption> Items { get; }
public LogPage() public LogPage()
{ {
InitializeComponent(); InitializeComponent();
WeakReferenceMessenger.Default.Register<string>(this, (r, scannedValue) => WeakReferenceMessenger.Default.Register<string>(this, (r, scannedValue) =>
{ {
Enterprise.Text = scannedValue; SERVERIP.Text = scannedValue;
}); });
//可用服务器列表
AppModels.sqliteHelper.Open();
var userdata = AppModels.sqliteHelper.ExecuteDataSet("select * from Server", null);
AppModels.sqliteHelper.Close();
if (userdata != null)
{
foreach (DataRow dataRow in userdata.Tables[0].Rows)
{
if (dataRow != null)
{
Items?.Add(new ItemOption {
SERVER = dataRow.Field<string>("UserName") ?? "",
SERVERIP = dataRow.Field<string>("UserName") ?? ""
});
}
}
}
// 可选:设置默认选中项
if (Items != null)
{
SERVER.ItemsSource = Items;
SERVER.SelectedItem = Items[0];
}
}
private void SERVER_SelectedIndexChanged(object sender, EventArgs e)
{
var dat = SERVER.SelectedItem as ItemOption;
if (dat != null)
{
SERVERIP.Text = dat.SERVERIP.ToString();
}
} }
private void OnScanButtonClicked(object sender, EventArgs e) private void OnScanButtonClicked(object sender, EventArgs e)
@ -27,34 +70,43 @@ public partial class LogPage : ContentPage
private async void Log_Clicked(object sender, EventArgs e)//登录 private async void Log_Clicked(object sender, EventArgs e)//登录
{ {
TcpServer.Configuration(Enterprise.Text, UsernameEntry.Text, PasswordEntry.Text, TcpServer.Configuration(SERVERIP.Text, UsernameEntry.Text, PasswordEntry.Text,
Models.AppModels.GetAppUniqueId()); Models.AppModels.GetAppUniqueId());
// while(!App.GlobalData.LogNo); if (await AppModels.LogIn())
/*
var temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;");
if (temp_name != null)
{ {
//存在更新账号状态 var temp_name = AppModels.Select("SELECT * FROM Users WHERE User='" + UsernameEntry.Text + "' LIMIT 1;")!.Tables[0];
//var name = temp_name.Tables[0].AsEnumerable().FirstOrDefault(); if (temp_name != null)
{
//存在更新账号状态
string sql = @"UPDATE Users SET ServerID = '" + SERVERIP.Text
+ "', UserPassword = '" + PasswordEntry.Text +
"', LogNo = true WHERE User='" + UsernameEntry.Text + "';";
AppModels.Updata(sql);
}
else
{
//插入账号
string sql = @"INSERT INTO Users (User,UserId ,UserName ,UserPassword ,
Enterprise ,Department ,ServerID ,Groups ,
ProxyID ,LogNo ,LocalAreaNetworkMode ,DarkMode ,
MessageNotificationMode ,GUID
)VALUES('" + UsernameEntry.Text + "','SUNLIGHT','SUNLIGHT','" + PasswordEntry.Text +
"','SUNLIGHT','ENGINEER','" + SERVERIP.Text + "','ENGINEER'," +
" '',0,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');";
AppModels.INSERT(sql);
}
string sql = @"UPDATE Users SET ServerID = '" + Enterprise.Text var temp_server = AppModels.Select("SELECT * FROM Server WHERE ServerID='" + SERVERIP.Text + "' LIMIT 1;")!.Tables[0];
+ "', UserPassword = '" + PasswordEntry.Text+ if (temp_server == null)
"', LogNo = true WHERE User='" + UsernameEntry.Text + "';"; {
AppModels.Updata(sql); string sql = @"INSERT INTO Server ( Server, ServerID)VALUES('" + App.GlobalData.Enterprise + "','" +
App.GlobalData.ServerID + "');";
AppModels.INSERT(sql);
}
} }
else
{
//插入账号
string sql = @"INSERT INTO Users (User,UserId ,UserName ,UserPassword ,
Enterprise ,Department ,ServerID ,Groups ,
ProxyID ,LogNo ,LocalAreaNetworkMode ,DarkMode ,
MessageNotificationMode ,GUID
)VALUES('"+ UsernameEntry.Text + "','SUNLIGHT','SUNLIGHT','"+ PasswordEntry.Text +
"','SUNLIGHT','ENGINEER','"+ Enterprise.Text + "','ENGINEER',"+
" '',0,0,0,0,'" + Models.AppModels.GetAppUniqueId() + "');";
AppModels.INSERT(sql);
}*/
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
} }
} }
Loading…
Cancel
Save