From e0b0273b4d38a01b19ace75d25affe73f7da5622 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 23 Nov 2021 16:41:59 +0800 Subject: [PATCH] update --- src/PhotoCollector/MainForm.Designer.cs | 29 +-------- src/PhotoCollector/MainForm.cs | 83 ++++++++++++++++++------- src/PhotoCollector/MainForm.resx | 3 - src/PhotoCollector/WebView2Interop.cs | 15 +++++ src/WebApiTestServer/wwwroot/index.html | 61 +++++++++++------- 5 files changed, 119 insertions(+), 72 deletions(-) create mode 100644 src/PhotoCollector/WebView2Interop.cs diff --git a/src/PhotoCollector/MainForm.Designer.cs b/src/PhotoCollector/MainForm.Designer.cs index 6d64f0e..546ec5b 100644 --- a/src/PhotoCollector/MainForm.Designer.cs +++ b/src/PhotoCollector/MainForm.Designer.cs @@ -30,12 +30,9 @@ { this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); - this.menuStrip1 = new System.Windows.Forms.MenuStrip(); - this.配置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.webView21 = new Microsoft.Web.WebView2.WinForms.WebView2(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.statusStrip1.SuspendLayout(); - this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.webView21)).BeginInit(); this.SuspendLayout(); // @@ -55,30 +52,14 @@ this.toolStripStatusLabel1.Size = new System.Drawing.Size(131, 17); this.toolStripStatusLabel1.Text = "toolStripStatusLabel1"; // - // menuStrip1 - // - this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.配置ToolStripMenuItem}); - this.menuStrip1.Location = new System.Drawing.Point(0, 0); - this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(800, 25); - this.menuStrip1.TabIndex = 1; - this.menuStrip1.Text = "menuStrip1"; - // - // 配置ToolStripMenuItem - // - this.配置ToolStripMenuItem.Name = "配置ToolStripMenuItem"; - this.配置ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); - this.配置ToolStripMenuItem.Text = "配置"; - // // webView21 // this.webView21.CreationProperties = null; this.webView21.DefaultBackgroundColor = System.Drawing.Color.White; this.webView21.Dock = System.Windows.Forms.DockStyle.Fill; - this.webView21.Location = new System.Drawing.Point(0, 25); + this.webView21.Location = new System.Drawing.Point(0, 0); this.webView21.Name = "webView21"; - this.webView21.Size = new System.Drawing.Size(800, 403); + this.webView21.Size = new System.Drawing.Size(800, 428); this.webView21.TabIndex = 2; this.webView21.ZoomFactor = 1D; // @@ -89,15 +70,11 @@ this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.webView21); this.Controls.Add(this.statusStrip1); - this.Controls.Add(this.menuStrip1); - this.MainMenuStrip = this.menuStrip1; this.Name = "MainForm"; this.Text = "Form1"; this.Shown += new System.EventHandler(this.MainForm_Shown); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); - this.menuStrip1.ResumeLayout(false); - this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.webView21)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -108,8 +85,6 @@ private StatusStrip statusStrip1; private ToolStripStatusLabel toolStripStatusLabel1; - private MenuStrip menuStrip1; - private ToolStripMenuItem 配置ToolStripMenuItem; private Microsoft.Web.WebView2.WinForms.WebView2 webView21; private FolderBrowserDialog folderBrowserDialog1; } diff --git a/src/PhotoCollector/MainForm.cs b/src/PhotoCollector/MainForm.cs index 079423d..958f6ad 100644 --- a/src/PhotoCollector/MainForm.cs +++ b/src/PhotoCollector/MainForm.cs @@ -1,7 +1,6 @@ using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; using System.Security.Cryptography; -using System.Text; using System.Text.Encodings.Web; using System.Text.Json; @@ -59,24 +58,39 @@ namespace PhotoCollector webView.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested; webView.CoreWebView2.Settings.UserAgent = $"{webView.CoreWebView2.Settings.UserAgent}|{AppContext.BaseDirectory}"; webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived; + AddEventHandler(); + webView.CoreWebView2.AddHostObjectToScript("dotnet", new WebView2Interop()); webView.CoreWebView2.Navigate("https://localhost:7235/"); } - private void CoreWebView2_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e) + private void AddEventHandler() { - var messages = JsonSerializer.Deserialize>(e.WebMessageAsJson); - var command = messages["command"].ToString(); - if (command == "folder") + WebView2Interop.FuncList.Add("selectPath", o => { - this.folderBrowserDialog1.SelectedPath = messages["path"]; + var code = 0; + var message = "ɹ"; + var path = o["path"]; + this.folderBrowserDialog1.SelectedPath = path; if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK) { - this.webView21.ExecuteScriptAsync($"onfolderSelected({JsonSerializer.Serialize(folderBrowserDialog1.SelectedPath, jsonSerializerOptions)})"); + path = folderBrowserDialog1.SelectedPath; } - } - else if (command == "read") + else + { + code = 1; + message = "δѡκĿ¼"; + path = String.Empty; + } + return JsonSerializer.Serialize(new + { + path, + code, + message + }, jsonSerializerOptions); + }); + WebView2Interop.FuncList.Add("readFile", o => { - var file = messages["file"]; + var file = o["file"]; var base64 = string.Empty; var code = 0; var message = string.Empty; @@ -106,21 +120,44 @@ namespace PhotoCollector code = 1; message = $"ļ:{file}"; } - var args = JsonSerializer.Serialize(new + return JsonSerializer.Serialize(new { file, base64, code, message }, jsonSerializerOptions); - this.webView21.ExecuteScriptAsync($"onFileReceived({args})"); - } - else if (command == "save") + }); + WebView2Interop.FuncList.Add("saveFile", o => + { + var code = 0; + var message = "ɹ"; + try + { + var file = o["file"]; + File.WriteAllBytes(file, Convert.FromBase64String(o["base64"].Split(',')[1])); + File.WriteAllText(file + ".meta", FileMd5(file)); + } + 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); + var command = dict["command"]; + if (command == "setStatus") { - var file = messages["file"]; - File.WriteAllBytes(file, Convert.FromBase64String(messages["base64"].Split(',')[1])); - File.WriteAllText(file + ".meta", FileMd5(file)); - this.webView21.ExecuteScriptAsync($"alert({JsonSerializer.Serialize(messages["file"])})"); + this.toolStripStatusLabel1.Text = dict["message"]; } } @@ -144,11 +181,15 @@ namespace PhotoCollector } try { - //this.Text = o ? "" : "δ"; + var msg = o ? "" : "δ"; + if (this.toolStripStatusLabel1.Text != msg) + { + this.toolStripStatusLabel1.Text = msg; + } } catch (Exception ex) { - MessageBox.Show(ex.Message); + this.toolStripStatusLabel1.Text = ex.Message; } })); }); @@ -164,7 +205,7 @@ namespace PhotoCollector { this.toolStripStatusLabel1.Text = o.Name; var param = JsonSerializer.Serialize(o, jsonSerializerOptions); - this.webView21.ExecuteScriptAsync($"onRead({param})"); + this.webView21.ExecuteScriptAsync($"onIdCardRead({param})"); } catch (Exception ex) { diff --git a/src/PhotoCollector/MainForm.resx b/src/PhotoCollector/MainForm.resx index 5e33791..642ca77 100644 --- a/src/PhotoCollector/MainForm.resx +++ b/src/PhotoCollector/MainForm.resx @@ -60,9 +60,6 @@ 17, 17 - - 138, 17 - 258, 17 diff --git a/src/PhotoCollector/WebView2Interop.cs b/src/PhotoCollector/WebView2Interop.cs new file mode 100644 index 0000000..27df968 --- /dev/null +++ b/src/PhotoCollector/WebView2Interop.cs @@ -0,0 +1,15 @@ +using System.Text.Json; + +namespace PhotoCollector +{ + public class WebView2Interop + { + public static Dictionary, string>> FuncList = new Dictionary, string>>(); + + public string Action(string message) + { + var json = JsonSerializer.Deserialize>(message); + return FuncList[json["command"]].Invoke(json); + } + } +} \ No newline at end of file diff --git a/src/WebApiTestServer/wwwroot/index.html b/src/WebApiTestServer/wwwroot/index.html index 9998163..495f04d 100644 --- a/src/WebApiTestServer/wwwroot/index.html +++ b/src/WebApiTestServer/wwwroot/index.html @@ -51,8 +51,8 @@ - - + +
@@ -65,31 +65,50 @@