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

6 months ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.IdentityModel.Protocols;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class GetToken
  13. {
  14. /// <summary>
  15. /// 返回值
  16. /// </summary>
  17. public string Get(ICSLogin infos)
  18. {
  19. ICSLogin szJson = new ICSLogin();
  20. DataTable dt = null;
  21. string json = "";
  22. if (string.IsNullOrWhiteSpace(infos.LoginID))
  23. {
  24. throw new Exception("传入参数为空!");
  25. }
  26. if (string.IsNullOrWhiteSpace(infos.Secret))
  27. {
  28. throw new Exception("密码为空!");
  29. }
  30. string res = string.Empty;
  31. try
  32. {
  33. string sql = string.Empty;
  34. sql = @"SELECT StartTime,Time,Authkey from Cust_ICSLogin where LoginID='" + infos.LoginID + "' and Secret='" + infos.Secret + "'";
  35. if (infos.Type=="ERP")
  36. {
  37. dt = ICSHelper.GetDataTableERP(sql);
  38. }
  39. else
  40. {
  41. dt = ICSHelper.GetDataTable(sql);
  42. }
  43. //json = JsonConvert.SerializeObject(dt);
  44. if (dt.Rows.Count <= 0 || dt == null)
  45. throw new Exception("传入账号有误,请核对!");
  46. else
  47. {
  48. if (!string.IsNullOrWhiteSpace(dt.Rows[0]["StartTime"].ToString()))
  49. {
  50. DateTime dtime = Convert.ToDateTime(dt.Rows[0]["StartTime"].ToString());
  51. int Time = Convert.ToInt32(dt.Rows[0]["Time"].ToString());
  52. if (DateTime.Now < dtime.AddMinutes(Time))
  53. {
  54. json = dt.Rows[0]["Authkey"].ToString();
  55. }
  56. else
  57. {
  58. string charset = "UTF-8";
  59. string content = infos.LoginID + infos.Secret + DateTime.Now;
  60. string key = "ICS999";
  61. json = DoSign(content, key, charset);
  62. if (!string.IsNullOrWhiteSpace(json))
  63. {
  64. sql = @"UPDATE Cust_ICSLogin SET Authkey ='" + json + "',StartTime='" + DateTime.Now + "' WHERE LoginID='" + infos.LoginID + "' and Secret='" + infos.Secret + "'";
  65. if (infos.Type == "ERP")
  66. {
  67. ICSHelper.ExecuteDateERP(sql);//修改包涵拓展(公共段19)
  68. }
  69. else
  70. {
  71. ICSHelper.ExecuteDate(sql);//修改包涵拓展(公共段19)
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return json;
  78. }
  79. catch (Exception ex)
  80. {
  81. throw new Exception(ex.Message);
  82. }
  83. }
  84. public bool Check(string Authkey,string Type)
  85. {
  86. bool isfalse = false;
  87. DataTable dt = new DataTable();
  88. try
  89. {
  90. string IsOpenKey =ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["IsOpenKey"].ToString();
  91. if (IsOpenKey=="true")
  92. {
  93. string sql = @"SELECT Time from Cust_ICSLogin WHERE Authkey='" + Authkey + "' ";
  94. if (Type== "ERP")
  95. {
  96. dt = ICSHelper.GetDataTableERP(sql);
  97. }
  98. else
  99. {
  100. dt = ICSHelper.GetDataTable(sql);
  101. }
  102. if (dt != null && dt.Rows.Count > 0)
  103. {
  104. string _Time = dt.Rows[0][0].ToString();
  105. int ___Time = Int32.Parse(_Time);
  106. int a = 0;
  107. string sqlAsy = @"SELECT COUNT(*) from Cust_ICSLogin WHERE Authkey='" + Authkey + "' AND StartTime>'" + DateTime.Now.AddMinutes(-___Time) + "'";
  108. if (Type == "ERP")
  109. {
  110. dt = ICSHelper.GetDataTableERP(sqlAsy);
  111. }
  112. else
  113. {
  114. dt = ICSHelper.GetDataTable(sqlAsy);
  115. }
  116. //json = JsonConvert.SerializeObject(dt);
  117. if (dt.Rows.Count <= 0 || dt == null)
  118. throw new Exception("Authkey已失效,请重新获取!");
  119. else
  120. {
  121. a = Int32.Parse(dt.Rows[0][0].ToString());
  122. }
  123. if (a > 0)
  124. isfalse = true;
  125. }
  126. else
  127. {
  128. throw new Exception("Authkey已失效,请重新获取!");
  129. }
  130. }
  131. else
  132. isfalse = true;
  133. return isfalse;
  134. }
  135. catch (Exception ex)
  136. {
  137. throw new Exception(ex.Message);
  138. }
  139. }
  140. /// <summary>
  141. /// 请求报文签名
  142. /// </summary>
  143. /// <param name="content">内容参数</param>
  144. /// <param name="charset">编码格式</param>
  145. /// <param name="key">key值</param>
  146. /// <returns></returns>
  147. public static string DoSign(string content, string key, string charset = "UTF-8")
  148. {
  149. string sign;
  150. content = content + key;
  151. //md5加密
  152. MD5 md5 = new MD5CryptoServiceProvider();
  153. byte[] b = md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(content));
  154. //base64编码
  155. sign = Convert.ToBase64String(b).Trim();
  156. return sign;
  157. }
  158. public class ICSLogin
  159. {
  160. public string Secret;
  161. public string LoginID;
  162. public string Type;
  163. }
  164. }
  165. }