Login.razor.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using EasyTemplate.Blazor.Web.Common;
  2. using EasyTemplate.Tool;
  3. using EasyTemplate.Tool.Util;
  4. using Microsoft.JSInterop;
  5. namespace EasyTemplate.Blazor.Web.Components.Pages.Account.Login;
  6. //https://www.cnblogs.com/j4587698/p/16531294.html
  7. public partial class Login
  8. {
  9. protected override async void OnInitialized()
  10. {
  11. base.OnInitialized();
  12. }
  13. protected override async Task OnAfterRenderAsync(bool firstRender)
  14. {
  15. if (firstRender)
  16. {
  17. Model.LoginType = "1";
  18. var localStorageHelper = new LocalStorage(JSRuntime);
  19. var auto = await localStorageHelper.GetLocalStorage(LocalStorage.AutoLogin);
  20. Model.AutoLogin = auto == "True";
  21. if (Model.AutoLogin)
  22. {
  23. var user = await localStorageHelper.GetLocalStorage(LocalStorage.UserInfo);
  24. if (!string.IsNullOrWhiteSpace(user))
  25. {
  26. var suser = Crypto.AESDecrypt(user).ToEntity<SystemUser>();
  27. var match = User.AsQueryable()
  28. .Where(x => x.Account == suser.Account && x.Password == suser.Password)
  29. .First();
  30. if (match is not null)
  31. ExecuteLogin(suser, true);
  32. }
  33. }
  34. }
  35. }
  36. private async Task HandleSubmit()
  37. {
  38. LoginLoading = true;
  39. try
  40. {
  41. SystemUser user = null;
  42. if (Model.LoginType == "1")
  43. {
  44. if (string.IsNullOrWhiteSpace(Model.Account) || string.IsNullOrWhiteSpace(Model.Password))
  45. {
  46. Message.Error("账号或密码错误");
  47. return;
  48. }
  49. user = User.AsQueryable()
  50. .Where(x => x.Account == Model.Account && x.Password == Crypto.MD5Encrypt(Model.Password))
  51. .First();
  52. }
  53. else
  54. {
  55. if (string.IsNullOrWhiteSpace(Model.Mobile) || string.IsNullOrWhiteSpace(Model.VerifyCode))
  56. {
  57. Message.Error("手机号或验证码错误");
  58. return;
  59. }
  60. user = User.AsQueryable()
  61. .Where(x => x.Mobile == Model.Mobile)
  62. .First();
  63. }
  64. if (user == null)
  65. {
  66. Message.Error("账号或密码错误");
  67. return;
  68. }
  69. var localStorageHelper = new LocalStorage(JSRuntime);
  70. if (Model.AutoLogin)
  71. {
  72. await localStorageHelper.SetLocalStorage(LocalStorage.AutoLogin, Model.AutoLogin.ToString());//
  73. }
  74. await localStorageHelper.SetLocalStorage(LocalStorage.UserInfo, Crypto.AESEncrypt(new { user.Id, user.Account, user.Password, Expired = DateTime.Now.AddDays(Global.Expired) }.ToJson()));
  75. ExecuteLogin(user);
  76. }
  77. catch (Exception ex)
  78. {
  79. Message.Error(ex.ToString());
  80. }
  81. finally
  82. {
  83. LoginLoading = false;
  84. }
  85. }
  86. public async Task ExecuteLogin(SystemUser user, bool auto = false)
  87. {
  88. var host = Accessor.GetHost();
  89. Global.CurrentUser = User.AsQueryable().Where(x => x.Account == user.Account && x.Password == user.Password).First();
  90. if (auto)
  91. {
  92. LogLogin.Insert(new SystemLogLogin() { Info = $"用户【{Global.CurrentUser.Account}】通过自动登录功能登录账号", UserId = Global.CurrentUser.Id, IpAddress= host, CreateTime = DateTime.Now });
  93. }
  94. else
  95. {
  96. LogLogin.Insert(new SystemLogLogin() { Info = $"用户【{Global.CurrentUser.Account}】登录账号", UserId = Global.CurrentUser.Id, IpAddress = host, CreateTime = DateTime.Now });
  97. }
  98. //AuthStateProvider.SignIn(Global.CurrentUser);
  99. NavigationManager.NavigateTo("/");
  100. }
  101. /// <summary>
  102. /// 发送短信验证码
  103. /// </summary>
  104. private async Task SendSM()
  105. {
  106. Loading = true;
  107. if (!string.IsNullOrWhiteSpace(Model?.Mobile))
  108. {
  109. await Task.Delay(8000);
  110. }
  111. else
  112. {
  113. MessageService.Error("请输入手机号");
  114. }
  115. Loading = false;
  116. }
  117. /// <summary>
  118. ///
  119. /// </summary>
  120. private readonly LoginInput Model = new();
  121. /// <summary>
  122. ///
  123. /// </summary>
  124. [Inject] public NavigationManager NavigationManager { get; set; }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. [Inject] public MessageService Message { get; set; }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. [Inject] private SqlSugarRepository<SystemUser> User { get; set; }
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. [Inject] private SqlSugarRepository<SystemLogLogin> LogLogin { get; set; }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. [Inject] private CustomAuthenticationStateProvider AuthStateProvider { get; set; }
  141. /// <summary>
  142. ///
  143. /// </summary>
  144. [Inject] private IHttpContextAccessor Accessor { get; set; }
  145. /// <summary>
  146. ///
  147. /// </summary>
  148. [Inject] private IJSRuntime JSRuntime { get; set; }
  149. /// <summary>
  150. ///
  151. /// </summary>
  152. [Inject] private IMessageService MessageService { get; set; }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. private bool Loading;
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. private bool LoginLoading;
  161. }