Login.razor.cs 6.1 KB

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