DMSSSOLogin.cs 11.7 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.IO;
using DMSSSOUpgrade;

namespace DMSSSOAgent
{


    public partial class DMSSSOLogin : Form
    {
        #region 变量定义
        public string P_UserName = "";
        public string P_Password = "";
        public string P_UserID = "";
        public string P_UName = "";
        private Point downPoint;
        #endregion

        #region 构造函数
        public DMSSSOLogin()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
        }
        #endregion

        #region Functions
        #region Form Load
        private void DMSSSOLogin_Load(object sender, EventArgs e)
        {
            try
            {
                //判断是否有新版本
                DMSSSOService.SSOServices SSOServices = new global::DMSSSOAgent.DMSSSOService.SSOServices();
                object[] oSysList = SSOServices.getUpdateInfor();
                if (((System.Xml.XmlNode[])(oSysList[0]))[0].Value != ConfigurationSettings.AppSettings["VersionNo"].ToString())
                {
                    //MessageBox.Show(((System.Xml.XmlNode[])(oSysList[0]))[0].Value + " versionNo" + ConfigurationSettings.AppSettings["VersionNo"].ToString());
                    //判断是否必须更新版本  1→是 0→否
                    if (((System.Xml.XmlNode[])(oSysList[4]))[0].Value == "1")
                    {
                        MessageBox.Show("有新版本必须更新!");
                        Application.Exit();
                        Process.Start("DMSSSOUpgrade.exe", Application.StartupPath + @"\DMSSSOUpgrade.exe");
                    }
                    else
                    {
                        if (MessageBox.Show("有新版本可更新,您是否更新!", "确认更新?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            Application.Exit();
                            //Process.Start("DMSSSOUpgrade.exe", @"D:\Project\TNT\P-DMS SSO\Code\DMSSSO\DMSSSOUpgrade\bin\Debug\\DMSSSOUpgrade.exe");
                            Process.Start("DMSSSOUpgrade.exe",Application.StartupPath+ @"\DMSSSOUpgrade.exe"); 
                        }
                    }
                }
                GetDDLLanguageText();
                ApplyResource();
                string sProcessesName = ConfigurationSettings.AppSettings["ProcessesName"].ToString().Trim();
                if (Process.GetProcessesByName(sProcessesName).Length > 1)
                {
                    MessageBox.Show("The process is still live.");
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region 记录日志

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public void WriteLog()
        {
            try
            {
                string filePath = Application.StartupPath + @"\IMG\";
                string FileName = filePath + "SysLog.Log";
                if (!File.Exists(FileName))
                {
                    File.Create(FileName);
                }
                System.IO.StreamWriter sw = System.IO.File.AppendText(FileName);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") +" Button X:"+ btnLogin.Location.X.ToString()+"Y:"+btnLogin.Location.Y.ToString());
                sw.Close();
            }
            catch(Exception ex)
            {
                throw (ex);
            }
        }
        #endregion
        

        #region Login
        private void Login()
        {
            if (comBoxSelectLanguage.Text == "-Select Language-")
            {
                MessageBox.Show("Please Select Language");
                return;
            }
            string username = "";
            username = txtUserName.Text;
            string password = "";
            password = txtPassword.Text;

            if (username == "" || password == "") return;
            try
            {
                DMSSSOService.SSOServices SSOservice = new global::DMSSSOAgent.DMSSSOService.SSOServices();
                
                object[] oUserInfo = SSOservice.validateUser(username, password);
                if (oUserInfo == null) return;
                if (((System.Xml.XmlNode[])(oUserInfo[0]))[0].Value == "SUCCESS")
                {
                    P_UserID = ((System.Xml.XmlNode[])(oUserInfo[1]))[0].Value;
                    P_UserName = ((System.Xml.XmlNode[])(oUserInfo[2]))[0].Value;
                    P_Password = ((System.Xml.XmlNode[])(oUserInfo[3]))[0].Value;
                    P_UName = ((System.Xml.XmlNode[])(oUserInfo[4]))[0].Value;
                    ParameterSettings.UserID = ((System.Xml.XmlNode[])(oUserInfo[1]))[0].Value;
                    ParameterSettings.UserName = ((System.Xml.XmlNode[])(oUserInfo[2]))[0].Value;
                    ParameterSettings.Password = ((System.Xml.XmlNode[])(oUserInfo[3]))[0].Value;
                    ParameterSettings.Name = ((System.Xml.XmlNode[])(oUserInfo[4]))[0].Value;
                   
                    if (ParameterSettings.IsReLogin == false)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.Hide();
                        ParameterSettings.IsReLogin = true;
                        DMSSSOAgent fm = new DMSSSOAgent();
                        fm.ShowDialog();
                        this.Close();
                        this.Dispose();
                    }
                }
                else if (((System.Xml.XmlNode[])(oUserInfo[0]))[0].Value == "AD_CONNECTION_FALIED")
                {
                    MessageBox.Show("连接AD服务器错误,请联系Helpdesk");
                }
                else if (((System.Xml.XmlNode[])(oUserInfo[0]))[0].Value == "AD_VALIDATE_FALIED")
                {
                    MessageBox.Show("请使用正确的TNT Global Link ID和密码登录(同Windows登录),如需帮助,请联系Helpdesk");
                }
                else if (((System.Xml.XmlNode[])(oUserInfo[0]))[0].Value == "VALIDATE_FALIED")
                {
                    MessageBox.Show("此账号无DMS权限,请联系Helpdesk");
                }
                else
                {
                    MessageBox.Show("Login Failed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check the network.");
            }

        }
        #endregion

        #region System Check
        private bool SystemCheck()
        {
            bool returnvalue = false;



            return returnvalue;

        }
        private bool ConfigCheck()
        {
            bool returnvalue = false;



            return returnvalue;

        }
        private bool RCDirectoryCheck(ref string errormsg)
        {
            bool returnvalue = false;




            return returnvalue;
        }
        private bool RCFileTypeCheck(ref string errormsg)
        {
            bool returnvalue = false;




            return returnvalue;
        }
        #endregion

        #region Apply Resource
        /// <summary>
        /// 应用资源
        /// ApplyResources 的第一个参数为要设置的控件
        ///                  第二个参数为在资源文件中的ID,默认为控件的名称
        /// </summary>
        private void ApplyResource()
        {
            //获取语言
            string language = ConfigurationSettings.AppSettings["language"].ToString().Trim();
            if (language == "Default")
            {
                ConfigurationSettings.AppSettings["language"] = "en";
            }
            //更改当前线程的 CultureInfo
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ConfigurationSettings.AppSettings["language"].ToString().Trim());
           
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(DMSSSOLogin));
            res.ApplyResources(lblUserName, "lblUserName");
            res.ApplyResources(lblPassword, "lblPassword");
            res.ApplyResources(btnLogin, "btnLogin");
            res.ApplyResources(lblLanuage, "lblLanuage");

            res.ApplyResources(this, "$this");
        }
        #endregion

        #endregion

        #region Button Events
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void btnMini_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }
        #endregion

        #region Mouse Event
        private void DMSSSOLogin_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }

