From 8fa7f8eb2f815869609150de8888fd0a9ffb3287 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 24 Nov 2021 11:36:44 +0800 Subject: [PATCH] update --- src/PhotoCollector/Data/AppDbContext.cs | 80 +++++++++++++++++++++ src/PhotoCollector/MainForm.Designer.cs | 10 +-- src/PhotoCollector/MainForm.cs | 92 +++++++++++++++++++++++- src/PhotoCollector/PhotoCollector.csproj | 4 +- src/PhotoCollector/Program.cs | 3 + src/WebApiTestServer/wwwroot/index.html | 86 ++++++++++++++++++++-- 6 files changed, 261 insertions(+), 14 deletions(-) create mode 100644 src/PhotoCollector/Data/AppDbContext.cs diff --git a/src/PhotoCollector/Data/AppDbContext.cs b/src/PhotoCollector/Data/AppDbContext.cs new file mode 100644 index 0000000..b5a9828 --- /dev/null +++ b/src/PhotoCollector/Data/AppDbContext.cs @@ -0,0 +1,80 @@ +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; + +namespace PhotoCollector.Data +{ + internal class AppDbContext : DbContext + { + public DbSet Configs { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=config.db"); + } + + public static void Init() + { + using var db = new AppDbContext(); + if (db.Database.EnsureCreated()) + { + db.Configs.Add(new Config { Key = "test", Value = "testValue" }); + db.Configs.Add(new Config { Key = "testKey1", Value = "testValue1" }); + db.Configs.Add(new Config { Key = "testKey2", Value = "testValue2" }); + db.Configs.Add(new Config { Key = "name", Value = $"测试程序" }); + db.Configs.Add(new Config { Key = "version", Value = Application.ProductVersion }); + db.Configs.Add(new Config { Key = "url", Value = "https://localhost:7235/" }); + db.SaveChanges(); + } + } + + public static string getConfig(string key) + { + using var db = new AppDbContext(); + return db.Configs.SingleOrDefault(o => o.Key == key)?.Value; + } + + public static void setConfig(string key, string value) + { + using var db = new AppDbContext(); + var config = db.Configs.SingleOrDefault(o => o.Key == key); + if (config == null) + { + config = new Config { Key = key, Value = value }; + db.Configs.Add(config); + } + config.Value = value; + db.SaveChanges(); + } + + public static void removeConfig(string key) + { + var skips = new string[] { "name", "version", "url" }; + using var db = new AppDbContext(); + var config = db.Configs.SingleOrDefault(o => o.Key == key); + if (config != null && !skips.Contains(config.Key)) + { + db.Configs.Remove(config); + db.SaveChanges(); + } + } + + public static List getConfigs(string key) + { + using var db = new AppDbContext(); + var query = db.Configs.AsQueryable(); + if (!string.IsNullOrWhiteSpace(key)) + { + query = query.Where(o => o.Key.Contains(key)); + } + return query.ToList(); + } + } + + public class Config + { + [Key] + public string Key { get; set; } + + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/src/PhotoCollector/MainForm.Designer.cs b/src/PhotoCollector/MainForm.Designer.cs index 546ec5b..e9bb179 100644 --- a/src/PhotoCollector/MainForm.Designer.cs +++ b/src/PhotoCollector/MainForm.Designer.cs @@ -40,9 +40,9 @@ // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripStatusLabel1}); - this.statusStrip1.Location = new System.Drawing.Point(0, 428); + this.statusStrip1.Location = new System.Drawing.Point(0, 707); this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 22); + this.statusStrip1.Size = new System.Drawing.Size(1008, 22); this.statusStrip1.TabIndex = 0; this.statusStrip1.Text = "statusStrip1"; // @@ -59,7 +59,7 @@ this.webView21.Dock = System.Windows.Forms.DockStyle.Fill; this.webView21.Location = new System.Drawing.Point(0, 0); this.webView21.Name = "webView21"; - this.webView21.Size = new System.Drawing.Size(800, 428); + this.webView21.Size = new System.Drawing.Size(1008, 707); this.webView21.TabIndex = 2; this.webView21.ZoomFactor = 1D; // @@ -67,11 +67,13 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1008, 729); this.Controls.Add(this.webView21); this.Controls.Add(this.statusStrip1); this.Name = "MainForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Form1"; + this.Load += new System.EventHandler(this.MainForm_Load); this.Shown += new System.EventHandler(this.MainForm_Shown); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); diff --git a/src/PhotoCollector/MainForm.cs b/src/PhotoCollector/MainForm.cs index 81d9ae5..c3c30bc 100644 --- a/src/PhotoCollector/MainForm.cs +++ b/src/PhotoCollector/MainForm.cs @@ -1,5 +1,6 @@ using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; +using PhotoCollector.Data; using Serilog; using System.Security.Cryptography; using System.Text.Encodings.Web; @@ -49,7 +50,7 @@ namespace PhotoCollector private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) { var webView = sender as WebView2; - if(webView!=null) + if (webView != null) { webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false; webView.CoreWebView2.Settings.IsPinchZoomEnabled = false; @@ -63,7 +64,7 @@ namespace PhotoCollector webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; AddEventHandler(); webView.CoreWebView2.AddHostObjectToScript("dotnet", new WebView2Interop()); - webView.CoreWebView2.Navigate("https://localhost:7235/"); + webView.CoreWebView2.Navigate(AppDbContext.getConfig("url")); } } @@ -154,12 +155,92 @@ namespace PhotoCollector message }, jsonSerializerOptions); }); + WebView2Interop.FuncList.Add("getConfig", o => + { + var code = 0; + var message = "ɹ"; + var value = string.Empty; + try + { + value = AppDbContext.getConfig(o["key"]); + } + catch (Exception ex) + { + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message, + value + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("getConfigs", o => + { + var code = 0; + var message = "ɹ"; + List value = new List(); + try + { + value = AppDbContext.getConfigs(o["key"]); + } + catch (Exception ex) + { + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message, + value + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("setConfig", o => + { + var code = 0; + var message = "ɹ"; + try + { + AppDbContext.setConfig(o["key"], o["value"]); + } + catch (Exception ex) + { + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("removeConfig", o => + { + var code = 0; + var message = "ɹ"; + try + { + AppDbContext.removeConfig(o["key"]); + } + catch (Exception ex) + { + code = 1; + message = ex.Message; + } + return JsonSerializer.Serialize(new + { + code, + message + }, jsonSerializerOptions); + }); } private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e) { var dict = JsonSerializer.Deserialize>(e.WebMessageAsJson); - if(dict != null) + if (dict != null) { var command = dict["command"]; if (command == "setStatus") @@ -236,5 +317,10 @@ namespace PhotoCollector stream.Dispose(); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } + + private void MainForm_Load(object sender, EventArgs e) + { + this.Text = $"{AppDbContext.getConfig("name")} v {Application.ProductVersion}"; + } } } \ No newline at end of file diff --git a/src/PhotoCollector/PhotoCollector.csproj b/src/PhotoCollector/PhotoCollector.csproj index 859be69..d459db1 100644 --- a/src/PhotoCollector/PhotoCollector.csproj +++ b/src/PhotoCollector/PhotoCollector.csproj @@ -10,6 +10,7 @@ full True zh-Hans + 0.1.0 @@ -21,8 +22,7 @@ - - + diff --git a/src/PhotoCollector/Program.cs b/src/PhotoCollector/Program.cs index 6d49b4d..c9691ef 100644 --- a/src/PhotoCollector/Program.cs +++ b/src/PhotoCollector/Program.cs @@ -1,3 +1,5 @@ +using Microsoft.Data.Sqlite; +using PhotoCollector.Data; using Serilog; using Serilog.Core; using Serilog.Events; @@ -36,6 +38,7 @@ namespace PhotoCollector Log.Information($"SetDllDirectory:{path}"); SetDllDirectory(path); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + AppDbContext.Init(); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += new ThreadExceptionEventHandler((s, e) => MessageBox.Show(e.Exception.Message)); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((s, e) => MessageBox.Show(e.ExceptionObject.ToString())); diff --git a/src/WebApiTestServer/wwwroot/index.html b/src/WebApiTestServer/wwwroot/index.html index 59cb8f9..dab0a52 100644 --- a/src/WebApiTestServer/wwwroot/index.html +++ b/src/WebApiTestServer/wwwroot/index.html @@ -5,6 +5,11 @@ 模拟接口