using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace ButcherManage.BO.Utils
|
|
{
|
|
public static class LoginUtil
|
|
{
|
|
const string WORKPATH = "Config\\Worker.ldb";
|
|
public static Worker InitUserFromLocal()
|
|
{
|
|
var work = new Worker();
|
|
if (!AppContext.ConnectInfo.LocalOffline)
|
|
{
|
|
if (File.Exists(WORKPATH))
|
|
work.Name = File.ReadAllText(WORKPATH);
|
|
}
|
|
else
|
|
{
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var query = new DmoQuery(typeof(Worker));
|
|
var obj = session.ExecuteScalar(query);
|
|
if (obj != null)
|
|
work = (Worker)obj;
|
|
}
|
|
}
|
|
return work;
|
|
}
|
|
|
|
public static string GetWorkerNameByCode(string code)
|
|
{
|
|
string method = "/MainSystem/B3ClientService/Rpcs/LoginRpc/GetWorkerNameByCode";
|
|
var result = SimpleRest.Call<string>(method, code);
|
|
int r;
|
|
if (int.TryParse(result, out r))
|
|
{
|
|
if (r == 0)
|
|
throw new Exception("工号输入错误");
|
|
throw new Exception("账号被停用");
|
|
}
|
|
else
|
|
{
|
|
AppContext.Worker.Code = code;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public static void Login(string userName, string pwd)
|
|
{
|
|
RpcLogin(userName, pwd);
|
|
|
|
AppContext.Worker.Name = userName;
|
|
AppContext.Worker.Password = EncodePwd(pwd);
|
|
if (!AppContext.ConnectInfo.LocalOffline)
|
|
{
|
|
File.WriteAllText(WORKPATH, userName);
|
|
return;
|
|
}
|
|
using (var session = DmoSession.New())
|
|
{
|
|
var table = DmoInfo.Get(typeof(Worker)).MappedDBObject;
|
|
var sql = string.Format(@"delete from [{0}]", table);
|
|
session.ExecuteSqlNonQuery(sql);
|
|
session.Insert(AppContext.Worker);
|
|
session.Commit();
|
|
}
|
|
}
|
|
|
|
static void RpcLogin(string name, string pwd)
|
|
{
|
|
const string loginMethod = "/MainSystem/B3ClientService/Rpcs/LoginRpc/Login";
|
|
var r = SimpleRest.Call<long>(loginMethod, name, pwd);
|
|
if (r == 0)
|
|
throw new Exception("用户名密码错误");
|
|
else if (r == -1)
|
|
throw new Exception("账号被停用");
|
|
AppContext.Worker.ID = r;
|
|
const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/LoginRpc/GetWorkerBindDrive";
|
|
AppContext.Worker.Role = SimpleRest.Call<string>(wpfUserMethod, AppContext.Worker.ID);
|
|
}
|
|
|
|
public static byte[] EncodePwd(string pwd)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
return md5.ComputeHash(Encoding.Unicode.GetBytes(pwd));
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|