📄 adminlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Xml;
using System.Data;
using System.Net;
using System.Diagnostics;
namespace XFMAS
{
/// <summary>
/// AdminLogin 的摘要说明。
/// </summary>
public class AdminLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox pwd;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button b222;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox user;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public AdminLogin()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//配置文件路径
Public.configFile = Application.StartupPath + "\\" + Public.configFile;
this.ReadConfigXml();
//检查软件更新
this.timer1.Start();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AdminLogin));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.user = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.pwd = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.b222 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.user);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.pwd);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 80);
this.groupBox1.TabIndex = 9;
this.groupBox1.TabStop = false;
//
// user
//
this.user.Location = new System.Drawing.Point(104, 16);
this.user.Name = "user";
this.user.Size = new System.Drawing.Size(152, 21);
this.user.TabIndex = 1;
this.user.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 2;
this.label1.Text = "内网用户名";
//
// pwd
//
this.pwd.Location = new System.Drawing.Point(104, 40);
this.pwd.Name = "pwd";
this.pwd.PasswordChar = '*';
this.pwd.Size = new System.Drawing.Size(152, 21);
this.pwd.TabIndex = 2;
this.pwd.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 3;
this.label2.Text = "内网登录密码";
//
// b222
//
this.b222.Location = new System.Drawing.Point(168, 96);
this.b222.Name = "b222";
this.b222.Size = new System.Drawing.Size(112, 23);
this.b222.TabIndex = 3;
this.b222.Text = "登录管理";
this.b222.Click += new System.EventHandler(this.b222_Click_1);
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Location = new System.Drawing.Point(8, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 23);
this.button2.TabIndex = 10;
this.button2.Text = "服务器设置";
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// timer1
//
this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// AdminLogin
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(298, 135);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.b222);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "AdminLogin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "管理员登录";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AdminLogin_KeyDown);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 设置参数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) {
SetWin sw = new SetWin();
sw.ShowDialog();
}
/// <summary>
/// 使用本机IP地址登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e) {
RegisterIP rip = new RegisterIP();
rip.ShowDialog();
this.LoadUserName();
}
/// <summary>
/// 使用用户名登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void b222_Click(object sender, System.EventArgs e) {
bool loginResult = DBConn.UserIsRight(this.user.Text.Trim(),Public.AddSec(this.pwd.Text));
if(loginResult){
//取值
Public.currentUserID = this.user.Text.Trim();
Public.currentUserName = DBConn.GetUserRealName(Public.currentUserID);
//记录到本地配置文件
Public.configXml.Rows[0]["LastUserID"] = this.user.Text.Trim();
Public.configXml.Rows[0]["LastUserPwd"] = Public.Encode("ESST_PWD",this.pwd.Text);
//保存到XML
SRXML.SaveDataTableToXML(Public.configXml,Public.configFile);
try{
//取出提醒时间段
DataTable alertTimes = DBConn.GetAlertTime();
if(!DBConn.CheckUserIsAdmin(this.user.Text.Trim())){
throw new Exception("非系统管理员,不能登录!");
}else{
Public.currentRCS = DBConn.GetAdminRuleCode(this.user.Text.Trim());
if(Public.currentRCS == null){
throw new Exception("虽为管理员身份,但没有被赋于任何权限,不能登入!");
}
}
string today = DBConn.GetServerTime().Date.ToString().Replace("0:00:00","");
Public.startTime = Convert.ToDateTime(today + alertTimes.Rows[0]["StartTime"].ToString());
Public.endTime = Convert.ToDateTime(today + alertTimes.Rows[0]["EndTime"].ToString());
if(alertTimes.Rows[0]["isMod"].ToString() == "1"){
Public.alertingIsMod = true;
}else{
Public.alertingIsMod = false;
}
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
this.Visible = false;
XFMAS.Admin.MainForm mf = new XFMAS.Admin.MainForm();
mf.Show();
}else{
MessageBox.Show("用户名或密码不正确,不能登录服务器!");
return;
}
}
private void button2_Click(object sender, System.EventArgs e) {
linkLabel1_LinkClicked(null,null);
}
#region 读取xml文件里的配置
/// <summary>
/// 读取磁盘中事先设置好的配置参数
/// 主要是数据库和自动登录的配置
/// </summary>
public void ReadConfigXml(){
bool isSetWin = false;
//检查System.Config 是否存在
if(!File.Exists(Public.configFile)){
isSetWin = true;
}else{
Public.configXml = SRXML.ConfigTable();
}
if(!isSetWin){
//读取配置
try{
Public.configXml = SRXML.ReadDataTaBleFromXML(Public.configFile);
}catch(Exception ex){
isSetWin = true;
}
//验证连接串
if(!isSetWin){
try{
Public.Conn(Public.configXml.Rows[0]["SqlServer"].ToString(),Public.configXml.Rows[0]["SqlUser"].ToString(),Public.DeCode("ESST_PWD",Public.configXml.Rows[0]["SqlPwd"].ToString()));
//记住上次输入的登录信息
this.user.Text = Public.configXml.Rows[0]["LastUserID"].ToString();
//从服务器端获取系统相关配置参数,如授权文件、用户表连接串等
try{
DataSet ds = DBConn.GetSystemInfo();
DataRow row = ds.Tables[0].Rows[0];
//用户表数据库的配置
Public.usersDbInfo = row["UsersSqlServer"].ToString();
//验证用户数据库登录信息
string usersDBconn = DBConn.CheckUsersDBConn();
//如果有异常则抛出
if(usersDBconn != "OK"){
throw new Exception(usersDBconn);
}
//系统名称
Public.systemName = row["systemName"].ToString();
}catch(Exception ex){
MessageBox.Show("从服务器端读取系统配置时出错:" + ex.Message.ToString());
return;
}
}catch(Exception ex){
isSetWin = true;
MessageBox.Show(ex.Message.ToString());
}
}
}
if(isSetWin){
if(MessageBox.Show("本地配置文件不存在或已失效,请重新配置服务器连接的参数!") == DialogResult.OK){
SetWin sw = new SetWin();
sw.ShowDialog();
this.ReadConfigXml();
}
}else{
}
}
/// <summary>
/// 根据IP取出绑定的用户名
/// </summary>
private void LoadUserName(){
//
}
#endregion
/// <summary>
/// 载入配置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
//this.timer1.Stop();
//this.ReadConfigXml();
}
/// <summary>
/// 变更了IP后
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void IP_TextChanged(object sender, System.EventArgs e) {
this.LoadUserName();
}
private void IP_Leave(object sender, System.EventArgs e) {
this.IP_TextChanged(sender,e);
}
/// <summary>
/// 不能关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
if(e.Clicks == 2) this.Visible = true;
}
private void LoginApp_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
this.Visible = false;
}
private void menuItem1_Click(object sender, System.EventArgs e) {
AboutUs au = new AboutUs();
au.ShowDialog();
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void b222_Click_1(object sender, System.EventArgs e) {
this.b222_Click(sender,e);
}
private void button2_Click_1(object sender, System.EventArgs e) {
SetWin sw = new SetWin();
sw.SetCheckBoxState(false);
sw.ShowDialog();
this.ReadConfigXml();
}
private void AdminLogin_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if(e.KeyCode == System.Windows.Forms.Keys.Enter){
b222_Click_1(null,null);
}
}
/// <summary>
/// 是否新版本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckUpdate(){
String temp = "";
try{
WebRequest Wrequest = WebRequest.Create("http://10.227.64.92/AMS/AdminUpdate.htm");
WebResponse Wresponse = Wrequest.GetResponse();
Stream Sm = Wresponse.GetResponseStream();
StreamReader Sr = new StreamReader(Sm,Encoding.GetEncoding("GB2312"));
temp = Sr.ReadToEnd().ToString();
Sr.Close();
}catch(Exception ex){
MessageBox.Show("升级检查失败:" + ex.Message.ToString());
return;
}
int oneI = temp.IndexOf("]");
string softVer = temp.Substring(1,oneI-1).ToString();
try{
//if(Public.configXml.Rows[0]["SoftVer"].ToString() != softVer && MessageBox.Show("系统检查到有升级文件可供更新,确定升级吗?","升级确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK){
if(Public.configXml.Rows[0]["SoftVer"].ToString() != softVer){
Public.configXml.Rows[0]["SoftVer"] = softVer;
//保存到XML
SRXML.SaveDataTableToXML(Public.configXml,Public.configFile);
System.Diagnostics.Process p = new Process();
p.StartInfo.FileName = Application.StartupPath + "\\UpdateEr.exe";
p.Start();
Application.Exit();
}
}catch(Exception ex){
MessageBox.Show("开始升级时出错:" + ex.Message.ToString());
}
}
/// <summary>
/// 检查升级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, System.EventArgs e) {
this.timer1.Stop();
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.CheckUpdate));
thread.IsBackground = true;
thread.Start();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -