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 _signInManager; private readonly IConfiguration _configuration; private readonly IRepository _identityUserRepository; private readonly IdentityUserManager _userManager; private readonly IRepository _repository; public PersonAppService(IRepository repository, IConfiguration configuration, IRepository identityUserRepository, IdentityUserManager userManager) { _repository = repository; _configuration = configuration; _identityUserRepository = identityUserRepository; _userManager = userManager; } public async Task GetByIdAsync(PersonIdInputDto input) { var entity = await _repository.GetAsync(o=>o.UserId == input.PersonId); var entityDto = ObjectMapper.Map(entity); return entityDto; } [HttpPost("api/app/Person/GetWechatUserTokenAsync")] public async Task 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 { {"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(tokenResultStr); // } //} //else //{ // if (string.IsNullOrEmpty(tokenResultStr)) // throw new BusinessException(tokenResult.ReasonPhrase); //} //return tokenResultStr; } public async Task CreateAsync(CreatePersonDto input) { var entity = ObjectMapper.Map(input); var entityDto = ObjectMapper.Map(entity); return entityDto; } } }