基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
4.2 KiB

  1. using BPA.SAAS.Manage.Application.Auth.Dtos;
  2. using BPA.SAAS.Manage.Comm.Const;
  3. using BPA.SAAS.Manage.Comm.Enum;
  4. using BPA.SAAS.Manage.Core.Org;
  5. using Mapster;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace BPA.SAAS.Manage.Application.Auth
  14. {
  15. public class AuthService : IAuthService, IDynamicApiController
  16. {
  17. private readonly ISqlSugarClient _db;
  18. private readonly IHttpContextAccessor _httpContextAccessor;
  19. public AuthService(ISqlSugarClient db, IHttpContextAccessor httpContextAccessor)
  20. {
  21. _db = db;
  22. _httpContextAccessor = httpContextAccessor;
  23. }
  24. /// <summary>
  25. /// 用户名密码登录
  26. /// </summary>
  27. /// <param name="LoginType">1平台用户登录,0加盟商登录</param>
  28. /// <param name="input"></param>
  29. /// <returns></returns>
  30. [HttpPost("/api/auth/login")]
  31. [AllowAnonymous]
  32. public async Task<LoginOutInfo> Login([FromHeader] string logintype, [Required] LoginInput input)
  33. {
  34. // 获取加密后的密码
  35. var encryptPasswod = MD5Encryption.Encrypt(input.Password).ToLower();
  36. // 判断用户名和密码是否正确 忽略全局过滤器
  37. var user = await _db.Queryable<BPA_Users>().Where(u => u.Account.Equals(input.Account)
  38. && u.Password.Equals(encryptPasswod)
  39. && u.IsDeleted == 0
  40. ).FirstAsync();
  41. _ = user ?? throw Oops.Oh("用户名或密码不正确");
  42. //获取权限
  43. var company = _db.Queryable<BPA_Company>().First(x => x.Id == user.GroupId && x.Status == CommonStatus.ENABLE && x.IsDeleted == 0);
  44. string CLAINM_SUPERADMIN = "Customer";
  45. if (user.AdminType == 0)
  46. {
  47. CLAINM_SUPERADMIN = "1";
  48. }
  49. _ = company ?? throw Oops.Oh("企业被锁定,请联系管理员");
  50. var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
  51. {
  52. { ClaimConst.CLAINM_USERID, user.Id },
  53. { ClaimConst.LoginType, 0},
  54. { ClaimConst.CLAINM_ACCOUNT, user.Account },
  55. { ClaimConst.CLAINM_NAME, user.Name },
  56. { ClaimConst.CLAINM_SUPERADMIN, CLAINM_SUPERADMIN },
  57. { ClaimConst.GroupId, user.GroupId},
  58. { ClaimConst.OrgId,user.SysOrgId},
  59. //{ ClaimConst.SupplyPlatformId,company?.SupplyPlatformId}
  60. }, 1440);
  61. // 设置Swagger自动登录
  62. // _httpContextAccessor.SigninToSwagger(accessToken);
  63. // 生成刷新Token令牌
  64. var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, 1445);
  65. _httpContextAccessor.HttpContext.Response.Headers["access-token"] = accessToken;
  66. // 设置刷新Token令牌
  67. _httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
  68. LoginOutInfo loginOutInfo = new LoginOutInfo()
  69. {
  70. userID = user.Id,
  71. token = accessToken
  72. };
  73. return loginOutInfo;
  74. }
  75. /// <summary>
  76. /// 获取当前登录用户信息
  77. /// </summary>
  78. /// <returns></returns>
  79. [HttpGet("/api/auth/getLoginUser")]
  80. [AllowAnonymous]
  81. public async Task<LoginOutput> GetLoginUserAsync()
  82. {
  83. string userid = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  84. var user = await _db.Queryable<BPA_Users>().Where(u => u.Id == userid).FirstAsync();
  85. if (user != null)
  86. {
  87. var reslut = user.Adapt<LoginOutput>();
  88. return reslut;
  89. }
  90. else
  91. {
  92. throw Oops.Oh($"用户不存在");
  93. }
  94. }
  95. /// <summary>
  96. /// 退出
  97. /// </summary>
  98. /// <returns></returns>
  99. [HttpGet("/api/auth/logout")]
  100. public async Task LogoutAsync()
  101. {
  102. // _httpContextAccessor.SignoutToSwagger();
  103. await Task.CompletedTask;
  104. }
  105. }
  106. }