njcit/NJCIT/LoginForm.cs

257 lines
7.9 KiB
C#

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace NJCIT
{
public partial class LoginForm : Form
{
private static bool isAutoLogin = false;
private static bool isRemember = true;
private static string path = "./config.ini";
private static int domain;
private static string userName;
private static string password;
private Form mainForm;
public LoginForm(Form form)
{
this.mainForm = form;
InitializeComponent();
}
// 窗口载入时执行的操作
private void LoginForm_Load(object sender, EventArgs e)
{
ReadConfig();
// 检查用户在线状态
if (!LoginServer.Logouted)
{
if (LoginServer.Online)
{
//MessageBox.Show("用户已登录", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ChangeForm();
}
else if(isAutoLogin)
{
this.AutoLogin();
}
}
}
// 用户名
private void TextBox_UserName_TextChanged(object sender, EventArgs e)
{
userName = TextBox_Username.Text;
}
// 认证域
private void ComboBox_Domain_SelectedIndexChanged(object sender, EventArgs e)
{
domain = ComboBox_Domain.SelectedIndex; ;
}
// 密码
private void TextBox_Password_TextChanged(object sender, EventArgs e)
{
password = StringToBase64(TextBox_Password.Text);
}
// 自动登录按钮
private void CheckBox_AutoLogin_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox_AutoLogin.Checked)
{
CheckBox_Remember.Checked = true;
}
}
//保存密码按钮
private void CheckBox_Remember_CheckedChanged(object sender, EventArgs e)
{
if (!CheckBox_Remember.Checked)
{
CheckBox_AutoLogin.Checked = false;
}
}
// 登录按钮
private void Button_Login_Click(object sender, EventArgs e)
{
if (TextBox_Username.Text.Length == 0)
{
MessageBox.Show("用户名不能为空!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
TextBox_Username.Focus();
return;
}
if (TextBox_Password.Text.Length == 0)
{
MessageBox.Show("密码不能为空!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
TextBox_Password.Focus();
return;
}
this.AutoLogin();
}
private void TextBox_Username_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Button_Login.PerformClick();
}
}
private void TextBox_Password_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Button_Login.PerformClick();
}
}
private void CheckBox_AutoLogin_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Button_Login.PerformClick();
}
}
private void CheckBox_Remember_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Button_Login.PerformClick();
}
}
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!LoginServer.Online)
{
Environment.Exit(0);
}
}
// 登录操作
private void AutoLogin()
{
LoginServer.DoLogin(userName, domain, password);
if (LoginServer.Online)
{
this.ChangeForm();
WriteConfig();
}
else
{
ArrayList list = LoginServer.GetInformation();
if (list == null)
{
MessageBox.Show("登录失败,请检查您的互联网连接状态!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(list[0].ToString(), "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
// 切换窗体
private void ChangeForm()
{
this.mainForm.Show();
this.Close();
}
// 编码转换
private static string StringToBase64(string str)
{
byte[] bytes = Encoding.ASCII.GetBytes(str);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
// 编码转换
private static string Base64ToString(string str)
{
try
{
byte[] bytes = Convert.FromBase64String(str);
return Encoding.ASCII.GetString(bytes);
}
catch (Exception)
{
return "";
}
}
// 读取配置
private void ReadConfig()
{
// 读取配置
if (!File.Exists(path))
{
try
{
File.Create(path).Close();
}
catch (Exception)
{
}
}
isAutoLogin = OperateIniFile.ReadIniData("Settings", "AutoLogin", "0", path).Equals("1") ? true : false;
isRemember = OperateIniFile.ReadIniData("Settings", "SavePassword", "1", path).Equals("1") ? true : false;
try
{
domain = Int32.Parse(OperateIniFile.ReadIniData("Account", "Domain", "0", path));
}
catch (Exception)
{
domain = 0;
}
userName = OperateIniFile.ReadIniData("Account", "UserName", "", path);
password = OperateIniFile.ReadIniData("Account", "Password", "", path);
CheckBox_AutoLogin.Checked = isAutoLogin;
CheckBox_Remember.Checked = isRemember;
TextBox_Username.Text = userName;
ComboBox_Domain.SelectedIndex = domain;
TextBox_Password.Text = Base64ToString(password);
}
// 保存配置
private void WriteConfig()
{
isAutoLogin = CheckBox_AutoLogin.Checked;
isRemember = CheckBox_Remember.Checked;
userName = TextBox_Username.Text;
domain = ComboBox_Domain.SelectedIndex;
password = StringToBase64(TextBox_Password.Text);
OperateIniFile.WriteIniData("Settings", "AutoLogin", isAutoLogin ? "1" : "0", path);
OperateIniFile.WriteIniData("Settings", "SavePassword", isRemember ? "1" : "0", path);
OperateIniFile.WriteIniData("Account", "UserName", userName, path);
OperateIniFile.WriteIniData("Account", "Domain", domain.ToString(), path);
OperateIniFile.WriteIniData("Account", "Password", isRemember ? password : "", path);
}
private void toolStripButton_about_Click(object sender, EventArgs e)
{
new AboutForm().ShowDialog();
}
private void toolStripButton_help_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("help.html");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}