You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.4 KiB

using ICSSoft.ERPWMS.Entity;
using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ICSSoft.ERPWMS.SQL
{
public class GetToken
{
/// <summary>
/// 返回值
/// </summary>
public string Get(ICSLogin infos)
{
ICSLogin szJson = new ICSLogin();
DataTable dt = null;
string json = "";
if (string.IsNullOrWhiteSpace(infos.LoginID))
{
throw new Exception("传入参数为空!");
}
if (string.IsNullOrWhiteSpace(infos.Secret))
{
throw new Exception("密码为空!");
}
string res = string.Empty;
try
{
string sql = string.Empty;
sql = @"SELECT StartTime,Time,Authkey from Cust_ICSLogin where LoginID='" + infos.LoginID + "' and Secret='" + infos.Secret + "'";
if (infos.Type=="ERP")
{
dt = ICSHelper.GetDataTableERP(sql);
}
else
{
dt = ICSHelper.GetDataTable(sql);
}
//json = JsonConvert.SerializeObject(dt);
if (dt.Rows.Count <= 0 || dt == null)
throw new Exception("传入账号有误,请核对!");
else
{
if (!string.IsNullOrWhiteSpace(dt.Rows[0]["StartTime"].ToString()))
{
DateTime dtime = Convert.ToDateTime(dt.Rows[0]["StartTime"].ToString());
int Time = Convert.ToInt32(dt.Rows[0]["Time"].ToString());
if (DateTime.Now < dtime.AddMinutes(Time))
{
json = dt.Rows[0]["Authkey"].ToString();
}
else
{
string charset = "UTF-8";
string content = infos.LoginID + infos.Secret + DateTime.Now;
string key = "ICS999";
json = DoSign(content, key, charset);
if (!string.IsNullOrWhiteSpace(json))
{
sql = @"UPDATE Cust_ICSLogin SET Authkey ='" + json + "',StartTime='" + DateTime.Now + "' WHERE LoginID='" + infos.LoginID + "' and Secret='" + infos.Secret + "'";
if (infos.Type == "ERP")
{
ICSHelper.ExecuteDateERP(sql);//修改包涵拓展(公共段19)
}
else
{
ICSHelper.ExecuteDate(sql);//修改包涵拓展(公共段19)
}
}
}
}
}
return json;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public bool Check(string Authkey,string Type)
{
bool isfalse = false;
DataTable dt = new DataTable();
try
{
string IsOpenKey =ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["IsOpenKey"].ToString();
if (IsOpenKey=="true")
{
string sql = @"SELECT Time from Cust_ICSLogin WHERE Authkey='" + Authkey + "' ";
if (Type== "ERP")
{
dt = ICSHelper.GetDataTableERP(sql);
}
else
{
dt = ICSHelper.GetDataTable(sql);
}
if (dt != null && dt.Rows.Count > 0)
{
string _Time = dt.Rows[0][0].ToString();
int ___Time = Int32.Parse(_Time);
int a = 0;
string sqlAsy = @"SELECT COUNT(*) from Cust_ICSLogin WHERE Authkey='" + Authkey + "' AND StartTime>'" + DateTime.Now.AddMinutes(-___Time) + "'";
if (Type == "ERP")
{
dt = ICSHelper.GetDataTableERP(sqlAsy);
}
else
{
dt = ICSHelper.GetDataTable(sqlAsy);
}
//json = JsonConvert.SerializeObject(dt);
if (dt.Rows.Count <= 0 || dt == null)
throw new Exception("Authkey已失效,请重新获取!");
else
{
a = Int32.Parse(dt.Rows[0][0].ToString());
}
if (a > 0)
isfalse = true;
}
else
{
throw new Exception("Authkey已失效,请重新获取!");
}
}
else
isfalse = true;
return isfalse;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 请求报文签名
/// </summary>
/// <param name="content">内容参数</param>
/// <param name="charset">编码格式</param>
/// <param name="key">key值</param>
/// <returns></returns>
public static string DoSign(string content, string key, string charset = "UTF-8")
{
string sign;
content = content + key;
//md5加密
MD5 md5 = new MD5CryptoServiceProvider();
byte[] b = md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(content));
//base64编码
sign = Convert.ToBase64String(b).Trim();
return sign;
}
public class ICSLogin
{
public string Secret;
public string LoginID;
public string Type;
}
}
}