Пример #1
0
        /// <summary>
        /// 添加 BDO 客户端日志
        /// </summary>
        /// <param name="logs"></param>
        /// <returns></returns>
        public static bool AddBDOClientLogs(HrmResource loginUser, string userName, string slKeyCode, string itMark, string logType, string logData)
        {
            try
            {
                ClientLogsV2 clientLogsV2 = new ClientLogsV2();
                clientLogsV2.company    = !string.IsNullOrEmpty(loginUser.subcompanyname) ? loginUser.subcompanyname : "";
                clientLogsV2.department = !string.IsNullOrEmpty(loginUser.departmentname) ? loginUser.departmentname : "";
                //clientLogsV2.userName = !string.IsNullOrEmpty(userName) ? userName : "";
                //clientLogsV2.loginId = !string.IsNullOrEmpty(loginUser.userId) ? loginUser.userId : "";
                clientLogsV2.userName   = !string.IsNullOrEmpty(loginUser.name) ? loginUser.name : "";
                clientLogsV2.loginId    = !string.IsNullOrEmpty(loginUser.loginid) ? loginUser.loginid : "";
                clientLogsV2.userSlCode = !string.IsNullOrEmpty(slKeyCode) ? slKeyCode : "";
                clientLogsV2.itMark     = !string.IsNullOrEmpty(itMark) ? itMark : "";

                HardwareHelper hardwareInfo = HardwareHelper.Instance();
                clientLogsV2.pcName       = hardwareInfo.ComputerName;
                clientLogsV2.cpuId        = hardwareInfo.CpuID;
                clientLogsV2.cpuName      = hardwareInfo.CpuName;
                clientLogsV2.diskName     = hardwareInfo.DiskID;
                clientLogsV2.mcAddress    = hardwareInfo.MacAddress;
                clientLogsV2.ip           = hardwareInfo.IpAddress;
                clientLogsV2.ram          = Math.Ceiling(Convert.ToDecimal(hardwareInfo.TotalPhysicalMemory) / 1024 / 1024 / 1024).ToString() + "G";
                clientLogsV2.logDate      = DateTime.Now.ToString("yyyy-MM-dd");
                clientLogsV2.logTime      = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                clientLogsV2.logType      = logType;            //TODO: ....
                clientLogsV2.logTypeValue = "1";                //TODO: ....
                clientLogsV2.logData      = logData;            //

                string           bdoClientLogURL = GetEoopAppSettings("EOOP_BDO_CLIENT_LOG");
                string           requesstBody    = JsonConvert.SerializeObject(clientLogsV2);
                string           result          = POSTEoopHttpRequest(bdoClientLogURL, requesstBody);
                BaseEoopResponse resultResponse  = (BaseEoopResponse)JsonConvert.DeserializeObject(result, typeof(BaseEoopResponse));
                return(resultResponse.ok);
            }
            catch (Exception ex)
            {
                //Exception clientEx = new Exception("添加客户端日志失败:" + ex.Message, ex);
                //throw clientEx;
                return(false);
            }
        }
        /// <summary>
        /// 登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            #region 检测网络连接是否正常

            if (!CommonService.IsConnectInternet())
            {
                string productName = ResourceCulture.GetString("AppName");  //Application.ProductName
                string exMsg       = string.Format(ResourceCulture.GetString("GeneralMsg_NetworkOffline"), Application.ProductName);
                MessageBox.Show(exMsg, productName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            #endregion

            //string userName = this.cbxUserName.Text.Trim();
            string userName = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Text.Trim();

            if (string.IsNullOrWhiteSpace(userName))
            {
                MessageBox.Show(ResourceCulture.GetString("GeneralMsg_EmptyUser"), ResourceCulture.GetString("GeneralTitle_Prompt"));
                return;
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show(ResourceCulture.GetString("GeneralMsg_EmptyPwd"), ResourceCulture.GetString("GeneralTitle_Prompt"));
                return;
            }

            bool   loginResult  = false;        //登录结果
            string loginUserId  = string.Empty; //登录用户对应 OA 系统用户 id
            string loginMessage = string.Empty; //登录结果提示信息

            try
            {
                #region LDAP 登录&OA验证&初始密码验证

                loginResult = EoopService.LoginVerify(userName, password, out loginUserId, out loginMessage);

                if (!loginResult)
                {
                    MessageBox.Show(loginMessage, ResourceCulture.GetString("GeneralTitle_Prompt"));
                    return;
                }

                if (loginResult && !string.IsNullOrEmpty(loginMessage) && loginMessage.Trim() == "1")//默认密码登录,强制更新密码
                {
                    this.Hide();
                    ChangePassword changePwdForm = new ChangePassword(DefaultLanguage, userName, password, true);
                    changePwdForm.ShowDialog();
                    return;
                }

                #endregion

                #region OA 系统验证登录用户信息

                List <HrmResource> loginHrmUsers = EoopService.GetHrmResource(userName);
                if (loginHrmUsers.Count != 1)//"AD 人事系统登录用户({userName}不存在/不唯一";
                {
                    string showMsg = loginHrmUsers.Count == 0 ? string.Format(ResourceCulture.GetString("LoginMsg_NotExistUser"), userName)
                                                              : string.Format(ResourceCulture.GetString("LoginMsg_NotUniqueUser"), userName);
                    MessageBox.Show(showMsg, ResourceCulture.GetString("GeneralTitle_Prompt"));
                    return;
                }
                LoginHrmUser = loginHrmUsers[0];

                #endregion

                #region OA 系统验证资产用户信息

                //string loginId = CurrentAsset != null ? CurrentAsset.loginid : "";
                //List<HrmResource> thisHrmUsers = EoopService.GetHrmResource(loginId);
                //if (thisHrmUsers.Count != 1)//"AD 人事系统登录用户({userName}不存在/不唯一";
                //{
                //    string showMsg = thisHrmUsers.Count == 0 ? string.Format(ResourceCulture.GetString("LoginMsg_NotExistAssetUser"), userName) : string.Format(ResourceCulture.GetString("LoginMsg_NotUniqueAssetUser"), userName);
                //    MessageBox.Show(showMsg, ResourceCulture.GetString("GeneralTitle_Prompt"));
                //    return;
                //}
                //CurrentAssetHrmUser = thisHrmUsers.Count > 0 ? thisHrmUsers[0] : new HrmResource();

                #endregion

                #region 判断用户是否可使用该资产登录

                if (CurrentAsset.tinyintfield2.Trim() != "1")//非公共资产
                {
                    List <Property> loginUserPropertyList = EoopService.GetUserAssetList(userName);
                    if (loginUserPropertyList == null || loginUserPropertyList.Where(p => p.id == CurrentAsset.id).Count() == 0)//非为资产本人登录
                    {
                        //同一用户组判断
                        var groupMembers = EoopService.GetGroupMembers(CurrentAssetHrmUser.userId, LoginHrmUser.loginid);
                        var departRoles  = EoopService.GetDepartRoleMapping(LoginHrmUser.departmentid);
                        if (groupMembers.Count == 0 && departRoles.Count == 0)//非资产所属用户组用户登录
                        {
                            MessageBox.Show(ResourceCulture.GetString("LoginMsg_NotSameGroup"), ResourceCulture.GetString("LoginMsg_Title"));
                            return;
                        }
                    }
                }

                #endregion

                #region 记住用户 & 记住密码

                #region 注释代码:记住多个用户

                //List<LogUser> userList = new List<LogUser>();

                //using (FileStream fStream = new FileStream("data.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                //{
                //    BinaryFormatter bFormat = new BinaryFormatter();
                //    List<LogUser> users = fStream.Length > 0 ? bFormat.Deserialize(fStream) as List<LogUser> : new List<LogUser>();
                //    if (chxUser.Checked)
                //    {
                //        LogUser user = new LogUser();
                //        user.Username = userName;
                //        user.Password = chxPassword.Checked ? password : "";        //如果单击了记住密码的功能则在文件中保存密码,否则不在文件中保存密码
                //        var containsUsers = users.Where(u => u.Username == userName);
                //        if (containsUsers != null && containsUsers.Count() > 0)   //选在集合中是否存在用户名
                //        {
                //            int index = users.IndexOf(containsUsers.SingleOrDefault());
                //            users.RemoveAt(index);
                //        }
                //        users.Add(user);
                //    }
                //    else
                //    {
                //        var containsUsers = users.Where(u => u.Username == userName);
                //        if (containsUsers != null && containsUsers.Count() > 0)          //选在集合中是否存在用户名
                //        {
                //            int index = users.IndexOf(containsUsers.SingleOrDefault());
                //            users.RemoveAt(index);
                //        }
                //    }
                //    userList = users;
                //}

                //using (FileStream fStream = new FileStream("data.bin", FileMode.Open, FileAccess.ReadWrite))
                //{
                //    BinaryFormatter bFormat = new BinaryFormatter();
                //    bFormat.Serialize(fStream, userList);               //要先将User类先设为可以序列化(即在类的前面加[Serializable])
                //}

                #endregion

                #region 记录最近一次登录用户

                /*using (FileStream fStream = new FileStream(Application.StartupPath + @"\data.bin", FileMode.Create))
                 * {
                 *  BinaryFormatter bFormat = new BinaryFormatter();
                 *  List<LogUser> users = fStream.Length > 0 ? bFormat.Deserialize(fStream) as List<LogUser> : new List<LogUser>();
                 *  if (chxUser.Checked)
                 *  {
                 *      LogUser user = new LogUser();
                 *      user.Username = userName;
                 *      user.Password = chxPassword.Checked ? password : "";        //如果单击了记住密码的功能则在文件中保存密码,否则不在文件中保存密码
                 *      var containsUsers = users.Where(u => u.Username == userName);
                 *      if (containsUsers != null && containsUsers.Count() > 0)   //选在集合中是否存在用户名
                 *      {
                 *          int index = users.IndexOf(containsUsers.SingleOrDefault());
                 *          users.RemoveAt(index);
                 *      }
                 *      users.Add(user);
                 *  }
                 *  else
                 *  {
                 *      var containsUsers = users.Where(u => u.Username == userName);
                 *      if (containsUsers != null && containsUsers.Count() > 0)          //选在集合中是否存在用户名
                 *      {
                 *          int index = users.IndexOf(containsUsers.SingleOrDefault());
                 *          users.RemoveAt(index);
                 *      }
                 *  }
                 *
                 *  bFormat.Serialize(fStream, users);               //要先将User类先设为可以序列化(即在类的前面加[Serializable])
                 * }*/

                if (chxUser.Checked)
                {
                    Properties.Settings.Default.UserName = userName;
                    Properties.Settings.Default.Password = password;
                }
                else
                {
                    Properties.Settings.Default.UserName = "";
                    Properties.Settings.Default.Password = "";
                }
                Properties.Settings.Default.Save();

                #endregion

                #endregion

                #region 登录,进入程序主界面 MainForm

                //if (!Cef.IsInitialized)
                //{
                //    Cef.RunMessageLoop();
                //    Cef.Shutdown();
                //}

                MainForm mainForm = new MainForm(loginUserId, userName, password, DefaultLanguage, CurrentSLCode, CurrentSLCodeList, CurrentAsset, LoginHrmUser);
                this.Hide();
                mainForm.WindowState = FormWindowState.Maximized;
                mainForm.Show();

                //初始化 CefSharp 浏览器内核组件
                InitializeCefSharp(DefaultLanguage);

                txtPassword.Text    = string.Empty;
                chxUser.Checked     = false;
                chxPassword.Checked = false;

                #endregion
            }
            catch (Exception ex)
            {
                string exMessage = ResourceCulture.GetString("GeneralMsg_AppException");
                if (Program.ReleaseType == "DEBUG")
                {
                    exMessage = ex.Message + ex.StackTrace;
                }
                MessageBox.Show(exMessage, ResourceCulture.GetString("LoginMsg_Title"));
            }
        }
        /// <summary>
        /// 窗体初始化
        /// </summary>
        public Login()
        {
            try
            {
                InitializeComponent();

                //初始化窗体信息
                InitializeFormInfo();

                #region 验证:锁号 & 资产 & 资产绑定

                List <string> slCodeList = new List <string>();

                #region 获取资产软锁锁号信息

                try
                {
                    slCodeList = (new safeNetBiz()).getSlCode();
                }
                catch
                {
                }

                #endregion

#if DEBUG
                //slCodeList.Clear();
                //slCodeList.Add("256451793977733503");
#endif

                if (slCodeList.Count == 0)//未生成锁号,需进入资产绑定授权界面
                {
                    GotoBindAsset();
                    return;
                }

                List <UserSLCodes> userSlCodeList = new List <UserSLCodes>();
                foreach (var slcode in slCodeList)
                {
                    UserSLCodes userSLCodes = EoopService.GetUserSLCodes(slcode);
                    if (userSLCodes != null)
                    {
                        userSlCodeList.Add(userSLCodes);
                    }
                }

                //已生成锁号,未绑定资产(锁号未启用,请联系技术部)
                if (userSlCodeList.Count == 0)
                {
                    MessageBox.Show(ResourceCulture.GetString("LoginMsg_Disable"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FormDisabled();
                    return;
                }

                //检测到软锁已绑定多台资产,请联系信息技术部。
                if (userSlCodeList.Where(u => u.propertyId == userSlCodeList[0].propertyId).Count() != userSlCodeList.Count)
                {
                    MessageBox.Show(ResourceCulture.GetString("LoginMsg_MultipleAsset"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FormDisabled();
                    return;
                }

                string propertyId = userSlCodeList[0].propertyId;
                string slCode     = userSlCodeList[0].slCode;

                Property currentAsset = EoopService.GetUserAsset(propertyId);
                if (currentAsset == null)
                {
                    //锁号未启用,请联系信息技术部。
                    MessageBox.Show(ResourceCulture.GetString("LoginMsg_Disable"), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    FormDisabled();
                    return;
                }

                #endregion

                CurrentSLCodeList = slCodeList;
                CurrentAsset      = currentAsset;
                CurrentSLCode     = slCodeList[0];

                string             loginId      = CurrentAsset != null ? CurrentAsset.loginid : "";
                List <HrmResource> thisHrmUsers = EoopService.GetHrmResource(loginId);
                if (thisHrmUsers.Count != 1)//AD 人事系统登录用户不存在/不唯一
                {
                    string showMsg = thisHrmUsers.Count == 0 ? ResourceCulture.GetString("LoginMsg_NotExistAssetUser")
                                                             : ResourceCulture.GetString("LoginMsg_NotUniqueAssetUser");
                    MessageBox.Show(showMsg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ApplicationExit();
                    return;
                }

                CurrentAssetHrmUser = thisHrmUsers.Count > 0 ? thisHrmUsers[0] : new HrmResource();

                #region 读取配置文件: 绑定记住的用户名和密码

                if (cbxUserName.Items.Count > 0)
                {
                    cbxUserName.Items.Clear();
                }

                //数据序列化异常,更换存储方式 neo 2017年4月19日9:55:43

                /*using (FileStream fStream = new FileStream(Application.StartupPath + @"\data.bin", FileMode.OpenOrCreate))
                 * {
                 *  if (fStream.Length > 0)
                 *  {
                 *      BinaryFormatter bf = new BinaryFormatter();
                 *      List<LogUser> users = bf.Deserialize(fStream) as List<LogUser>;
                 *
                 #region 下拉框 - 记住密码
                 *
                 *      //foreach (LogUser user in users)
                 *      //{
                 *      //    this.cbxUserName.Items.Add(user.Username);
                 *      //}
                 *      //if (!string.IsNullOrWhiteSpace(cbxUserName.Text))
                 *      //{
                 *      //    var user = users.Where(u => u.Username == cbxUserName.Text).SingleOrDefault();
                 *      //    if (user != null)
                 *      //    {
                 *      //        this.txtPassword.Text = user.Password;
                 *      //        this.chxUser.Checked = true;
                 *      //        this.chxPassword.Checked = !string.IsNullOrEmpty(user.Password);
                 *      //    }
                 *      //}
                 *
                 #endregion
                 *
                 #region 输入框 - 记住密码
                 *
                 *      foreach (LogUser user in users)
                 *      {
                 *          this.txtUserName.Text = !string.IsNullOrWhiteSpace(user.Username) ? user.Username : string.Empty;
                 *          this.txtPassword.Text = !string.IsNullOrEmpty(user.Password) ? user.Password : string.Empty;
                 *          this.chxUser.Checked = !string.IsNullOrWhiteSpace(user.Username);
                 *          this.chxPassword.Checked = !string.IsNullOrEmpty(user.Password);
                 *      }
                 *
                 #endregion
                 *  }
                 * }*/


                string username = Properties.Settings.Default.UserName;
                string password = Properties.Settings.Default.Password;
                this.txtUserName.Text    = !string.IsNullOrWhiteSpace(username) ? username : string.Empty;
                this.txtPassword.Text    = !string.IsNullOrEmpty(password) ? password : string.Empty;
                this.chxUser.Checked     = !string.IsNullOrWhiteSpace(username);
                this.chxPassword.Checked = !string.IsNullOrEmpty(password);

                #region 设置默认(第一个)登录用户

                //if (this.cbxUserName.Items.Count > 0)
                //{
                //    this.cbxUserName.SelectedIndex = this.cbxUserName.Items.Count - 1;
                //}

                #endregion

                #endregion
            }
            catch (Exception ex)
            {
                string exMessage = ResourceCulture.GetString("GeneralMsg_AppException");
                if (Program.ReleaseType == "DEBUG")
                {
                    exMessage = ex.Message + ex.StackTrace;
                }
                MessageBox.Show(exMessage, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                FormDisabled();
            }
        }