From 9009f627d147b5aac34792e6853fc81ae4ad3f36 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 25 Feb 2020 16:27:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E7=AA=97?= =?UTF-8?q?=E4=BD=93=E5=92=8C=E8=BF=9B=E5=BA=A6=E6=9D=A1=E7=AA=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 751e4990acbb560cc2e0914a4e2a9ad95a8fc004 --- labs/CameraCard/CameraCard/App.config | 14 +- labs/CameraCard/CameraCard/CameraCard.csproj | 24 +++ .../CameraCard/Data/LoginResponse.cs | 9 ++ .../CameraCard/CameraCard/Data/MyDbContext.cs | 2 +- labs/CameraCard/CameraCard/Data/User.cs | 1 + .../CameraCard/LoginForm.Designer.cs | 147 ++++++++++++++++++ labs/CameraCard/CameraCard/LoginForm.cs | 134 ++++++++++++++++ labs/CameraCard/CameraCard/LoginForm.resx | 120 ++++++++++++++ .../CameraCard/MainForm.Designer.cs | 19 ++- labs/CameraCard/CameraCard/MainForm.cs | 10 +- labs/CameraCard/CameraCard/Program.cs | 2 +- .../CameraCard/ProgressForm.Designer.cs | 86 ++++++++++ labs/CameraCard/CameraCard/ProgressForm.cs | 42 +++++ labs/CameraCard/CameraCard/ProgressForm.resx | 123 +++++++++++++++ 14 files changed, 713 insertions(+), 20 deletions(-) create mode 100644 labs/CameraCard/CameraCard/Data/LoginResponse.cs create mode 100644 labs/CameraCard/CameraCard/LoginForm.Designer.cs create mode 100644 labs/CameraCard/CameraCard/LoginForm.cs create mode 100644 labs/CameraCard/CameraCard/LoginForm.resx create mode 100644 labs/CameraCard/CameraCard/ProgressForm.Designer.cs create mode 100644 labs/CameraCard/CameraCard/ProgressForm.cs create mode 100644 labs/CameraCard/CameraCard/ProgressForm.resx diff --git a/labs/CameraCard/CameraCard/App.config b/labs/CameraCard/CameraCard/App.config index 4ac19ac7..16751609 100644 --- a/labs/CameraCard/CameraCard/App.config +++ b/labs/CameraCard/CameraCard/App.config @@ -1,10 +1,12 @@ - - + + + + - - - - + + + + \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/CameraCard.csproj b/labs/CameraCard/CameraCard/CameraCard.csproj index b47b495c..7cf5f938 100644 --- a/labs/CameraCard/CameraCard/CameraCard.csproj +++ b/labs/CameraCard/CameraCard/CameraCard.csproj @@ -65,7 +65,9 @@ .\DirectShowLib-2005.dll + + @@ -78,6 +80,7 @@ + @@ -95,6 +98,12 @@ + + Form + + + LoginForm.cs + Form @@ -102,13 +111,25 @@ MainForm.cs + + Form + + + ProgressForm.cs + IdCard.cs + + LoginForm.cs + MainForm.cs + + ProgressForm.cs + ResXFileCodeGenerator Resources.Designer.cs @@ -134,6 +155,9 @@ + + 5.2.7 + 3.1.2 diff --git a/labs/CameraCard/CameraCard/Data/LoginResponse.cs b/labs/CameraCard/CameraCard/Data/LoginResponse.cs new file mode 100644 index 00000000..44aaa4d5 --- /dev/null +++ b/labs/CameraCard/CameraCard/Data/LoginResponse.cs @@ -0,0 +1,9 @@ +namespace CameraCard.Data +{ + public class LoginResponse + { + public int code { get; set; } + public string message { get; set; } + public string data { get; set; } + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/Data/MyDbContext.cs b/labs/CameraCard/CameraCard/Data/MyDbContext.cs index 6e2203eb..f6e28376 100644 --- a/labs/CameraCard/CameraCard/Data/MyDbContext.cs +++ b/labs/CameraCard/CameraCard/Data/MyDbContext.cs @@ -22,7 +22,7 @@ namespace CameraCard.Data private void Seed() { - this.Users.Add(new User { UserName = "admin", PasswordHash = "123456" }); + //this.Users.Add(new User { UserName = "admin", Password = "123456" }); } } } \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/Data/User.cs b/labs/CameraCard/CameraCard/Data/User.cs index 636067e4..256f93db 100644 --- a/labs/CameraCard/CameraCard/Data/User.cs +++ b/labs/CameraCard/CameraCard/Data/User.cs @@ -3,6 +3,7 @@ public class User : Entity { public string UserName { get; set; } + public string Password { get; set; } public string PasswordHash { get; set; } } } \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/LoginForm.Designer.cs b/labs/CameraCard/CameraCard/LoginForm.Designer.cs new file mode 100644 index 00000000..5d009ace --- /dev/null +++ b/labs/CameraCard/CameraCard/LoginForm.Designer.cs @@ -0,0 +1,147 @@ +namespace CameraCard +{ + partial class LoginForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.userNameInput = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.passwordInput = new System.Windows.Forms.TextBox(); + this.login = new System.Windows.Forms.Button(); + this.userNameValid = new System.Windows.Forms.Label(); + this.passwordValid = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(41, 56); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(53, 12); + this.label1.TabIndex = 0; + this.label1.Text = "登录名:"; + // + // userNameInput + // + this.userNameInput.Location = new System.Drawing.Point(96, 50); + this.userNameInput.Name = "userNameInput"; + this.userNameInput.Size = new System.Drawing.Size(157, 21); + this.userNameInput.TabIndex = 1; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(56, 18); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(197, 12); + this.label2.TabIndex = 2; + this.label2.Text = "普通高中学业水平考试信息管理系统"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(41, 105); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(41, 12); + this.label3.TabIndex = 3; + this.label3.Text = "密码:"; + // + // passwordInput + // + this.passwordInput.Location = new System.Drawing.Point(96, 100); + this.passwordInput.Name = "passwordInput"; + this.passwordInput.PasswordChar = '*'; + this.passwordInput.Size = new System.Drawing.Size(157, 21); + this.passwordInput.TabIndex = 4; + // + // login + // + this.login.Location = new System.Drawing.Point(96, 149); + this.login.Name = "login"; + this.login.Size = new System.Drawing.Size(157, 30); + this.login.TabIndex = 5; + this.login.Text = "登录"; + this.login.UseVisualStyleBackColor = true; + this.login.Click += new System.EventHandler(this.login_Click); + // + // userNameValid + // + this.userNameValid.AutoSize = true; + this.userNameValid.ForeColor = System.Drawing.Color.Red; + this.userNameValid.Location = new System.Drawing.Point(96, 78); + this.userNameValid.Name = "userNameValid"; + this.userNameValid.Size = new System.Drawing.Size(29, 12); + this.userNameValid.TabIndex = 6; + this.userNameValid.Text = "验证"; + // + // passwordValid + // + this.passwordValid.AutoSize = true; + this.passwordValid.ForeColor = System.Drawing.Color.Red; + this.passwordValid.Location = new System.Drawing.Point(95, 127); + this.passwordValid.Name = "passwordValid"; + this.passwordValid.Size = new System.Drawing.Size(29, 12); + this.passwordValid.TabIndex = 7; + this.passwordValid.Text = "验证"; + // + // LoginForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(304, 202); + this.Controls.Add(this.passwordValid); + this.Controls.Add(this.userNameValid); + this.Controls.Add(this.login); + this.Controls.Add(this.passwordInput); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.userNameInput); + this.Controls.Add(this.label1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.Name = "LoginForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "登录"; + this.Load += new System.EventHandler(this.LoginForm_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox userNameInput; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox passwordInput; + private System.Windows.Forms.Button login; + private System.Windows.Forms.Label userNameValid; + private System.Windows.Forms.Label passwordValid; + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/LoginForm.cs b/labs/CameraCard/CameraCard/LoginForm.cs new file mode 100644 index 00000000..1ff671b0 --- /dev/null +++ b/labs/CameraCard/CameraCard/LoginForm.cs @@ -0,0 +1,134 @@ +using CameraCard.Data; +using System; +using System.Configuration; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using System.Web.Script.Serialization; +using System.Windows.Forms; + +namespace CameraCard +{ + public partial class LoginForm : Form + { + private ProgressForm _progress; + + public LoginForm() + { + InitializeComponent(); + this.userNameValid.Text = ""; + this.passwordValid.Text = ""; + } + + private void LoginForm_Load(object sender, EventArgs e) + { + MyDbContext.Init(); + } + + private void login_Click(object sender, EventArgs e) + { + var valid = true; + if (string.IsNullOrWhiteSpace(this.userNameInput.Text)) + { + valid = false; + this.userNameValid.Text = "用户名不能为空"; + } + else + { + this.userNameValid.Text = ""; + } + if (string.IsNullOrWhiteSpace(this.passwordInput.Text)) + { + valid = false; + this.passwordValid.Text = "用户名不能为空"; + } + else + { + this.passwordValid.Text = ""; + } + if (valid) + { + var userName = this.userNameInput.Text.Trim(); + var password = this.passwordInput.Text.Trim(); + using (var db = new MyDbContext()) + { + if (db.Users.Any()) + { + if (db.Users.Any(o => o.UserName == userName && o.Password == password)) + { + this.Hide(); + new MainForm().Show(); + } + else + { + MessageBox.Show("用户名或密码错误"); + } + } + else + { + this.BeginInvoke(new Action(() => + { + if (this._progress == null) + { + var thread = new Thread(() => + { + this._progress?.SetProgress("正在登录,请稍候", 30); + var url = ConfigurationManager.AppSettings["login"]; + var client = HttpClientFactory.Create(); + var content = new JavaScriptSerializer().Serialize(new { userName = userName, password = password }); + var response = client.PostAsync(url, new StringContent(content)).Result; + this._progress?.SetProgress("正在登录,请稍候", 60); + if (response.StatusCode == HttpStatusCode.OK) + { + var result = new JavaScriptSerializer().Deserialize(response.Content.ReadAsStringAsync().Result); + if (result.code == 0) + { + var user = db.Users.FirstOrDefault(o => o.UserName == userName); + if (user == null) + { + user = new User { UserName = userName, Password = password, PasswordHash = result.data }; + } + else + { + user.Password = password; + user.PasswordHash = result.data; + } + db.SaveChanges(); + //this.Update(); + this._progress?.SetProgress("登录成功", 100); + this._progress.Close(); + this.Hide(); + new MainForm().Show(); + } + else + { + MessageBox.Show(result.message); + this._progress?.SetProgress("登录失败", 100); + this._progress.Close(); + } + } + else + { + MessageBox.Show(response.Content.ReadAsStringAsync().Result); + this._progress?.SetProgress("登录失败", 100); + this._progress.Close(); + } + }); + this._progress = new ProgressForm("登录中", "正在登录,请稍候", 0, 100, this.Width, () => + { + this._progress = null; + thread.Abort(); + }); + thread.Start(); + this._progress.ShowDialog(); + } + })); + } + } + } + } + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/LoginForm.resx b/labs/CameraCard/CameraCard/LoginForm.resx new file mode 100644 index 00000000..1af7de15 --- /dev/null +++ b/labs/CameraCard/CameraCard/LoginForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/MainForm.Designer.cs b/labs/CameraCard/CameraCard/MainForm.Designer.cs index 05e32237..5fb645d1 100644 --- a/labs/CameraCard/CameraCard/MainForm.Designer.cs +++ b/labs/CameraCard/CameraCard/MainForm.Designer.cs @@ -30,6 +30,7 @@ { System.Windows.Forms.TabPage tab1; this.idcGroupBox = new System.Windows.Forms.GroupBox(); + this.idCard = new CameraCard.IdCard(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.photo = new System.Windows.Forms.PictureBox(); this.shotBtn = new System.Windows.Forms.Button(); @@ -58,7 +59,6 @@ this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.versionLabel = new System.Windows.Forms.ToolStripStatusLabel(); - this.idCard = new CameraCard.IdCard(); tab1 = new System.Windows.Forms.TabPage(); tab1.SuspendLayout(); this.idcGroupBox.SuspendLayout(); @@ -102,6 +102,13 @@ this.idcGroupBox.TabStop = false; this.idcGroupBox.Text = "身份证"; // + // idCard + // + this.idCard.Location = new System.Drawing.Point(-1, 16); + this.idCard.Name = "idCard"; + this.idCard.Size = new System.Drawing.Size(420, 200); + this.idCard.TabIndex = 8; + // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) @@ -388,13 +395,6 @@ this.versionLabel.Size = new System.Drawing.Size(14, 17); this.versionLabel.Text = "v"; // - // idCard - // - this.idCard.Location = new System.Drawing.Point(-1, 16); - this.idCard.Name = "idCard"; - this.idCard.Size = new System.Drawing.Size(420, 200); - this.idCard.TabIndex = 8; - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); @@ -402,11 +402,14 @@ this.ClientSize = new System.Drawing.Size(1008, 729); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.tabControl1); + this.KeyPreview = true; this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "MainFrom"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainForm_FormClosed); this.Load += new System.EventHandler(this.MainFrom_Load); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.shotBtn_Click); tab1.ResumeLayout(false); this.idcGroupBox.ResumeLayout(false); this.groupBox1.ResumeLayout(false); diff --git a/labs/CameraCard/CameraCard/MainForm.cs b/labs/CameraCard/CameraCard/MainForm.cs index 5380d9f1..57beb7c0 100644 --- a/labs/CameraCard/CameraCard/MainForm.cs +++ b/labs/CameraCard/CameraCard/MainForm.cs @@ -1,11 +1,9 @@ -using CameraCard.Data; -using System; +using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Reflection; -using System.Runtime.InteropServices; using System.Windows.Forms; namespace CameraCard @@ -21,7 +19,6 @@ namespace CameraCard InitializeComponent(); this.versionLabel.Alignment = ToolStripItemAlignment.Right; this.versionLabel.Text += Assembly.GetExecutingAssembly().GetName().Version.ToString(); - MyDbContext.Init(); } private void MainFrom_Load(object sender, EventArgs e) @@ -239,5 +236,10 @@ namespace CameraCard this._cameraHelper.Dispose(); this._pen.Dispose(); } + + private void MainForm_FormClosed(object sender, FormClosedEventArgs e) + { + Application.Exit(); + } } } \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/Program.cs b/labs/CameraCard/CameraCard/Program.cs index c766ee70..97911a4f 100644 --- a/labs/CameraCard/CameraCard/Program.cs +++ b/labs/CameraCard/CameraCard/Program.cs @@ -24,7 +24,7 @@ namespace CameraCard Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + Application.Run(new LoginForm()); GC.KeepAlive(mutex); } diff --git a/labs/CameraCard/CameraCard/ProgressForm.Designer.cs b/labs/CameraCard/CameraCard/ProgressForm.Designer.cs new file mode 100644 index 00000000..c4d7f574 --- /dev/null +++ b/labs/CameraCard/CameraCard/ProgressForm.Designer.cs @@ -0,0 +1,86 @@ +namespace CameraCard +{ + partial class ProgressForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.progressBar = new System.Windows.Forms.ProgressBar(); + this.toolStrip1 = new System.Windows.Forms.ToolStrip(); + this.status = new System.Windows.Forms.ToolStripLabel(); + this.toolStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // progressBar + // + this.progressBar.Dock = System.Windows.Forms.DockStyle.Top; + this.progressBar.Location = new System.Drawing.Point(0, 0); + this.progressBar.Name = "progressBar"; + this.progressBar.Size = new System.Drawing.Size(284, 20); + this.progressBar.TabIndex = 0; + // + // toolStrip1 + // + this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Bottom; + this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.status}); + this.toolStrip1.Location = new System.Drawing.Point(0, 26); + this.toolStrip1.Name = "toolStrip1"; + this.toolStrip1.Size = new System.Drawing.Size(284, 25); + this.toolStrip1.TabIndex = 1; + this.toolStrip1.Text = "toolStrip1"; + // + // status + // + this.status.Name = "status"; + this.status.Size = new System.Drawing.Size(32, 22); + this.status.Text = "就绪"; + // + // ProgressForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 51); + this.Controls.Add(this.toolStrip1); + this.Controls.Add(this.progressBar); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.Name = "ProgressForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "任务执行中"; + this.toolStrip1.ResumeLayout(false); + this.toolStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.ProgressBar progressBar; + private System.Windows.Forms.ToolStrip toolStrip1; + private System.Windows.Forms.ToolStripLabel status; + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/ProgressForm.cs b/labs/CameraCard/CameraCard/ProgressForm.cs new file mode 100644 index 00000000..e7a4419e --- /dev/null +++ b/labs/CameraCard/CameraCard/ProgressForm.cs @@ -0,0 +1,42 @@ +using System; +using System.Windows.Forms; + +namespace CameraCard +{ + public partial class ProgressForm : Form + { + private readonly Action _closed; + + public ProgressForm(string title, string status, int min, int max, int width = 0, Action action = null) + { + InitializeComponent(); + this.Width = width == 0 ? this.Width : width; + this.Text = title; + this.status.Text = status; + this.progressBar.Minimum = min; + this.progressBar.Maximum = max; + this._closed = action; + //this.ControlBox = this._closed != null; + this.FormClosed += ProgressForm_FormClosed; + } + + private void ProgressForm_FormClosed(object sender, FormClosedEventArgs e) + { + this._closed?.Invoke(); + } + + public void SetProgress(string label, int progress) + { + this.Invoke(new Action(() => + { + this.status.Text = label; + this.progressBar.Value = progress; + })); + } + + public void CloseProgress() + { + this.CloseProgress(); + } + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/ProgressForm.resx b/labs/CameraCard/CameraCard/ProgressForm.resx new file mode 100644 index 00000000..5da7a24d --- /dev/null +++ b/labs/CameraCard/CameraCard/ProgressForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file