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.
 
 
 

137 lines
5.0 KiB

using Shentun.WebPeis.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Users;
using Shentun.WebPeis.Wechats;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using System.Net;
using System.Security.Claims;
using System.Text.Json;
using Volo.Abp.Identity;
using Microsoft.AspNetCore.Identity;
using OpenIddict.Server.AspNetCore;
using Polly;
using Volo.Abp;
using Shentun.WebPeis.Enums;
using System.Net.Http.Headers;
using IdentityModel.Client;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;
using Microsoft.AspNetCore.Mvc;
namespace Shentun.WebPeis.Persons
{
public class PersonAppService : ApplicationService
{
private SignInManager<Volo.Abp.Identity.IdentityUser> _signInManager;
private readonly IConfiguration _configuration;
private readonly IRepository<Volo.Abp.Identity.IdentityUser, Guid> _identityUserRepository;
private readonly IdentityUserManager _userManager;
private readonly IRepository<Person> _repository;
public PersonAppService(IRepository<Person> repository,
IConfiguration configuration,
IRepository<Volo.Abp.Identity.IdentityUser, Guid> identityUserRepository,
IdentityUserManager userManager)
{
_repository = repository;
_configuration = configuration;
_identityUserRepository = identityUserRepository;
_userManager = userManager;
}
public async Task<PersonDto> GetByIdAsync(PersonIdInputDto input)
{
var entity = await _repository.GetAsync(o=>o.UserId == input.PersonId);
var entityDto = ObjectMapper.Map<Person, PersonDto>(entity);
return entityDto;
}
[HttpPost("api/app/Person/GetWechatUserTokenAsync")]
public async Task<UserTokenDto> GetWechatUserTokenAsync(WechatUserJsCodeInputDto input)
{
var wechatSession = await WeChatHelper.GetWechatSession(_configuration, input.JsCode);
if (wechatSession == null)
{
throw new Exception("微信会话返回空值");
}
if (wechatSession.ErrCode != 0)
{
throw new Exception("微信账户登陆失败");
}
var client = new HttpClient();
var weChatClientId = _configuration.GetSection("AuthServer").GetSection("WeChatClientId").Value;
var secret = _configuration.GetSection("AuthServer").GetSection("WeChatClientSecret").Value;
var dic = new Dictionary<string, object>
{
{"client_id",weChatClientId},
{"client_secret",secret},
{"grant_type",WeChatGrant.GrantType},
};
var tokenRequest = new TokenRequest()
{
ClientId = weChatClientId,
ClientSecret = secret,
GrantType = WeChatGrant.GrantType
};
var token = await client.RequestTokenAsync(tokenRequest);
if (token.HttpResponse != null && token.HttpResponse.StatusCode == HttpStatusCode.OK)
{
var userTokenDto = new UserTokenDto
{
AccessToken = token.AccessToken,
RefreshToken = token.RefreshToken
};
return userTokenDto;
}
else
{
//msg = new UserLoginDto { code = 1, msg = "登录成功", peisid = PeisId };
throw new UserFriendlyException("获取token失败");
}
//var dicStr = dic.Select(m => m.Key + "=" + m.Value).DefaultIfEmpty().Aggregate((m, n) => m + "&" + n);
//HttpContent httpContent = new StringContent(dicStr);
//httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
//var tokenResult = await client.PostAsync("connect/token", httpContent);
//var tokenResultStr = await tokenResult.Content.ReadAsStringAsync();
//if (tokenResult.IsSuccessStatusCode)
//{
// if (!string.IsNullOrEmpty(tokenResultStr))
// {
// dynamic signResult = JsonSerializer.Deserialize<object>(tokenResultStr);
// }
//}
//else
//{
// if (string.IsNullOrEmpty(tokenResultStr))
// throw new BusinessException(tokenResult.ReasonPhrase);
//}
//return tokenResultStr;
}
public async Task<PersonDto> CreateAsync(CreatePersonDto input)
{
var entity = ObjectMapper.Map<CreatePersonDto, Person>(input);
var entityDto = ObjectMapper.Map<Person, PersonDto>(entity);
return entityDto;
}
}
}