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.
134 lines
5.7 KiB
134 lines
5.7 KiB
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<LoginResponse>(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();
|
|
}
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |