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

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