|
|
|
@ -1,12 +1,11 @@
|
|
|
|
|
using CameraCard.Data;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web.Script.Serialization;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
@ -57,8 +56,11 @@ namespace CameraCard
|
|
|
|
|
{
|
|
|
|
|
if (db.Users.Any())
|
|
|
|
|
{
|
|
|
|
|
if (db.Users.Any(o => o.UserName == userName && o.Password == password))
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
@ -71,63 +73,86 @@ namespace CameraCard
|
|
|
|
|
{
|
|
|
|
|
this.BeginInvoke(new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
if (this._progress == null)
|
|
|
|
|
this.LoginFromServer(userName, password);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoginFromServer(string userName, string password)
|
|
|
|
|
{
|
|
|
|
|
if (this._progress == null)
|
|
|
|
|
{
|
|
|
|
|
var thread = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
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("正在登录,请稍候", 60);
|
|
|
|
|
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 thread = new Thread(() =>
|
|
|
|
|
var user = db.Users.FirstOrDefault(o => o.UserName == userName);
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
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, () =>
|
|
|
|
|
user = new User { UserName = userName, Password = password, PasswordHash = result.data };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this._progress = null;
|
|
|
|
|
thread.Abort();
|
|
|
|
|
});
|
|
|
|
|
thread.Start();
|
|
|
|
|
this._progress.ShowDialog();
|
|
|
|
|
user.Password = password;
|
|
|
|
|
user.PasswordHash = result.data;
|
|
|
|
|
}
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
MainForm.User = user;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
this._progress?.SetProgress("登录成功,正在加载数据", 50);
|
|
|
|
|
MainForm.LoadData(this._progress);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|