LoginExcutor.cs 5.0 KB

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