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
188 lines
6.4 KiB
using Hangfire.States;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using OpenIddict.Abstractions;
|
|
using OpenIddict.Server.AspNetCore;
|
|
using Shentun.Peis.Models;
|
|
using Shentun.Peis.ThirdUsers;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.OpenIddict.Controllers;
|
|
using Volo.Abp.Uow;
|
|
|
|
namespace Shentun.Peis.Controllers
|
|
{
|
|
|
|
/// <summary>
|
|
/// 小程序登录
|
|
/// </summary>
|
|
[Route("/connect/token", Order = -1)]
|
|
public class MiniProgramTokenController : TokenController
|
|
{
|
|
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IRepository<ThirdUser, Guid> _thirdUserRepository;
|
|
private readonly SignInManager<Volo.Abp.Identity.IdentityUser> _signInManager;
|
|
private readonly IRepository<Volo.Abp.Identity.IdentityUser, Guid> _identityUserRepository;
|
|
|
|
|
|
public MiniProgramTokenController(
|
|
IConfiguration configuration,
|
|
IRepository<ThirdUser, Guid> thirdUserRepository,
|
|
SignInManager<Volo.Abp.Identity.IdentityUser> signInManager,
|
|
IRepository<Volo.Abp.Identity.IdentityUser, Guid> identityUserRepository)
|
|
{
|
|
_configuration = configuration;
|
|
_thirdUserRepository = thirdUserRepository;
|
|
_signInManager = signInManager;
|
|
_identityUserRepository = identityUserRepository;
|
|
}
|
|
|
|
|
|
|
|
public override async Task<IActionResult> HandleAsync()
|
|
{
|
|
//MiniProgram
|
|
var request = await GetOpenIddictServerRequestAsync(HttpContext);
|
|
|
|
string grantType = request.GrantType;
|
|
|
|
if (grantType == "mini_program")
|
|
{
|
|
var resultDto = new ThirdLoginDto();
|
|
|
|
|
|
//小程序登录
|
|
|
|
// 获取小程序 code 并换取 openid
|
|
var jsCode = request.GetParameter("jsCode").ToString();
|
|
var mobilePhone = request.GetParameter("mobile_phone").ToString();
|
|
|
|
var wechatSession = await WeChatHelper.GetWechatSession(_configuration, jsCode);
|
|
|
|
var thirdUserEnt = await _thirdUserRepository.FirstOrDefaultAsync(f => f.WechatOpenId == wechatSession.OpenId);
|
|
|
|
if (thirdUserEnt == null)
|
|
{
|
|
//未注册 注册信息
|
|
thirdUserEnt = new ThirdUser
|
|
{
|
|
AbpUserId = null,
|
|
IsActive = 'Y',
|
|
MobilePhone = mobilePhone,
|
|
UserRegisterFlag = '0',
|
|
WechatOpenId = wechatSession.OpenId
|
|
};
|
|
|
|
await _thirdUserRepository.InsertAsync(thirdUserEnt, true);
|
|
|
|
resultDto = new ThirdLoginDto
|
|
{
|
|
IsToken = "N",
|
|
Message = "用户未授权",
|
|
OpenId = wechatSession.OpenId
|
|
};
|
|
|
|
return Ok(resultDto);
|
|
|
|
}
|
|
else if (thirdUserEnt.IsActive == 'N')
|
|
{
|
|
resultDto = new ThirdLoginDto
|
|
{
|
|
IsToken = "N",
|
|
Message = "用户被禁用",
|
|
OpenId = wechatSession.OpenId
|
|
};
|
|
|
|
return Ok(resultDto);
|
|
}
|
|
else if (thirdUserEnt.AbpUserId == null)
|
|
{
|
|
resultDto = new ThirdLoginDto
|
|
{
|
|
IsToken = "N",
|
|
Message = "用户未授权",
|
|
OpenId = wechatSession.OpenId
|
|
};
|
|
|
|
return Ok(resultDto);
|
|
}
|
|
|
|
|
|
// 查询关联的 AbpUser
|
|
var abpUser = await _identityUserRepository.FirstOrDefaultAsync(f => f.Id == thirdUserEnt.AbpUserId);
|
|
if (abpUser == null)
|
|
{
|
|
resultDto = new ThirdLoginDto
|
|
{
|
|
IsToken = "N",
|
|
Message = "用户未关联权限",
|
|
OpenId = wechatSession.OpenId
|
|
};
|
|
|
|
return Ok(resultDto);
|
|
}
|
|
|
|
// 生成声明主体
|
|
var principal = await _signInManager.CreateUserPrincipalAsync(abpUser);
|
|
|
|
var scopes = request.GetScopes();
|
|
var resources = await GetResourcesAsync(scopes);
|
|
principal.SetScopes(scopes);
|
|
principal.SetResources(resources);
|
|
|
|
await SetClaimsDestinationsAsync(principal);
|
|
|
|
return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
|
|
|
}
|
|
//else if (grantType == "phone_verify")
|
|
//{
|
|
// //手机号+验证码登录认证
|
|
|
|
// var mobilePhone = request.GetParameter("mobilePhone").ToString();
|
|
// var verifyCode = request.GetParameter("verifyCode").ToString();
|
|
|
|
|
|
|
|
// var thirdUserEnt = await _thirdUserRepository.FirstOrDefaultAsync(f => f.MobilePhone == mobilePhone && f.IsActive == 'Y' && f.AbpUserId != null);
|
|
// if (thirdUserEnt == null)
|
|
// {
|
|
// throw new UserFriendlyException("用户未授权");
|
|
// }
|
|
// // 查询关联的 AbpUser
|
|
// var abpUser = await _identityUserRepository.FirstOrDefaultAsync(f => f.Id == thirdUserEnt.AbpUserId);
|
|
// if (abpUser == null)
|
|
// {
|
|
// throw new UserFriendlyException("用户未关联权限");
|
|
// }
|
|
|
|
// // 生成声明主体
|
|
// var principal = await _signInManager.CreateUserPrincipalAsync(abpUser);
|
|
|
|
// var scopes = request.GetScopes();
|
|
// var resources = await GetResourcesAsync(scopes);
|
|
// principal.SetScopes(scopes);
|
|
// principal.SetResources(resources);
|
|
|
|
// await SetClaimsDestinationsAsync(principal);
|
|
|
|
// return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
|
//}
|
|
else
|
|
{
|
|
return await base.HandleAsync();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|