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.

117 lines
5.2 KiB

2 years ago
  1. using EasyBL.WebApi;
  2. using EasyBL.WebApi.Common;
  3. using EasyBL.WebApi.Message;
  4. using EasyNet;
  5. using Entity.Sugar;
  6. using Newtonsoft.Json;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Net.Http;
  10. namespace EasyBL
  11. {
  12. public class BaseAuthorizeService : ServiceBase
  13. {
  14. #region 根据用户名获取token
  15. /// <summary>
  16. /// 函式名稱:GetToken
  17. /// 函式說明:获取token
  18. /// </summary>
  19. /// <param name="orgId">todo: describe orgId parameter on GetToken</param>
  20. /// <param name="userId">todo: describe userId parameter on GetToken</param>
  21. /// <param name="pasWd">todo: describe pasWd parameter on GetToken</param>
  22. /// <returns>
  23. /// 回傳 rm(Object)
  24. ///</returns>
  25. public HttpResponseMessage GetToken(string orgId, string userId, string pasWd)
  26. {
  27. SuccessResponseMessage srm = null;
  28. string sError = null;
  29. var db = SugarBase.GetIntance();
  30. try
  31. {
  32. do
  33. {
  34. //判断参数是否合法
  35. if (string.IsNullOrEmpty(orgId) || string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(pasWd))
  36. {
  37. srm = new SuccessResponseMessage(null, null)
  38. {
  39. STATUSCODE = (int)StatusCodeEnum.ParameterError,
  40. MSG = StatusCodeEnum.ParameterError.GetEnumText()
  41. };
  42. srm.DATA.Add("token", "");
  43. return HttpResponseExtension.ToJson(JsonConvert.SerializeObject(srm));
  44. }
  45. var oTicket = (OTB_SYS_TicketAuth)HttpRuntimeCache.Get(orgId + userId);
  46. if (oTicket == null || oTicket.ExpireTime < DateTime.Now)
  47. {
  48. var sEncryptPwd = SecurityUtil.Encrypt(pasWd);//將輸入之密碼轉換驗證格式
  49. var oUser = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == orgId && it.MemberID == userId && it.Password == sEncryptPwd);
  50. if (oUser != null)
  51. {
  52. oTicket = db.Queryable<OTB_SYS_TicketAuth>().Single(it => it.OrgID == orgId && it.UserID == userId);
  53. if (oTicket == null)
  54. {
  55. oTicket = new OTB_SYS_TicketAuth();
  56. }
  57. oTicket.Token = SignExtension.CreateToken();
  58. var iExpireTime = 240;
  59. var sExpireTime = Common.GetSystemSetting(db, orgId, "ExpireTime");
  60. if (!string.IsNullOrEmpty(sExpireTime))
  61. {
  62. iExpireTime = int.Parse(sExpireTime);
  63. }
  64. else
  65. {
  66. iExpireTime = int.Parse(WebSettingsConfig.ExpireTime);
  67. }
  68. oTicket.ExpireTime = DateTime.Now.AddMinutes(iExpireTime); //30分钟过期
  69. if (oTicket.UserID != null && oTicket.OrgID != null)
  70. {
  71. var iRel = db.Updateable(oTicket).IgnoreColumns(it => new { it.NO })
  72. .Where(it => it.NO == oTicket.NO).ExecuteCommand();
  73. }
  74. else
  75. {
  76. oTicket.OrgID = orgId;
  77. oTicket.UserID = userId;
  78. oTicket.UserName = "";
  79. oTicket.LoginIp = "";
  80. oTicket.LoginTime = DateTime.Now;
  81. oTicket.CreateTime = DateTime.Now;
  82. db.Insertable(oTicket).ExecuteCommand();
  83. }
  84. HttpRuntimeCache.Set(oTicket.OrgID + oTicket.UserID, oTicket, iExpireTime * 60, true);
  85. }
  86. else
  87. {
  88. oTicket = new OTB_SYS_TicketAuth();
  89. }
  90. }
  91. //返回token信息
  92. srm = new SuccessResponseMessage(null, null);
  93. srm.DATA.Add("token", oTicket.Token);
  94. } while (false);
  95. }
  96. catch (Exception ex)
  97. {
  98. sError = Util.GetLastExceptionMsg(ex);
  99. srm = new SuccessResponseMessage(null, null)
  100. {
  101. STATUSCODE = (int)StatusCodeEnum.Error,
  102. MSG = StatusCodeEnum.Error.GetEnumText()
  103. };
  104. srm.DATA.Add("token", "");
  105. LogAndSendEmail(sError + " Param:" + orgId + "|" + userId + "|" + pasWd, ex, orgId, userId, "AuthorizeService", nameof(GetToken), "GetToken(获取token)", "", "", "");
  106. }
  107. return HttpResponseExtension.ToJson(JsonConvert.SerializeObject(srm));
  108. }
  109. #endregion 根据用户名获取token
  110. }
  111. }