|
|
|
@ -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<Dictionary<string, string>>(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<Dictionary<string, string>>(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)
|
|
|
|
|
{
|
|
|
|
|