Files
U8-Import/NX.ERP.U8/U8LoginContext.cs
2021-04-26 11:29:45 +08:00

381 lines
8.6 KiB
C#

using MSXML2;
using System;
using System.Collections.Generic;
using System.Text;
using U8Login;
using UFIDA.U8.U8APIFramework;
using UFIDA.U8.U8MOMAPIFramework;
using Dapper;
namespace NX.ERP.U8
{
public class U8LoginContext : IDisposable
{
#region 单例
private static readonly object locker = new object();
private static U8LoginContext _u8LoginContext;
/// <summary>
/// 默认实例
/// </summary>
public static U8LoginContext Instance
{
get
{
if (_u8LoginContext == null)
{
lock (locker)
{
if (_u8LoginContext == null)
{
_u8LoginContext = new U8LoginContext();
}
}
}
return _u8LoginContext;
}
}
private UFSoft.U8.Framework.Login.UI.clsLogin uiLogin;
private clsLogin _clslogin;
#endregion
private string EaiPort = System.Configuration.ConfigurationManager.AppSettings["EaiPort"];
private string EaiPath = System.Configuration.ConfigurationManager.AppSettings["EaiPath"];
public string cAcc_id
{
get
{
return this._clslogin.get_cAcc_Id();
}
}
/// <summary>
///
/// </summary>
public U8LoginContext()
{
}
/// <summary>
///
/// </summary>
/// <param name="Server"></param>
/// <param name="Accid"></param>
public U8LoginContext(string Server, string Accid)
{
this._server = Server;
this._accid = Accid;
}
public U8LoginContext(string userid, string password, string server, string accid) : this(server, accid)
{
this._password = password;
this._userId = userid;
}
private static readonly object locker1 = new object();
public clsLogin ClsLogin
{
get
{
if (this._clslogin == null)
{
lock (locker1)
{
if (this._clslogin == null)
{
this.Login();
}
}
}
return this._clslogin;
}
}
public System.Data.IDbConnection Connection
{
get
{
return new System.Data.SqlClient.SqlConnection(this.Constr4Net);
}
}
//public int GetDefaultPoTaxRate()
//{
// try
// {
// string strSQL = "Select cValue from AccInformation Where cSysID=N'PU' and cName=N'iDefaultZHBVRate'";
// using (var conn = U8LoginContext.Instance.Connection)
// {
// return conn.ExecuteScalar<int>(strSQL, null, null, 0, System.Data.CommandType.Text);
// }
// }
// catch (Exception)
// {
// throw;
// }
// finally
// {
// }
//}
public void Dispose()
{
if (uiLogin != null)
{
uiLogin.SubLogout(this._accid);
uiLogin.ShutDown();
uiLogin = null;
}
}
private string _userId = "";
public string UserId
{
set
{
this._userId = value;
}
}
private string _password = "";
public string PassWord
{
set
{
this._password = value;
}
}
private string _server;
public string Server
{
set
{
this._server = value;
}
}
private string _accid;
public string AccID
{
set
{
this._accid = value;
}
get
{
return this.ClsLogin.get_cAcc_Id();
}
}
public string AccName
{
get
{
return this.ClsLogin.cAccName;
}
}
public string Serial { get; set; }
public string EAIUrl
{
get
{
return $"http://{this.ClsLogin.cServer}:{EaiPort}/{EaiPath}";
}
}
private string _conStr4net;
/// <summary>
/// ADO.NET 链接字符串
/// </summary>
public string Constr4Net
{
get
{
if (string.IsNullOrEmpty(this._conStr4net))
{
this._conStr4net = this.ClsLogin.UFDataConnstringForNet;
}
return this._conStr4net;
}
set
{
this._conStr4net = value;
}
}
public string LoginUserName
{
get
{
return this.ClsLogin.cUserName;
}
}
public string LoginUserId
{
get
{
return this.ClsLogin.cUserId;
}
}
/// <summary>
/// ADO的链接字符串
/// </summary>
public string Constr
{
get
{
return this.ClsLogin.UfDbName;
}
}
private UFSoft.U8.Framework.Login.UI.clsLogin UiLogin
{
get
{
if (uiLogin == null)
{
uiLogin = new UFSoft.U8.Framework.Login.UI.clsLogin();
}
return uiLogin;
}
}
/// <summary>
/// 判断是否使用UI登录
/// </summary>
public bool NeedUiLogin
{
get
{
return string.IsNullOrEmpty(this._userId) ||
string.IsNullOrEmpty(this._password) ||
string.IsNullOrEmpty(this._server) ||
string.IsNullOrEmpty(this._accid);
}
}
public void Login(string userId, string password)
{
if (!UiLogin.login("DP", userId, password, this._server, DateTime.Today.ToString("yyyy-MM-dd"), this._accid, Serial))
{
throw new Exception(UiLogin.ErrDescript);
}
if (UiLogin.SubLogin("ST") == null)
{
throw new Exception(UiLogin.ErrDescript);
}
this._clslogin = new clsLoginClass();
if (!_clslogin.ConstructLogin(UiLogin.userToken))
{
throw new Exception(_clslogin.ShareString);
}
_clslogin.set_TaskId(UiLogin.GetTaskID("ST"));
}
/// <summary>
/// 登录ID 唯一凭据
/// </summary>
public string loginId
{
get
{
return this.UserToken == null ? "" : this.UserToken.UserTokenData.SignedToken.Id;
}
}
public ufsoft UserToken
{
get
{
if (this._clslogin == null)
{
return null;
}
if (this._clslogin.userToken=="")
{
return null;
}
return XmlHelper.XmlDeserialize<ufsoft>(this._clslogin.userToken, Encoding.UTF8);
}
}
public void Login()
{
if (NeedUiLogin)
{
if (!UiLogin.login("DP"))
{
throw new Exception(UiLogin.ErrDescript);
}
}
else
{
if (!UiLogin.login("DP", this._userId, this._password, this._server, DateTime.Today.ToString("yyyy-MM-dd"), this._accid, Serial))
{
throw new Exception(UiLogin.ErrDescript);
}
}
if (UiLogin.SubLogin("ST") == null)
{
throw new Exception(UiLogin.ErrDescript);
}
this._clslogin = new clsLoginClass();
if (!_clslogin.ConstructLogin(UiLogin.userToken))
{
throw new Exception(_clslogin.ShareString);
}
_clslogin.set_TaskId(UiLogin.GetTaskID("ST"));
}
}
}