基础服务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.
 
 

121 lines
4.6 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. List<IConditionalModel> conModels = new List<IConditionalModel>();
  44. conModels.Add(new ConditionalModel() { FieldName = "Id", ConditionalType = ConditionalType.Equal, FieldValue = user.GroupId });
  45. conModels.Add(new ConditionalModel() { FieldName = "Status", ConditionalType = ConditionalType.Equal, FieldValue = CommonStatus.ENABLE.ToString() });
  46. conModels.Add(new ConditionalModel() { FieldName = "IsDeleted", ConditionalType = ConditionalType.Equal, FieldValue ="0" });
  47. var company = _db.Queryable<BPA_Company>().Where(conModels).First();
  48. string CLAINM_SUPERADMIN = "Customer";
  49. if (user.AdminType == 1)
  50. {
  51. CLAINM_SUPERADMIN = "1";
  52. }
  53. _ = company ?? throw Oops.Oh("企业被锁定,请联系管理员");
  54. var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
  55. {
  56. { ClaimConst.CLAINM_USERID, user.Id },
  57. { ClaimConst.LoginType, 0},
  58. { ClaimConst.CLAINM_ACCOUNT, user.Account },
  59. { ClaimConst.CLAINM_NAME, user.Name },
  60. { ClaimConst.CLAINM_SUPERADMIN, CLAINM_SUPERADMIN },
  61. { ClaimConst.GroupId, user.GroupId},
  62. { ClaimConst.OrgId,user.SysOrgId},
  63. //{ ClaimConst.SupplyPlatformId,company?.SupplyPlatformId}
  64. }, 1440);
  65. // 设置Swagger自动登录
  66. // _httpContextAccessor.SigninToSwagger(accessToken);
  67. // 生成刷新Token令牌
  68. var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, 1445);
  69. _httpContextAccessor.HttpContext.Response.Headers["access-token"] = accessToken;
  70. // 设置刷新Token令牌
  71. _httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
  72. LoginOutInfo loginOutInfo = new LoginOutInfo()
  73. {
  74. userID = user.Id,
  75. token = accessToken
  76. };
  77. return loginOutInfo;
  78. }
  79. /// <summary>
  80. /// 获取当前登录用户信息
  81. /// </summary>
  82. /// <returns></returns>
  83. [HttpGet("/api/auth/getLoginUser")]
  84. [AllowAnonymous]
  85. public async Task<LoginOutput> GetLoginUserAsync()
  86. {
  87. string userid = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  88. var user = await _db.Queryable<BPA_Users>().Where(u => u.Id == userid).FirstAsync();
  89. if (user != null)
  90. {
  91. var reslut = user.Adapt<LoginOutput>();
  92. return reslut;
  93. }
  94. else
  95. {
  96. throw Oops.Oh($"用户不存在");
  97. }
  98. }
  99. /// <summary>
  100. /// 退出
  101. /// </summary>
  102. /// <returns></returns>
  103. [HttpGet("/api/auth/logout")]
  104. public async Task LogoutAsync()
  105. {
  106. // _httpContextAccessor.SignoutToSwagger();
  107. await Task.CompletedTask;
  108. }
  109. }
  110. }