You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/labs/CameraCard/CameraCard/LoginForm.cs

171 lines
6.6 KiB

using CameraCard.Data;
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
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())
{
var user = db.Users.FirstOrDefault(o => o.UserName == userName && o.Password == password);
if (user != null)
{
this._progress?.SetProgress("本地登录成功", 100);
MainForm.User = user;
this.Hide();
new MainForm().Show();
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
else
{
this.BeginInvoke(new Action(() =>
{
this.LoginFromServer(userName, password);
}));
}
}
}
}
private void LoginFromServer(string userName, string password)
{
if (this._progress == null)
{
var thread = new Thread(() =>
{
try
{
this._progress?.SetProgress("正在登录,请稍候", 30);
var url = ConfigurationManager.AppSettings["login"];
var client = HttpClientFactory.Create();
var content = new StringContent(new JavaScriptSerializer().Serialize(new { userName = userName, password = password }), Encoding.UTF8, "application/json");
var response = client.PostAsync(url, content).Result;
this._progress?.SetProgress("正在登录,请稍候", 50);
if (response.StatusCode == HttpStatusCode.OK)
{
var result = new JavaScriptSerializer().Deserialize<LoginResponse>(response.Content.ReadAsStringAsync().Result);
if (result.code == 0)
{
using (var db = new MyDbContext())
{
var user = db.Users.FirstOrDefault(o => o.UserName == userName);
if (user == null)
{
user = new User { UserName = userName, Password = password, PasswordHash = result.data };
db.Users.Add(user);
}
else
{
user.Password = password;
user.PasswordHash = result.data;
}
db.SaveChanges();
MainForm.User = user;
}
this._progress?.SetProgress("登录成功,正在更新数据", 50);
MainForm.LoadData(this._progress);
this._progress?.SetProgress("数据更新完毕", 100);
this._progress.CloseProgress();
this.Invoke(new Action(() =>
{
this.Hide();
new MainForm().Show();
}));
}
else
{
MessageBox.Show($"错误代码:{result.code},错误消息:{result.message}");
this._progress?.SetProgress("登录失败", 100);
this._progress.CloseProgress();
}
}
else
{
MessageBox.Show(response.Content.ReadAsStringAsync().Result);
this._progress?.SetProgress("登录失败", 100);
this._progress.CloseProgress();
}
}
catch (ThreadAbortException ex)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
this._progress = new ProgressForm("登录中", "正在登录,请稍候", 0, 100, this.Width, () =>
{
this._progress = null;
if (thread != null && thread.IsAlive)
{
thread.Abort();
}
});
thread.Start();
this._progress.ShowDialog();
}
}
private void LoginForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.login_Click(sender, e);
}
}
}
}