using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using Forks.JsonRpc.Client;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
namespace ButcherFactory.BO.Utils
|
|
{
|
|
public static class LoginUtil
|
|
{
|
|
public static WpfUser InitUserFromLocal()
|
|
{
|
|
if (string.IsNullOrEmpty(AppContext.ConnectInfo.SqlConnection))
|
|
return new WpfUser();
|
|
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
|
|
{
|
|
var query = new DmoQuery(typeof(WpfUser));
|
|
var obj = session.ExecuteScalar(query);
|
|
if (obj != null)
|
|
{
|
|
var user = (WpfUser)obj;
|
|
if (!string.IsNullOrEmpty(user.Role))
|
|
user.RoleList.AddRange(user.Role.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
|
|
return user;
|
|
}
|
|
}
|
|
return new WpfUser();
|
|
}
|
|
|
|
public static void InitRpcFacade()
|
|
{
|
|
if (string.IsNullOrEmpty(AppContext.ConnectInfo.ServerUrl))
|
|
throw new Exception("请先设置服务器地址");
|
|
if (!AppContext.RpcFacadeInited)
|
|
{
|
|
RpcFacade.Init(AppContext.ConnectInfo.ServerUrl, "ButcherFactorySolution");
|
|
AppContext.RpcFacadeInited = true;
|
|
}
|
|
}
|
|
|
|
public static void ReInitRpcFacade()
|
|
{
|
|
if (AppContext.RpcFacadeInited)
|
|
RpcFacade.ReInit(AppContext.ConnectInfo.ServerUrl);
|
|
}
|
|
|
|
public static void LoginOut()
|
|
{
|
|
if (AppContext.User.Login)
|
|
{
|
|
try
|
|
{
|
|
AppContext.User.Login = false;
|
|
RpcFacade.Logout();
|
|
}
|
|
catch { };
|
|
}
|
|
}
|
|
|
|
public static string GetUserNameByCode(string code, out string error)
|
|
{
|
|
try
|
|
{
|
|
error = string.Empty;
|
|
const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserName";
|
|
return RpcFacade.Call<string>(wpfUserMethod, code);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error = ex.ToString();
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
public static void Login(string userName, string pwd)
|
|
{
|
|
RpcFacade.Login(userName, pwd);
|
|
AppContext.User = new WpfUser();
|
|
AppContext.User.Login = true;
|
|
AppContext.User.Name = userName;
|
|
FillUserEmpInfo(userName);
|
|
AppContext.User.Password = EncodePwd(pwd);
|
|
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
|
|
{
|
|
var table = DmoInfo.Get(typeof(WpfUser)).MappedDBObject;
|
|
var sql = string.Format(@"delete from [{0}]", table);
|
|
session.ExecuteSqlNonQuery(sql);
|
|
session.Insert(AppContext.User);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static void FillUserEmpInfo(string name)
|
|
{
|
|
const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserID";
|
|
AppContext.User.ID = RpcFacade.Call<long>(wpfUserMethod);
|
|
}
|
|
|
|
public static byte[] EncodePwd(string pwd)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
return md5.ComputeHash(Encoding.Unicode.GetBytes(pwd));
|
|
}
|
|
|
|
public static bool UserIsInRole(string roleName)
|
|
{
|
|
const string method = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/UserIsInRole";
|
|
return RpcFacade.Call<bool>(method, roleName);
|
|
}
|
|
|
|
public static void UpdateUserRole()
|
|
{
|
|
using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection))
|
|
{
|
|
var update = new DQUpdateDom(typeof(WpfUser));
|
|
update.Columns.Add(new DQUpdateColumn("Role", string.Join(" ", AppContext.User.RoleList)));
|
|
session.ExecuteNonQuery(update);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
public static bool TestConnection(int? millisecondsTimeout = null)
|
|
{
|
|
var url = AppContext.ConnectInfo.ServerUrl;
|
|
if (string.IsNullOrEmpty(url))
|
|
return false;
|
|
var uri = new Uri(url);
|
|
if (millisecondsTimeout == null)
|
|
{
|
|
millisecondsTimeout = 50;
|
|
}
|
|
return TestConnection(uri.Host, uri.Port, millisecondsTimeout.Value);
|
|
}
|
|
|
|
public static bool TestConnection(string host, int port, int millisecondsTimeout)
|
|
{
|
|
var client = new System.Net.Sockets.TcpClient();
|
|
try
|
|
{
|
|
var ar = client.BeginConnect(host, port, null, null);
|
|
ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
|
|
return client.Connected;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
client.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|