Files
lserp_cs_6.0/插件库/Lskj.Main/FrmSwap.cs
T

676 lines
27 KiB
C#

/******************************
* 说明:切换账户
* 创建人:龚宇超
* 创建日期:2017-08-14
* 修改人:
* 修改日期:
* 修改备注:
* 版本:1.0.0.0
******************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lskj.Control;
using Lskj.Business;
using Lskj.Model;
using Lskj.Main.Model;
using Lskj.Business.Impl;
using Lskj.Core;
using DevExpress.LookAndFeel;
using Lskj.Util;
using Lskj.Data;
using DevExpress.XtraEditors.Controls;
using Lskj.Control.Model;
namespace Lskj.Main
{
/// <summary>
/// 切换账户
/// </summary>
public partial class FrmSwap : BaseForm
{
/// <summary>
/// 记录窗口位置
/// </summary>
private DataTable _userTable;
private DataTable _ledgerTable;
/// <summary>
/// 记录初始套账信息
/// </summary>
private string _ledgerName;
private bool isLoginByAd = false;
public FrmSwap()
{
InitializeComponent();
//lueUserName.AutoShowPopup = false;
if (SystemInfo.Instance.AccountSwitchingRestrictions > 0 && SystemInfo.Instance.AccountSwitchingRestrictions < int.Parse(ERPInfo.Instance.UserId))
{
this.cbLedger.Enabled = false;
}
}
/// <summary>
/// <para>说明:帐套</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2019-11-11 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="typeName">Name of the type.</param>
private void StartLedger(string ledgerValue)
{
DataRow focusRow = _ledgerTable.Rows.Cast<DataRow>().FirstOrDefault(x => ledgerValue.Equals(x["ShowName"] + ""));
if (focusRow != null)
{
string serverIP = focusRow["IP"] + "";
string dbName = focusRow["DBName"] + "";
string dataBook = focusRow["ShowName"] + "";
if (string.IsNullOrWhiteSpace(serverIP) || string.IsNullOrWhiteSpace(dbName))
{
MessageUtil.Show(ResourceKeys.UnSetServerAddress);
return;
}
else
{
DBConfig.Instance.DataBook = dataBook;
DBConfig.Instance.ServerName = serverIP;
DBConfig.Instance.DataBase = dbName;
if (!DBConfig.Instance.CreateConnection())
{
MessageUtil.Show(ResourceKeys.ServerAddressFault);
return;
}
else
{
if (DelphiHelper.Delphi_Init(new StringBuilder(DBConfig.Instance.GetDelphiConnection())) == 0)
{
MessageUtil.Show(ResourceKeys.UnConnectServer + "[By Delphi]");
return;
}
ERPInfo.Instance.AccountBook = ResourceKeys.CurrentAccountBook + ledgerValue;
SystemInfo.RefreshSystemParam();
this.SetUsers();
}
}
}
}
/// <summary>
/// <para>说明:设置帐套信息</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-09 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void SetLedgers()
{
// 获取帐套信息
try
{
_ledgerTable = MainImpl.GetLedgerList();
cbLedger.Properties.Items.Clear();
if (_ledgerTable.Rows.Count > 0)
{
foreach (DataRow item in _ledgerTable.Rows)
{
cbLedger.Properties.Items.Add(item["ShowName"]);
}
if (!string.IsNullOrEmpty(DBConfig.Instance.DataBook))
{
DataRow ledRow = _ledgerTable.Rows.Cast<DataRow>().FirstOrDefault(x => DBConfig.Instance.DataBook.Equals(x["ShowName"] + ""));
cbLedger.Text = ledRow != null ? ledRow["ShowName"] + "" : _ledgerTable.Rows[0]["ShowName"] + "";
}
else
{
cbLedger.Text = _ledgerTable.Rows[0]["ShowName"] + "";
}
//this.StartLedger(cbLedger.EditValue + "");
}
else
{
cbLedger.Enabled = false;
}
_ledgerName = cbLedger.Text;
}
catch (Exception)
{
}
}
/// <summary>
/// <para>说明:设置系统用户</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-09 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
void SetUsers()
{
_userTable = MainImpl.GetEmployeeList();
lueUserName.AutoShowPopup = "1".Equals(BaseImpl.GetResult("SELECT LoginPopupFlag FROM P_SystemTab") + "");
//lueUserName.AutoShowPopup = true;
lueUserName.Focus();
lueUserName.ValueField = lueUserName.ValueMember = "UserId";
lueUserName.TextMember = "UserName";
lueUserName.SourceSQL = "select EmployeeId UserId,LoginAccount UserCode,EmployeeName UserName from P_EmployeeTab WHERE sign=0 and UseFlag=1";
lueUserName.DataSource = _userTable;
DataRow[] rowItems = _userTable.Select(" UserName = '" + DBConfig.Instance.LoginName + "'");
if (rowItems != null && rowItems.Length > 0)
{
if (ADUtil.ADLogin == null || ADUtil.ADLogin == false)//不是Ad域登录才赋值
{
lueUserName.EditValue = rowItems[0][lueUserName.ValueField] + "";
}
}
}
/// <summary>
/// <para>说明:设置delphi参数</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-03-12 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
void SetDelphiDllParams()
{
try
{
StringBuilder sb1 = new StringBuilder();
sb1.Append(ERPInfo.Instance.UserName);
StringBuilder sb2 = new StringBuilder();
sb2.Append(ERPInfo.Instance.WindowName);
StringBuilder sb3 = new StringBuilder();
sb3.Append(ERPInfo.Instance.SkinName);
//给delphi库设置参数(用户id、子系统id、操作员名称、计算机名称)
DelphiHelper.Delphi_SetParams(Convert.ToInt32(ERPInfo.Instance.UserId), Convert.ToInt32(ERPInfo.Instance.SubSysId), sb1, sb2, sb3);
}
catch (Exception ex)
{
MessageUtil.Show(ex);
LogHelper.Instance.WriteError(ex);
}
}
/// <summary>
/// 保存修改
/// </summary>
private void saveChange()
{
try
{
string userId = lueUserName.EditValue + "";
if (!string.IsNullOrEmpty(lueUserName.Text) && string.IsNullOrEmpty(lueUserName.EditValue))
{
lueUserName.EditValue = lueUserName.Text;
userId = lueUserName.EditValue + "";
}
string userName = lueUserName.Text.Trim();
string password = txtPassword.Text.Trim();
string userCode = string.Empty;
bool isConstraint = chkLogin.Checked;
string LoginAccount = string.Empty;
if (String.IsNullOrWhiteSpace(userName))
{
MessageUtil.Show(ResourceKeys.UserNameNotNull);
return;
}
DataTable table = MainImpl.GetEmployeeList();
#region Ad域判断打开
BaseResponse responseinfo = new BaseResponse();
int loginResult = -1;
bool isAd = false;
if (LoginByAD(userName, password, ref responseinfo))
{
DataTable dataTable = SqlHelper.ExecuteDataTable(string.Format("EXEC[P_Login_pr] '','','{0}','',5", userName));
if (dataTable.Rows.Count > 0)
{
userId = dataTable.Rows[0]["employeeid"] + "";
DataRow[] datas = table.Select("UserId=" + userId);//找到userId为当前的用户
if (datas.Count() > 0)
{
isAd = true;
DataRow data = datas[0];
userId = data["UserId"] + "";
userName = data["UserName"] + "";
LoginAccount = data["UserCode"] + "";
password = data["Password"] + "";
ERPInfo.Instance.LoginResult = true;
isLoginByAd = true;
loginResult = 0;
}
}
else
{
MessageUtil.Show("无对应的ad域关联数据");
return;
}
}
else
{
if (!string.IsNullOrWhiteSpace(userId) && !string.IsNullOrWhiteSpace(userName))
{
DataRow[] drRows = table.Select("UserName='" + userName + "' or UserCode='" + userName + "'");
if ((drRows == null) || (drRows.Count() < 1))
{
MessageUtil.Show(ResourceKeys.NameOrPasswordError);
return;
}
//找到多个操作员
if (drRows.Count() > 1)
{
MessageUtil.Show(ResourceKeys.DuplicatedNameError);
return;
}
DataRow drRow = drRows[0];
if (drRow != null)
{
userId = drRow["UserId"] + "";
userName = drRow["UserName"] + "";
userCode = drRow["UserCode"] + "";
}
else
{
userId = "";
}
if (string.IsNullOrWhiteSpace(userId))
{
MessageUtil.Show(ResourceKeys.UserNotFound);
return;
}
//权限
if (SystemInfo.Instance.SwitchPermission && !MainImpl.SwitchPermissions(userId, userName))
{
MessageUtil.Show("切换人未配置任何权限,切换失败");
return;
}
if (SystemInfo.Instance.IsEncrypt)
{
int[] keyHandles = ERPInfo.Instance.keyHandles = new int[8];
int[] keyNumber = ERPInfo.Instance.keyNumber = new int[8];
string appId = DBConfig.Instance.ServerName;
int frtn = SmartX1Api.SmartX1Find(appId, keyHandles, keyNumber);
int keyHandle = ERPInfo.Instance.keyHandles[0];
int uPin1 = Convert.ToInt32("0x987F6BCD", 16);
int uPin2 = Convert.ToInt32("0xE193C5B2", 16);
int uPin3 = Convert.ToInt32("0xD507CC28", 16);
int uPin4 = Convert.ToInt32("0x4B125AF6", 16);
int rtn = SmartX1Api.SmartX1Open(keyHandle, uPin1, uPin2, uPin3, uPin4);
if (rtn == 0)
{
//string writeKey = string.Format("{0}_{1}", DBConfig.Instance.ServerName, DBConfig.Instance.DataBase);
//int length = 256;
//byte[] readBuffer = new byte[length];
//int readRtn = SmartX1Api.SmartX1ReadPage(keyHandle, 0, 0, ref length, readBuffer);
//if (readRtn == 0)
//{
// Dictionary<string, string> readKeysValues = new Dictionary<string, string>();
// string readVerifyStr = Encoding.Default.GetString(readBuffer);
// string[] readVerifyStrArray = readVerifyStr.Replace("\0", "").Split(';');//获取多个账套设置
// foreach (string readVerify in readVerifyStrArray)
// {
// string[] readKeyValue = readVerify.Split('^');//获取单个账套
// if (readKeyValue.Length == 2)
// {
// readKeysValues.Add(readKeyValue[0], readKeyValue[1]);
// }
// }
// if (readKeysValues.ContainsKey(writeKey))//如果存在当前账套
// {
// string[] verifyArray = readKeysValues[writeKey].Split('^');
// if (!verifyArray.Contains(userId))
// {
// MessageUtil.Show("当前人员未拥有登录权限");
// return;
// }
// }
// else
// {
// MessageUtil.Show("当前人员未拥有登录权限");
// return;
// }
//}
//else
//{
// MessageUtil.Show("U盾数据获取失败,请重试或联系管理员");
// return;
//}
}
else
{
MessageUtil.Show("U盾数据获取失败,请重试或联系管理员");
return;
}
}
if (!MainImpl.CheckingMacAddress(userId))
{
MessageUtil.Show(ResourceKeys.MacAddressError);
return;
}
loginResult = MainImpl.Login(userId, password, isConstraint);
}
}
#endregion
//int loginResult = MainImpl.Login(userId, password, isConstraint);
switch (loginResult)
{
case 0: // 登录成功
// 设置皮肤
string skinName = MainImpl.GetUserSkin(userId);
skinName = string.IsNullOrEmpty(skinName) ? ERPInfo.Instance.SkinName : skinName;
UserLookAndFeel.Default.SetSkinStyle(skinName);
//设置登录人电话
string phone = MainImpl.GetUserPhone(userId);
ERPInfo.Instance.UserLinkPhone = phone;
ERPInfo.Instance.UserId = userId;
ERPInfo.Instance.UserName = userName;
ERPInfo.Instance.Password = isLoginByAd ? password : SHAHelper.EncryptPassword(password);
ERPInfo.Instance.SkinName = skinName;
ERPInfo.Instance.AccountBook = cbLedger.Text;
DBConfig.Instance.DataBook = cbLedger.Text;
DBConfig.Instance.LoginName = isLoginByAd ? lueUserName.Text.Trim() : userName;
DBConfig.Instance.NoticeUserID = userId;
DBConfig.Instance.NoticeUserName = userName;
DBConfig.Instance.WriteConfig();
ERPInfo.Instance.LoginAccount = userCode;
if (userName.Equals("管理员") && !isAd)
{
password = SqlHelper.ExecuteString("Password", "select Password from P_EmployeeTab where LoginAccount='" + ERPInfo.Instance.LoginAccount + "'");
ERPInfo.Instance.Password = password;
}
this.SetDelphiDllParams(); // 设置调用delphi参数
bool isStartMain = true;
if (SystemInfo.Instance.ForceUpdatePassword && string.IsNullOrEmpty(password))
{
// 弹出修改密码窗口
FrmPassword frmPassword = new FrmPassword();
frmPassword.UserId = userId;
frmPassword.UserName = userName;
DialogResult result = frmPassword.ShowDialog();
isStartMain = result == DialogResult.OK;
}
ERPInfo.Instance.UserId = userId;
ERPInfo.Instance.UserName = userName;
IniHelper.Write("isRefresh", "1");
if (Manager.ExternalMainShell == null)
{
this.Close();
Manager.ReStartMain(true);
}
else
{
// WPF 已接管主窗口生命周期:只返回切换成功,
// 由 WPF 在当前模态窗体关闭后重建主框架。
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
break;
case 1: // 密码不正确
MessageUtil.Show(ResourceKeys.NameOrPasswordError);
break;
case 2: // 已有此用户登录
MessageUtil.Show(ResourceKeys.AlreadyLogin);
break;
case -1: // 登录失败
default:
MessageUtil.Show(ResourceKeys.LoginFault);
break;
}
}
catch (Exception ex)
{
LogUtil.WriteError("保存修改出错!", ex);
MessageUtil.Show("保存修改出错!" + ex);
}
}
/// <summary>
/// 回车事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
saveChange();
}
}
/// <summary>
/// <para>说明:窗口加载</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-15 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void FrmSwap_Load(object sender, EventArgs e)
{
try
{
this.SetLedgers();
this.SetUsers();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:切换帐套</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-15 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DevExpress.XtraEditors.Controls.ClosedEventArgs"/> instance containing the event data.</param>
private void OnLedgerClosed(object sender, ClosedEventArgs e)
{
try
{
WaitForm.ShowForm(ResourceKeys.ServerConnecting);
string ledgerValue = cbLedger.EditValue + "";
this.StartLedger(ledgerValue);
}
catch (Exception ex)
{
// LogUtil.WriteError(ResourceKeys.LoginModuleName, ex);
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
finally
{
WaitForm.HideForm();
}
}
/// <summary>
/// <para>说明:切换用户</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-15 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void btnSave_Click(object sender, EventArgs e)
{
try
{
saveChange();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// <para>说明:取消</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-15 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void simpleButton1_Click(object sender, EventArgs e)
{
try
{
if (!_ledgerName.Equals(cbLedger.EditValue + ""))
{
this.StartLedger(_ledgerName);
}
this.Close();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// 点击右上角关闭判断
/// </summary>
/// <param name="msg"></param>
protected override void WndProc(ref Message msg)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (msg.Msg == WM_SYSCOMMAND && ((int)msg.WParam == SC_CLOSE))
{
if (!string.IsNullOrWhiteSpace(_ledgerName))
{
if (!_ledgerName.Equals(cbLedger.EditValue + ""))
{
this.StartLedger(_ledgerName);
}
}
}
base.WndProc(ref msg);
}
/// <summary>
/// <para>说明:打开搜索面板</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-15 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void lueUserName_Click(object sender, EventArgs e)
{
try
{
lueUserName.ShowPopup();
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
}
/// <summary>
/// 使用AD域登录
/// <para>创建日期:2023-3-30 </para>
/// <para>修改人:王一帆</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <returns></returns>
private bool LoginByAD(string LoginAccount, string Pwd, ref BaseResponse responseinfo)
{
if (ADUtil.ADLogin == null)
{
ADUtil.ADLogin = SystemInfo.Instance.adlogin;
ADUtil.ADPath = SystemInfo.Instance.ADPath;
ADUtil.ADUser = SystemInfo.Instance.ADUser;
ADUtil.ADPwd = SystemInfo.Instance.ADPwd;
if (string.IsNullOrEmpty(ADUtil.ADPath))
{
//responseinfo.msg = "AD域配置不正确,请检查!";
return false;
}
}
if (ADUtil.ADLogin == true)
{
LoginResult result = ADUtil.LoginByAccount(LoginAccount, Pwd);
responseinfo.success = true;
if (result != LoginResult.LOGIN_USER_OK)
{
responseinfo.success = false;
switch (result)
{
case LoginResult.LOGIN_USER_ACCOUNT_INACTIVE: responseinfo.msg = "登录失败,用户账号被禁用!"; break;
case LoginResult.LOGIN_USER_PASSWORD_INCORRECT: responseinfo.msg = "登录失败,用户密码不正确!"; break;
case LoginResult.LOGIN_USER_DOESNT_EXIST: responseinfo.msg = "登录失败,用户不存在!"; break;
}
return false;
}
return true;
}
return false;
}
}
}