        private void DMSSSOLogin_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = new Point(e.X, e.Y);
        }
        #endregion

        #region Key Event
        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLower(e.KeyChar))
            {
                txtUserName.SelectedText = Char.ToUpper(e.KeyChar).ToString();
                e.Handled = true;
            }
        }
        #endregion

        #region Languange Event

        /// <summary>
        /// 获取Lanuage
        /// </summary>
        public void GetDDLLanguageText()
        {
            if (ConfigurationSettings.AppSettings["language"] == "en")
            {
                comBoxSelectLanguage.Text = "English";
            }
            else if (ConfigurationSettings.AppSettings["language"] == "zh-CHS")
            {
                comBoxSelectLanguage.Text = "简体中文";

            }
            else if (ConfigurationSettings.AppSettings["language"] == "zh-CHT")
            {
                comBoxSelectLanguage.Text = "繁体中文";
            }
            else
            {
                comBoxSelectLanguage.Text = "-Select Language-";
            }
        }
        /// <summary>
        /// Language 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void comBoxSelectLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = comBoxSelectLanguage.Text;
            if (s == "English")
            {
                ConfigurationSettings.AppSettings["language"] = "en";

            }
            else if (s == "简体中文")
            {
                ConfigurationSettings.AppSettings["language"] = "zh-CHS";

            }
            else if (s == "繁体中文")
            {
                ConfigurationSettings.AppSettings["language"] = "zh-CHT";

            }
            else
            {
                MessageBox.Show("Please Select Language");
            }
            ApplyResource();
        }

        #endregion
    }
}