LoginExcutor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using JLHHJSvr.BLL;
  8. using JLHHJSvr.Com;
  9. using JLHHJSvr.Com.Model;
  10. using JLHHJSvr.DBA.DBModle;
  11. using JLHHJSvr.LJException;
  12. using LJLib.DAL.SQL;
  13. using LJLib.Net.SPI.Server;
  14. using LJLib.Tools.DEncrypt;
  15. using LJLib.Tools.Encry;
  16. namespace JLHHJSvr.Excutor
  17. {
  18. internal sealed class LoginExcutor : ExcutorBase<LoginRequest, LoginResponse>
  19. {
  20. protected override void ExcuteInternal(LoginRequest request, object state, LoginResponse rslt)
  21. {
  22. if (string.IsNullOrEmpty(request.usercode))
  23. {
  24. rslt.ErrMsg = "用户名不能为空";
  25. return;
  26. }
  27. //if (string.IsNullOrEmpty(request.psw))
  28. //{
  29. // rslt.ErrMsg = "密码不能为空";
  30. //}
  31. u_user_jlhprice stUser = new u_user_jlhprice();
  32. rslt.rsltFunids = new List<int>();
  33. using (var con = new SqlConnection(GlobalVar.ConnectionString))
  34. using (var cmd = con.CreateCommand())
  35. {
  36. con.Open();
  37. try
  38. {
  39. if (DbSqlHelper.SelectOne(cmd, "u_user_jlhprice", "userid = @usercode",
  40. new Dictionary<string, object>() { { "@usercode", request.usercode } }, stUser,
  41. "userid, empid, username, usermode, psw, access_failed_count, last_failed_attempt_time") != 1)
  42. {
  43. rslt.ErrMsg = "用户名不存在或密码错误";
  44. return;
  45. }
  46. // 判断是否lock
  47. if (stUser.isLocked)
  48. {
  49. throw new LJCommonException("登录连续错误5次,账号已锁定,请联系管理员解锁!");
  50. }
  51. psw_bczh3 pswhelper = new psw_bczh3();
  52. if (pswhelper.GetEntrypt(request.psw, 0, "123457851239866") != stUser.psw)
  53. {
  54. using (cmd.Transaction = con.BeginTransaction())
  55. {
  56. try
  57. {
  58. cmd.CommandText = @"UPDATE u_user_jlhprice SET access_failed_count = CASE
  59. WHEN last_failed_attempt_time < @failedDate THEN 1
  60. ELSE access_failed_count + 1
  61. END,
  62. last_failed_attempt_time = GETUTCDATE()
  63. WHERE u_user_jlhprice.empid = @empid";
  64. cmd.Parameters.Clear();
  65. cmd.Parameters.AddWithValue("@failedDate", DateTime.UtcNow.AddMonths(-1));
  66. cmd.Parameters.AddWithValue("@empid", stUser.empid);
  67. cmd.ExecuteNonQuery();
  68. cmd.Transaction.Commit();
  69. }
  70. catch (Exception e)
  71. {
  72. cmd.Transaction.Rollback();
  73. }
  74. }
  75. throw new LJCommonException($"密码错误,剩余尝试次数:{5 - stUser.access_failed_count + 1} 次(共 5 次)");
  76. }
  77. string token = Guid.NewGuid().ToString();
  78. rslt.token = token;
  79. rslt.username = stUser.username;
  80. rslt.usercode = stUser.userid;
  81. rslt.empid = stUser.empid;
  82. rslt.usermode = stUser.usermode;
  83. rslt.rsltFunids = UserHelper.FilterMyFunids(cmd, stUser.empid);
  84. var tokenData = new TokenData
  85. {
  86. empid = stUser.empid,
  87. usercode = stUser.userid,
  88. userid = stUser.empid,
  89. username = stUser.username,
  90. usermode = stUser.usermode
  91. };
  92. BllHelper.SetToken(token, tokenData);
  93. // 登录成功,清除错误次数
  94. using (cmd.Transaction = con.BeginTransaction())
  95. {
  96. try
  97. {
  98. UserHelper.UnLock(cmd, new List<int>() { stUser.empid });
  99. cmd.Transaction.Commit();
  100. }
  101. catch (Exception e)
  102. {
  103. cmd.Transaction.Rollback();
  104. }
  105. }
  106. }
  107. catch(LJCommonException ex)
  108. {
  109. rslt.ErrMsg = ex.Message;
  110. }
  111. }
  112. }
  113. }
  114. }