96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public partial class LoginForm : Form
|
|
{
|
|
private ApiController apiController;
|
|
|
|
public LoginForm()
|
|
{
|
|
InitializeComponent();
|
|
if (File.Exists(AppConstant.DEFAULT_LOGO_IMAGE))
|
|
{
|
|
pictureBoxImageLogo.Image = new Bitmap(AppConstant.DEFAULT_LOGO_IMAGE);
|
|
}
|
|
txtUsername.Focus();
|
|
lblLoginStatus.Text = string.Empty;
|
|
AcceptButton = btnLogin;
|
|
}
|
|
|
|
private async void Login()
|
|
{
|
|
if (string.IsNullOrEmpty(txtServerAddress.Text))
|
|
{
|
|
lblLoginStatus.Text = "Địa chỉ server không được để trống!";
|
|
return;
|
|
}
|
|
|
|
if (!Util.IsUrlValid(txtServerAddress.Text))
|
|
{
|
|
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n http://localhost:80 hoặc http://192.168.1.2:80";
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
apiController = new ApiController(txtServerAddress.Text);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
lblLoginStatus.Text = string.Empty;
|
|
string username = txtUsername.Text;
|
|
string password = txtPassword.Text;
|
|
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
|
{
|
|
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống!";
|
|
return;
|
|
}
|
|
|
|
var loginResult = await apiController.Login(new LoginModel { Username = username, Password = password });
|
|
if (loginResult.Exception != null)
|
|
{
|
|
var execeptioMessage = loginResult.Exception.Message;
|
|
if (execeptioMessage.Contains("Error converting value"))
|
|
{
|
|
lblLoginStatus.UpdateLabel("Tên tài khoản hoặc mật khẩu không đúng!", Color.Red);
|
|
return;
|
|
}
|
|
|
|
if (execeptioMessage.Contains("An error occurred while sending the request"))
|
|
{
|
|
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
|
|
return;
|
|
}
|
|
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
|
|
}
|
|
|
|
if (loginResult.IsLoginSuccess)
|
|
{
|
|
new AIParkingApplicationForm(apiController, loginResult.LoginData).Show();
|
|
Hide();
|
|
}
|
|
}
|
|
|
|
private void btnLogin_Click(object sender, EventArgs e)
|
|
{
|
|
Login();
|
|
}
|
|
|
|
private void btnExit_Click(object sender, EventArgs e)
|
|
{
|
|
if (DialogResult.OK == MessageBox.Show("AIParking - Bạn muốn thoát ứng dụng?", "Cảnh báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
|
|
{
|
|
Application.Exit();
|
|
}
|
|
}
|
|
}
|
|
}
|