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

250 lines
8.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.Base;
  5. using BPA.SAAS.Manage.Core.DataBase;
  6. using BPA.SAAS.Manage.Core.Org;
  7. using Mapster;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace BPA.SAAS.Manage.Application.Auth
  16. {
  17. public class AuthService : IAuthService, IDynamicApiController
  18. {
  19. private readonly ISqlSugarClient _db;
  20. private readonly IHttpContextAccessor _httpContextAccessor;
  21. public AuthService(ISqlSugarClient db, IHttpContextAccessor httpContextAccessor)
  22. {
  23. _db = db;
  24. _httpContextAccessor = httpContextAccessor;
  25. }
  26. /// <summary>
  27. /// 用户名密码登录
  28. /// </summary>
  29. /// <param name="LoginType">1平台用户登录,0加盟商登录</param>
  30. /// <param name="input"></param>
  31. /// <returns></returns>
  32. [HttpPost("/api/auth/login")]
  33. [AllowAnonymous]
  34. public async Task<LoginOutInfo> Login(LoginInput input)
  35. {
  36. // 获取加密后的密码
  37. var encryptPasswod = MD5Encryption.Encrypt(input.Password).ToLower();
  38. // 判断用户名和密码是否正确 忽略全局过滤器
  39. var user = await _db.Queryable<BPA_Users>().Where(u => u.Account.Equals(input.Account)
  40. && u.Password.Equals(encryptPasswod)
  41. && u.IsDeleted == 0
  42. ).FirstAsync();
  43. _ = user ?? throw Oops.Oh("用户名或密码不正确");
  44. //获取权限
  45. List<IConditionalModel> conModels = new List<IConditionalModel>();
  46. conModels.Add(new ConditionalModel() { FieldName = "Id", ConditionalType = ConditionalType.Equal, FieldValue = user.GroupId });
  47. conModels.Add(new ConditionalModel() { FieldName = "Status", ConditionalType = ConditionalType.Equal, FieldValue = CommonStatus.ENABLE.ToString() });
  48. conModels.Add(new ConditionalModel() { FieldName = "IsDeleted", ConditionalType = ConditionalType.Equal, FieldValue ="0" });
  49. //if (user.AdminType != 1)
  50. //{
  51. // conModels.Add(new ConditionalModel() { FieldName = "Type", ConditionalType = ConditionalType.Equal, FieldValue = input.Type.ToString() });
  52. //}
  53. var company = _db.Queryable<BPA_Company>().Where(conModels).First();
  54. string CLAINM_SUPERADMIN = "Customer";
  55. if (user.AdminType == 1)
  56. {
  57. CLAINM_SUPERADMIN = "1";
  58. }
  59. _ = company ?? throw Oops.Oh("企业被锁定,请联系管理员");
  60. var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
  61. {
  62. { ClaimConst.CLAINM_USERID, user.Id },
  63. { ClaimConst.LoginType, 0},
  64. { ClaimConst.CLAINM_ACCOUNT, user.Account },
  65. { ClaimConst.CLAINM_NAME, user.Name },
  66. { ClaimConst.CLAINM_SUPERADMIN, CLAINM_SUPERADMIN },
  67. { ClaimConst.GroupId, user.GroupId},
  68. { ClaimConst.OrgId,user.SysOrgId},
  69. //{ ClaimConst.SupplyPlatformId,company?.SupplyPlatformId}
  70. }, 1440);
  71. // 设置Swagger自动登录
  72. // _httpContextAccessor.SigninToSwagger(accessToken);
  73. // 生成刷新Token令牌
  74. var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, 1445);
  75. _httpContextAccessor.HttpContext.Response.Headers["access-token"] = accessToken;
  76. // 设置刷新Token令牌
  77. _httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = refreshToken;
  78. LoginOutInfo loginOutInfo = new LoginOutInfo()
  79. {
  80. userID = user.Id,
  81. token = accessToken
  82. };
  83. return loginOutInfo;
  84. }
  85. /// <summary>
  86. /// 获取当前登录用户信息
  87. /// </summary>
  88. /// <returns></returns>
  89. [HttpGet("/api/auth/getLoginUser")]
  90. [AllowAnonymous]
  91. public async Task<LoginOutput> GetLoginUserAsync()
  92. {
  93. string userid = App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
  94. var user = await _db.Queryable<BPA_Users>().Where(u => u.Id == userid).FirstAsync();
  95. if (user != null)
  96. {
  97. var reslut = user.Adapt<LoginOutput>();
  98. return reslut;
  99. }
  100. else
  101. {
  102. throw Oops.Oh($"用户不存在");
  103. }
  104. }
  105. /// <summary>
  106. /// 退出
  107. /// </summary>
  108. /// <returns></returns>
  109. [HttpGet("/api/auth/logout")]
  110. public async Task LogoutAsync()
  111. {
  112. // _httpContextAccessor.SignoutToSwagger();
  113. await Task.CompletedTask;
  114. }
  115. #region 添加平台授权
  116. /// <summary>
  117. /// 分页
  118. /// </summary>
  119. /// <param name="input"></param>
  120. /// <returns></returns>
  121. [HttpPost("/api/authorization/pageauthorization")]
  122. public async Task<PageUtil> PageAuthorization(PageInputBase input)
  123. {
  124. RefAsync<int> total = 0;
  125. var res = await _db.Queryable<BPA_PlatformAuthorization>()
  126. .OrderBy(x => x.CreateAt, OrderByType.Desc)
  127. .Select(x => new PlatformAuthorizationDto()
  128. {
  129. Id = x.Id.SelectAll(),
  130. })
  131. .ToPageListAsync(input.Current, input.PageSize, total);
  132. PageUtil util = new PageUtil()
  133. {
  134. Total = total,
  135. Data = res
  136. };
  137. return util;
  138. }
  139. /// <summary>
  140. /// 添加授权码
  141. /// </summary>
  142. /// <returns></returns>
  143. [HttpPost("/api/authorization/addauthorization")]
  144. public async Task<bool> AddAuthorization(CreateOrUpDatePlatformAuthorizationDto input)
  145. {
  146. var data = _db.Queryable<BPA_PlatformAuthorization>().ToList();
  147. if (data.Count > 0)
  148. {
  149. throw Oops.Oh("授权码已存在");
  150. }
  151. var res = await _db.Insertable(new BPA_PlatformAuthorization()
  152. {
  153. Key = Guid.NewGuid().ToString(),
  154. PeriodValidity = input.PeriodValidity,
  155. UpdateAt=DateTime.Now,
  156. }).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
  157. return res > 0;
  158. }
  159. /// <summary>
  160. /// 修改授权码
  161. /// </summary>
  162. /// <param name="id"></param>
  163. /// <returns></returns>
  164. [HttpPost("/api/authorization/updateauthorization")]
  165. public async Task<bool> UpdateAuthorization(string id)
  166. {
  167. var data = await _db.Queryable<BPA_PlatformAuthorization>().FirstAsync(x => x.Id == id);
  168. if (data == null)
  169. {
  170. throw Oops.Oh("授权信息不存在");
  171. }
  172. data.Key = Guid.NewGuid().ToString();
  173. data.UpdateAt = DateTime.Now;
  174. //data.PeriodValidity = input.PeriodValidity;
  175. return await _db.Updateable(data).ExecuteCommandHasChangeAsync();
  176. }
  177. /// <summary>
  178. /// 修改授权码授权时间
  179. /// </summary>
  180. /// <param name="id"></param>
  181. /// <returns></returns>
  182. [HttpPost("/api/authorization/updateauthtime")]
  183. public async Task<bool> UpdateAuthTime(CreateOrUpDatePlatformAuthorizationDto input)
  184. {
  185. var data = await _db.Queryable<BPA_PlatformAuthorization>().FirstAsync(x => x.Id == input.Id);
  186. if (data == null)
  187. {
  188. throw Oops.Oh("授权信息不存在");
  189. }
  190. data.PeriodValidity = input.PeriodValidity;
  191. data.UpdateAt = DateTime.Now;
  192. return await _db.Updateable(data).ExecuteCommandHasChangeAsync();
  193. }
  194. /// <summary>
  195. /// 删除授权码
  196. /// </summary>
  197. /// <param name="id"></param>
  198. /// <returns></returns>
  199. [HttpPost("/api/authorization/delauthorization")]
  200. public async Task<bool> DelAuthorization(string id)
  201. {
  202. var data = await _db.Queryable<BPA_PlatformAuthorization>().FirstAsync(x => x.Id == id);
  203. if (data == null)
  204. {
  205. throw Oops.Oh("授权信息不存在");
  206. }
  207. return await _db.Deleteable(data).ExecuteCommandHasChangeAsync();
  208. }
  209. /// <summary>
  210. /// 获取加盟商信息
  211. /// </summary>
  212. /// <param name="id"></param>
  213. /// <returns></returns>
  214. [HttpGet("/api/authorization/GetCompanyByIdNew")]
  215. [AllowAnonymous]
  216. public async Task<BPA_Company> GetCompanyById(string id)
  217. {
  218. var data = await _db.Queryable<BPA_Company>().FirstAsync(x => x.Id == id);
  219. return data;
  220. }
  221. #endregion
  222. }
  223. }