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.

188 lines
6.4 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
  1. using Hangfire.States;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using OpenIddict.Abstractions;
  6. using OpenIddict.Server.AspNetCore;
  7. using Shentun.Peis.Models;
  8. using Shentun.Peis.ThirdUsers;
  9. using System;
  10. using System.Threading.Tasks;
  11. using Volo.Abp;
  12. using Volo.Abp.Domain.Repositories;
  13. using Volo.Abp.Identity;
  14. using Volo.Abp.OpenIddict.Controllers;
  15. using Volo.Abp.Uow;
  16. namespace Shentun.Peis.Controllers
  17. {
  18. /// <summary>
  19. /// 小程序登录
  20. /// </summary>
  21. [Route("/connect/token", Order = -1)]
  22. public class MiniProgramTokenController : TokenController
  23. {
  24. private readonly IConfiguration _configuration;
  25. private readonly IRepository<ThirdUser, Guid> _thirdUserRepository;
  26. private readonly SignInManager<Volo.Abp.Identity.IdentityUser> _signInManager;
  27. private readonly IRepository<Volo.Abp.Identity.IdentityUser, Guid> _identityUserRepository;
  28. public MiniProgramTokenController(
  29. IConfiguration configuration,
  30. IRepository<ThirdUser, Guid> thirdUserRepository,
  31. SignInManager<Volo.Abp.Identity.IdentityUser> signInManager,
  32. IRepository<Volo.Abp.Identity.IdentityUser, Guid> identityUserRepository)
  33. {
  34. _configuration = configuration;
  35. _thirdUserRepository = thirdUserRepository;
  36. _signInManager = signInManager;
  37. _identityUserRepository = identityUserRepository;
  38. }
  39. public override async Task<IActionResult> HandleAsync()
  40. {
  41. //MiniProgram
  42. var request = await GetOpenIddictServerRequestAsync(HttpContext);
  43. string grantType = request.GrantType;
  44. if (grantType == "mini_program")
  45. {
  46. var resultDto = new ThirdLoginDto();
  47. //小程序登录
  48. // 获取小程序 code 并换取 openid
  49. var jsCode = request.GetParameter("jsCode").ToString();
  50. var mobilePhone = request.GetParameter("mobile_phone").ToString();
  51. var wechatSession = await WeChatHelper.GetWechatSession(_configuration, jsCode);
  52. var thirdUserEnt = await _thirdUserRepository.FirstOrDefaultAsync(f => f.WechatOpenId == wechatSession.OpenId);
  53. if (thirdUserEnt == null)
  54. {
  55. //未注册 注册信息
  56. thirdUserEnt = new ThirdUser
  57. {
  58. AbpUserId = null,
  59. IsActive = 'Y',
  60. MobilePhone = mobilePhone,
  61. UserRegisterFlag = '0',
  62. WechatOpenId = wechatSession.OpenId
  63. };
  64. await _thirdUserRepository.InsertAsync(thirdUserEnt, true);
  65. resultDto = new ThirdLoginDto
  66. {
  67. IsToken = "N",
  68. Message = "用户未授权",
  69. OpenId = wechatSession.OpenId
  70. };
  71. return Ok(resultDto);
  72. }
  73. else if (thirdUserEnt.IsActive == 'N')
  74. {
  75. resultDto = new ThirdLoginDto
  76. {
  77. IsToken = "N",
  78. Message = "用户被禁用",
  79. OpenId = wechatSession.OpenId
  80. };
  81. return Ok(resultDto);
  82. }
  83. else if (thirdUserEnt.AbpUserId == null)
  84. {
  85. resultDto = new ThirdLoginDto
  86. {
  87. IsToken = "N",
  88. Message = "用户未授权",
  89. OpenId = wechatSession.OpenId
  90. };
  91. return Ok(resultDto);
  92. }
  93. // 查询关联的 AbpUser
  94. var abpUser = await _identityUserRepository.FirstOrDefaultAsync(f => f.Id == thirdUserEnt.AbpUserId);
  95. if (abpUser == null)
  96. {
  97. resultDto = new ThirdLoginDto
  98. {
  99. IsToken = "N",
  100. Message = "用户未关联权限",
  101. OpenId = wechatSession.OpenId
  102. };
  103. return Ok(resultDto);
  104. }
  105. // 生成声明主体
  106. var principal = await _signInManager.CreateUserPrincipalAsync(abpUser);
  107. var scopes = request.GetScopes();
  108. var resources = await GetResourcesAsync(scopes);
  109. principal.SetScopes(scopes);
  110. principal.SetResources(resources);
  111. await SetClaimsDestinationsAsync(principal);
  112. return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
  113. }
  114. //else if (grantType == "phone_verify")
  115. //{
  116. // //手机号+验证码登录认证
  117. // var mobilePhone = request.GetParameter("mobilePhone").ToString();
  118. // var verifyCode = request.GetParameter("verifyCode").ToString();
  119. // var thirdUserEnt = await _thirdUserRepository.FirstOrDefaultAsync(f => f.MobilePhone == mobilePhone && f.IsActive == 'Y' && f.AbpUserId != null);
  120. // if (thirdUserEnt == null)
  121. // {
  122. // throw new UserFriendlyException("用户未授权");
  123. // }
  124. // // 查询关联的 AbpUser
  125. // var abpUser = await _identityUserRepository.FirstOrDefaultAsync(f => f.Id == thirdUserEnt.AbpUserId);
  126. // if (abpUser == null)
  127. // {
  128. // throw new UserFriendlyException("用户未关联权限");
  129. // }
  130. // // 生成声明主体
  131. // var principal = await _signInManager.CreateUserPrincipalAsync(abpUser);
  132. // var scopes = request.GetScopes();
  133. // var resources = await GetResourcesAsync(scopes);
  134. // principal.SetScopes(scopes);
  135. // principal.SetResources(resources);
  136. // await SetClaimsDestinationsAsync(principal);
  137. // return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
  138. //}
  139. else
  140. {
  141. return await base.HandleAsync();
  142. }
  143. }
  144. }
  145. }