Login.razor.cs 5.5 KB

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