LoginService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Ant.Core.Utils;
  2. using Ant.Core.WebApi.Enum;
  3. using Ant.Core.WebApi.Model;
  4. using Central.Control.WebApi.Cache;
  5. using Central.Control.WebApi.DbEntity;
  6. using Central.Control.WebApi.EFDbContext;
  7. using Central.Control.WebApi.Models.Request;
  8. using Central.Control.WebApi.Models.Response;
  9. using Central.Control.WebApi.Service.Interface;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Web;
  14. namespace Central.Control.WebApi.Service
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public class LoginService: ILoginService
  20. {
  21. private readonly ICacheHelper _cacheHelper;
  22. private readonly IDbContext _dbContent;
  23. private readonly IUserService _userService;
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="cacheHelper"></param>
  28. /// <param name="dbContent"></param>
  29. public LoginService(
  30. ICacheHelper cacheHelper,
  31. IDbContext dbContent,
  32. IUserService userService)
  33. {
  34. _cacheHelper = cacheHelper;
  35. _dbContent = dbContent;
  36. _userService = userService;
  37. }
  38. /// <summary>
  39. /// 设备登录接口
  40. /// </summary>
  41. /// <returns></returns>
  42. public ApiResult<DeviceLoginResponseDto> DeviceLogin(DeviceLoginRequestDto req)
  43. {
  44. var deviceInfo = _dbContent.Set<SYS_Device>().FirstOrDefault(p => p.Code == req.Code && p.Password == req.Password);
  45. if (deviceInfo == null)
  46. {
  47. return new ApiResult<DeviceLoginResponseDto>(ApiStatusCode.RecordNotFound, "用户名或密码不正确");
  48. }
  49. // 1、验证密码正确,老token进行失效
  50. var oldToken = _cacheHelper.Get<string>(deviceInfo.Code);
  51. if (!string.IsNullOrWhiteSpace(oldToken))
  52. {
  53. _cacheHelper.Remove(oldToken);
  54. }
  55. var loginSession = _dbContent.Set<SYS_LoginSession>().FirstOrDefault(p => p.UserId == deviceInfo.Id);
  56. bool isAdd = false;
  57. if (loginSession == null)
  58. {
  59. isAdd = true;
  60. loginSession = new SYS_LoginSession() { Id = IdGenerator.NewId() };
  61. }
  62. string token = IdGenerator.AntToken();
  63. // 2、存库
  64. loginSession.UserId = deviceInfo.Id;
  65. loginSession.UserName = deviceInfo.Code;
  66. loginSession.Token = token;
  67. loginSession.Expiration = DateTime.MaxValue;
  68. if (isAdd)
  69. {
  70. _dbContent.Set<SYS_LoginSession>().Add(loginSession);
  71. }
  72. // 执行保存
  73. _dbContent.SaveChanges();
  74. // 3、存cache
  75. _cacheHelper.SetByNotExpired(deviceInfo.Code, token);// 这一份存一个凭证
  76. _cacheHelper.SetByNotExpired(token, loginSession);// 这一份存真正的登录信息
  77. DeviceLoginResponseDto result = new DeviceLoginResponseDto()
  78. {
  79. Token = token
  80. };
  81. return new ApiResult<DeviceLoginResponseDto>(result);
  82. }
  83. /// <summary>
  84. /// 退出登录
  85. /// </summary>
  86. /// <returns></returns>
  87. public ApiResult Logout()
  88. {
  89. var session = _userService.GetLoginSession();
  90. // 删除cache session
  91. _cacheHelper.Remove(session.UserName);// 这一份存一个凭证
  92. _cacheHelper.Remove(session.Token);// 这一份存真正的登录信息
  93. // 删除数据库session
  94. var e = _dbContent.Set<SYS_LoginSession>().FirstOrDefault(m => m.UserId == session.UserId);
  95. _dbContent.Set<SYS_LoginSession>().Remove(e);
  96. _dbContent.SaveChanges();
  97. return new ApiResult();
  98. }
  99. }
  100. }