master
wanggang 4 years ago
parent c15c81d0b1
commit 8fa7f8eb2f

@ -0,0 +1,80 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace PhotoCollector.Data
{
internal class AppDbContext : DbContext
{
public DbSet<Config> 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<Config> 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; }
}
}

@ -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();

@ -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;
@ -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,6 +155,86 @@ 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<Config> value = new List<Config>();
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)
@ -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}";
}
}
}

@ -10,6 +10,7 @@
<DebugType>full</DebugType>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<SatelliteResourceLanguages>zh-Hans</SatelliteResourceLanguages>
<Version>0.1.0</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@ -21,8 +22,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1020.30" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />

@ -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()));

@ -5,6 +5,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<title>模拟接口</title>
<style>
body {
width: 100vw;
height: 100vh;
}
:root {
--width: 358px;
--height: 441px;
@ -53,6 +58,10 @@
<button id="folderBrowser">选择目录</button>
<button id="readFile">读取图片</button>
<button id="saveFile">保存图片</button>
<button id="getConfig">读取配置</button>
<button id="setConfig">保存配置</button>
<button id="removeConfig">移除配置</button>
<button id="getConfigs">查询配置</button>
<hr />
<div class="videoWrapper">
<video id="webcam"></video>
@ -71,6 +80,7 @@
document.getElementById('idCard').src = card.image;
}
//文件夹浏览
document.getElementById("folderBrowser").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: "selectPath",
@ -81,10 +91,26 @@
alert(result.message);
}
else {
alert(result.path);
alert(`选择的文件夹:${result.path}`);
}
}
//读取文件base64
document.getElementById("readFile").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'readFile',
file: appBasePath + "asserts/test.webp"
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
document.getElementById('idCard').src = `data:image/webp;base64,${result.base64}`;
}
}
//写入文件
document.getElementById("saveFile").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'saveFile',
@ -95,19 +121,69 @@
if (result.code) {
alert(result.message);
}
else {
alert("写入文件成功");
}
};
document.getElementById("readFile").onclick = function () {
//获取配置
document.getElementById("getConfig").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'readFile',
file: appBasePath + "asserts/test.webp"
command: 'getConfig',
key: "test"
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
document.getElementById('idCard').src = `data:image/webp;base64,${result.base64}`;
alert(result.value);
}
}
//查询配置
document.getElementById("getConfigs").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'getConfigs',
key: "test"
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
alert(JSON.stringify(result.value));
}
}
//设置配置
document.getElementById("setConfig").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'setConfig',
key: "test",
value: `当前时间:${new Date().toLocaleString()}`
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
alert("设置配置成功");
}
}
//删除配置,name和url不能删除
document.getElementById("removeConfig").onclick = function () {
var response = window.chrome.webview.hostObjects.sync.dotnet.Action(JSON.stringify({
command: 'removeConfig',
key: "test"
}));
var result = JSON.parse(response);
if (result.code) {
alert(result.message);
}
else {
alert("删除配置成功");
}
}
</script>

Loading…
Cancel
Save