Skip to content

Commit ecddc7f

Browse files
authored
#6 持久化WebUi登录信息
持久化Web UI登录信息
2 parents 6ad62d2 + 34e8f2a commit ecddc7f

11 files changed

Lines changed: 242 additions & 109 deletions

File tree

NapCatScript.Core/Services/SQLiteService.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ private SQLiteService()
1818
Connection = new SQLiteAsyncConnection(DataBasePath);
1919
}
2020

21+
/// <summary>
22+
/// Path是基于运行目录的相对路径
23+
/// </summary>
24+
/// <param name="dataBasePath"></param>
2125
public SQLiteService(string dataBasePath)
2226
{
23-
Connection = new SQLiteAsyncConnection(dataBasePath);
27+
string path = Path.Combine(Environment.CurrentDirectory, dataBasePath);
28+
string directorPath = Path.GetDirectoryName(path);
29+
if(!Directory.Exists(directorPath))
30+
Directory.CreateDirectory(directorPath);
31+
32+
Connection = new SQLiteAsyncConnection(path);
2433
}
2534

2635
/// <summary>

NapCatScript.Desktop/ViewLocator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NapCatScript.Desktop.ViewModels;
77
using NapCatScript.Desktop.ViewModels.NetWorkModels;
88
using NapCatScript.Desktop.Views;
9+
using NapCatScript.Desktop.Views.LoginView;
910
using NapCatScript.Desktop.Views.NetWorkViews;
1011
using NapCatScript.Desktop.Views.NetWorkViews.MiniViews;
1112
using BindingFlags = System.Reflection.BindingFlags;
@@ -18,6 +19,7 @@ public class ViewLocator : IDataTemplate
1819
{
1920
static ViewLocator()
2021
{
22+
ViewModelMap.Add(typeof(WebUiLoginInfo), typeof(WebUiLoginView));
2123
ViewModelMap.Add(HttpSseServerViewModel.Type, typeof(HttpSseServerView));
2224
ViewModelMap.Add(HttpServerViewModel.Type, typeof(HttpServerView));
2325
ViewModelMap.Add(HttpClientViewModel.Type, typeof(HttpClientView));

NapCatScript.Desktop/ViewModels/Interactions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,17 @@ public static class Interactions
88
/// <summary>
99
/// WebUI连接的交互
1010
/// </summary>
11-
public static Interaction<(string, string), Unit> WebUIConnectionEvent {get;} = new();
11+
public static Interaction<(string, string), bool> WebUIConnectionInteraction { get; } = new();
12+
13+
/// <summary>
14+
/// 删除LoginInfo
15+
/// </summary>
16+
public static Interaction<WebUiLoginInfo, Unit> DeleteLoginInfoInteraction { get; } = new();
17+
18+
/// <summary>
19+
/// 登录WebUi
20+
/// <para> <see cref="WebUIConnectionViewModel"/> 订阅 </para>
21+
/// <para> <see cref="WebUiLoginInfo"/> 提供处理程序 </para>
22+
/// </summary>
23+
public static Interaction<WebUiLoginInfo, Unit> LoginWebUiInteraction { get; } = new();
1224
}

NapCatScript.Desktop/ViewModels/MainWindowViewModel.cs

Lines changed: 69 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,82 @@
77
using NapCatScript.Core.JsonFormat;
88
using static NapCatScript.Core.MsgHandle.Utils;
99

10-
namespace NapCatScript.Desktop.ViewModels
10+
namespace NapCatScript.Desktop.ViewModels;
11+
12+
public class MainWindowViewModel : ViewModelBase
1113
{
12-
public class MainWindowViewModel : ViewModelBase
14+
private bool _webUiInit = false;
15+
private ViewModelBase currView;
16+
private string _selectedItem;
17+
18+
public IReactiveCommand ListBoxPropertyChangedCommand { get; private set; }
19+
private ViewModelBase? _logViewModel;
20+
private ViewModelBase? _netWorkModel;
21+
private ViewModelBase? _webUIConnectionViewModel;
22+
public ViewModelBase LogViewModel => _logViewModel ??= new LogViewModel();
23+
public ViewModelBase NetWorkModel => _netWorkModel ??= new NetWorkViewModel();
24+
public ViewModelBase WebUIConnectionViewModel => _webUIConnectionViewModel ??= new WebUIConnectionViewModel();
25+
26+
public ObservableCollection<string> Items { get; } = [];
27+
28+
public string SelectedItem
1329
{
14-
private bool _webUiInit = false;
15-
private ViewModelBase currView;
16-
private string _selectedItem;
17-
18-
public IReactiveCommand ListBoxPropertyChangedCommand {get; private set; }
19-
private ViewModelBase? _logViewModel;
20-
public ViewModelBase LogViewModel
21-
{
22-
get {
23-
if (_logViewModel is null) {
24-
_logViewModel = new LogViewModel();
25-
}
26-
return _logViewModel;
27-
}
28-
}
29-
30-
private ViewModelBase? _netWorkModel;
31-
public ViewModelBase NetWorkModel
32-
{
33-
get {
34-
if (_netWorkModel is null) {
35-
_netWorkModel = new NetWorkViewModel();
36-
}
37-
return _netWorkModel;
38-
}
39-
}
30+
get => _selectedItem;
31+
set => this.RaiseAndSetIfChanged(ref _selectedItem, value);
32+
}
4033

41-
private ViewModelBase? _webUIConnectionViewModel;
42-
public ViewModelBase WebUIConnectionViewModel
43-
{
44-
get {
45-
if (_webUIConnectionViewModel is null) {
46-
_webUIConnectionViewModel = new WebUIConnectionViewModel();
47-
}
48-
return _webUIConnectionViewModel;
49-
}
50-
}
51-
public ObservableCollection<string> Items { get; } = [];
52-
public string SelectedItem { get => _selectedItem; set => this.RaiseAndSetIfChanged(ref _selectedItem, value); }
53-
public ViewModelBase CurrView { get => currView; set => this.RaiseAndSetIfChanged(ref currView, value); }
54-
public MainWindowViewModel()
55-
{
56-
this.WhenAnyValue(f => f.SelectedItem)
57-
.WhereNotNull()
58-
.Subscribe(ListBoxPropertyChanged);
34+
public ViewModelBase CurrView
35+
{
36+
get => currView;
37+
set => this.RaiseAndSetIfChanged(ref currView, value);
38+
}
5939

60-
Interactions.WebUIConnectionEvent.RegisterHandler(WebUILoginEvent);
61-
Items.Add("连接");
62-
Items.Add("聊天");
63-
Items.Add("日志");
64-
Items.Add("网络");
65-
CurrView = WebUIConnectionViewModel;
66-
}
40+
public MainWindowViewModel()
41+
{
42+
this.WhenAnyValue(f => f.SelectedItem)
43+
.WhereNotNull()
44+
.Subscribe(ListBoxPropertyChanged);
6745

68-
private void ListBoxPropertyChanged(string str)
69-
{/*
70-
if (CurrView is LogViewModel log){
71-
log.Dispose();
72-
}
73-
CurrView = null;
74-
GC.Collect();*/
75-
76-
if(_webUiInit == false)
77-
return;
78-
if(str == "网络") CurrView = NetWorkModel;
79-
if(str == "日志") CurrView = LogViewModel;
80-
if(str == "连接") CurrView = WebUIConnectionViewModel;
81-
}
46+
Interactions.WebUIConnectionInteraction.RegisterHandler(WebUILoginEvent);
47+
Items.Add("连接");
48+
Items.Add("聊天");
49+
Items.Add("日志");
50+
Items.Add("网络");
51+
CurrView = WebUIConnectionViewModel;
52+
}
8253

83-
private async Task WebUILoginEvent(IInteractionContext<(string,string),Unit> interaction)
84-
{
85-
(string webURL, string token) connectionValue = interaction.Input;
86-
interaction.SetOutput(Unit.Default);
87-
if(connectionValue.token == ConfigValue.WebToken && connectionValue.webURL == ConfigValue.WebUri)
88-
return;
89-
90-
var rms = await WebUILogin(connectionValue.webURL, connectionValue.token);
91-
var requJson = await rms.Content.ReadAsStringAsync();
92-
if(!requJson.GetJsonElement(out var json))
93-
return;
94-
if(!json.TryGetPropertyValue("message", out var Jvalue))
95-
return;
54+
private void ListBoxPropertyChanged(string str)
55+
{
56+
if (_webUiInit == false)
57+
return;
58+
if (str == "网络") CurrView = NetWorkModel;
59+
if (str == "日志") CurrView = LogViewModel;
60+
if (str == "连接") CurrView = WebUIConnectionViewModel;
61+
}
9662

97-
if(Jvalue.GetString() != "success")
98-
return;
99-
100-
var aut = await GetAuthentication(connectionValue.webURL, connectionValue.token);
101-
ConfigValue.WebToken = connectionValue.token;
102-
ConfigValue.AuthToken = aut;
103-
ConfigValue.WebUri = connectionValue.webURL;
104-
CurrView = LogViewModel;
105-
_webUiInit = true;
63+
private async Task WebUILoginEvent(IInteractionContext<(string, string), bool> interaction)
64+
{
65+
(string webURL, string token) connectionValue = interaction.Input;
66+
if (connectionValue.token == ConfigValue.WebToken && connectionValue.webURL == ConfigValue.WebUri) {
67+
interaction.SetOutput(false);
68+
return;
10669
}
70+
71+
var rms = await WebUILogin(connectionValue.webURL, connectionValue.token);
72+
var requJson = await rms.Content.ReadAsStringAsync();
73+
if (!requJson.GetJsonElement(out var json) ||
74+
!json.TryGetPropertyValue("message", out var Jvalue) ||
75+
Jvalue.GetString() != "success") {
76+
interaction.SetOutput(false);
77+
return;
78+
}
79+
80+
var aut = await GetAuthentication(connectionValue.webURL, connectionValue.token);
81+
ConfigValue.WebToken = connectionValue.token;
82+
ConfigValue.AuthToken = aut;
83+
ConfigValue.WebUri = connectionValue.webURL;
84+
interaction.SetOutput(true);
85+
CurrView = LogViewModel;
86+
_webUiInit = true;
10787
}
10888
}

NapCatScript.Desktop/ViewModels/NetWorkModels/ListViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private void IsExistNameAndEdit(ServerObject obj, ServerType type)
237237
/// <summary>
238238
/// 从WebUI拉取网络配置
239239
/// </summary>
240-
private async void GetWebUiNetWorkConfig()
240+
public async void GetWebUiNetWorkConfig()
241241
{
242242
string s = await Core.MsgHandle.Utils
243243
.GetNetWorkConfig(ConfigValue.WebUri, ConfigValue.AuthToken);

NapCatScript.Desktop/ViewModels/NetWorkViewModel.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using DynamicData.Binding;
2-
using NapCatScript.Core.NetWork.NetWorkModel;
31
using NapCatScript.Desktop.ViewModels.NetWorkModels;
42
using ReactiveUI;
53
using System;
@@ -12,28 +10,20 @@ public class NetWorkViewModel : ViewModelBase
1210
private ViewModelBase _currView;
1311
public IReactiveCommand OpenNewNetWorkConfigCommand { get; set; }
1412
public IReactiveCommand OpenWorkListCommand { get; set; }
15-
public ViewModelBase CurrView { get=> _currView; set => this.RaiseAndSetIfChanged(ref _currView, value); }
13+
public ViewModelBase CurrView { get => _currView; set => this.RaiseAndSetIfChanged(ref _currView, value); }
1614
private ViewModelBase? _createViewModel;
1715
private ViewModelBase CreateViewModel
1816
{
1917
get {
20-
if (_createViewModel is null) {
21-
_createViewModel = new NetWorkCreateViewModel();
22-
return _createViewModel;
23-
}
24-
return _createViewModel;
18+
return _createViewModel ??= new NetWorkCreateViewModel();
2519
}
2620
}
2721

2822
private ViewModelBase? _listViewModel;
2923
private ViewModelBase ListViewModel
3024
{
3125
get {
32-
if (_listViewModel is null) {
33-
_listViewModel = new ListViewModel();
34-
return _listViewModel;
35-
}
36-
return _listViewModel;
26+
return _listViewModel ??= new ListViewModel();
3727
}
3828
}
3929
public NetWorkViewModel()
@@ -55,6 +45,8 @@ private void OpenNewNetWorkConfig()
5545
private void OpenWorkList()
5646
{
5747
CurrView = ListViewModel;
48+
var t = (ListViewModel)ListViewModel;
49+
t.GetWebUiNetWorkConfig();
5850
}
5951
}
6052

0 commit comments

Comments
 (0)