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.
227 lines
7.4 KiB
227 lines
7.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using DocumentFormat.OpenXml.Drawing.Charts;
|
|
using IdentityModel.Client;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Volo.Abp.Account;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Win_in.Sfs.Auth.Application.Contracts;
|
|
using Win_in.Sfs.Wms.Pda.Authenticaitons;
|
|
using Win_in.Sfs.Wms.Pda.Models;
|
|
using PdaMenuDto = Win_in.Sfs.Wms.Pda.Models.PdaMenuDto;
|
|
using PdaMenuGroupDto = Win_in.Sfs.Wms.Pda.Models.PdaMenuGroupDto;
|
|
|
|
namespace Win_in.Sfs.Wms.Pda.Controllers.Accounts;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[AllowAnonymous]
|
|
[ApiController]
|
|
[Route($"{PdaHostConst.ROOT_ROUTE}account")]
|
|
|
|
public class AccountController : AbpController
|
|
{
|
|
private readonly IProfileAppService _profileAppService;
|
|
private readonly ITokenService _tokenService;
|
|
private readonly IUserMenuAppService _userMenuAppService;
|
|
private readonly IUserWorkGroupAppService _userWorkGroupAppService;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly ILogger<TokenService> _logger;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="profileAppService"></param>
|
|
/// <param name="tokenService"></param>
|
|
/// <param name="userMenuAppService"></param>
|
|
/// <param name="userWorkGroupAppService"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="httpClientFactory"></param>
|
|
/// <param name="logger"></param>
|
|
public AccountController(IProfileAppService profileAppService
|
|
, ITokenService tokenService
|
|
, IUserMenuAppService userMenuAppService
|
|
, IUserWorkGroupAppService userWorkGroupAppService
|
|
, IConfiguration configuration, IHttpClientFactory httpClientFactory, ILogger<TokenService> logger)
|
|
{
|
|
_profileAppService = profileAppService;
|
|
_tokenService = tokenService;
|
|
_userMenuAppService = userMenuAppService;
|
|
_userWorkGroupAppService = userWorkGroupAppService;
|
|
_configuration = configuration;
|
|
_httpClientFactory = httpClientFactory;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Display]
|
|
public class LoginModel
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Display]
|
|
[Required]
|
|
public string UserName { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Display]
|
|
[Required]
|
|
public string Password { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 登录
|
|
/// </summary>
|
|
/// <param name="userLoginInput"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("login")]
|
|
public virtual async Task<BaererToken> LoginAsync(UserLoginInput userLoginInput)
|
|
{
|
|
|
|
var address = _configuration["AuthServer:Authority"];
|
|
var request = new DiscoveryDocumentRequest
|
|
{
|
|
Address = address,
|
|
Policy = new DiscoveryPolicy { RequireHttps = false }
|
|
};
|
|
var discovery = await _httpClientFactory.CreateClient().GetDiscoveryDocumentAsync(request).ConfigureAwait(false);
|
|
var clientId = _configuration["AuthServer:ClientId"];
|
|
var clientSecret = _configuration["AuthServer:ClientSecret"];
|
|
this._logger.LogInformation($"address:{address},TokenEndpoint:{discovery.TokenEndpoint},clientId:{clientId},clientSecret:{clientSecret}");
|
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest
|
|
{
|
|
Address = $"{address.TrimEnd('/')}/connect/token",
|
|
GrantType = "password",
|
|
ClientId = clientId,
|
|
ClientSecret = clientSecret,
|
|
UserName = userLoginInput.Username,
|
|
Password = userLoginInput.Password
|
|
}).ConfigureAwait(false);
|
|
Console.WriteLine($"Result:${(result.IsError ? result.ErrorDescription : result.AccessToken)}");
|
|
|
|
return new BaererToken()
|
|
{
|
|
refresh_token = result.RefreshToken,
|
|
access_token = result.AccessToken,
|
|
scope = result.Scope,
|
|
token_type = result.TokenType,
|
|
expires_in = result.ExpiresIn,
|
|
};
|
|
|
|
//return new JsonResult(new
|
|
//{
|
|
// result.TokenType,
|
|
// result.AccessToken,
|
|
// result.ExpiresIn,
|
|
// result.RefreshToken,
|
|
// result.Scope,
|
|
// result.HttpStatusCode,
|
|
// result.Error,
|
|
// result.HttpErrorReason,
|
|
// result.ErrorDescription,
|
|
// result.ErrorType,
|
|
// result.Exception?.Message,
|
|
// Exception = result.Exception?.ToString()
|
|
//});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 个人配置
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("profile")]
|
|
public virtual async Task<ProfileDto> GetProfileAsync()
|
|
{
|
|
return await _profileAppService.GetAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改个人配置
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("profile")]
|
|
public virtual async Task<ProfileDto> UpdateProfileAsync(UpdateProfileDto dto)
|
|
{
|
|
return await _profileAppService.UpdateAsync(dto).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改密码
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("change-password")]
|
|
public virtual async Task ChangePasswordAsync(ChangePasswordInput input)
|
|
{
|
|
await _profileAppService.ChangePasswordAsync(input).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 菜单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("menus/{userId}")]
|
|
public virtual async Task<List<PdaMenuGroupDto>> GetMenusAsync(Guid userId)
|
|
{
|
|
//var userId = CurrentUser.Id;
|
|
var pdaMenuGroupDtos = new List<PdaMenuGroupDto>();//返回给pda的菜单 已排序
|
|
var menusOfUser = await _userMenuAppService.GetPdaMenusOfUserAsync(userId).ConfigureAwait(false);
|
|
var menuGroups = menusOfUser.GroupBy(p => p.GroupName);
|
|
foreach (var menuGroup in menuGroups)
|
|
{
|
|
var pdaMenuDtos = new List<PdaMenuDto>();
|
|
var groupSort = 0;
|
|
var groupTitle = "";
|
|
foreach (var menuDto in menuGroup.OrderBy(p => p.Sort))
|
|
{
|
|
var pdaMenuDto = new PdaMenuDto
|
|
{
|
|
name = menuDto.Name,
|
|
path = menuDto.Route,
|
|
url = menuDto.Icon,
|
|
countUrl = menuDto.CountUrl
|
|
};
|
|
groupSort = menuDto.GroupSort;
|
|
groupTitle = menuDto.GroupName;
|
|
pdaMenuDtos.Add(pdaMenuDto);
|
|
}
|
|
pdaMenuGroupDtos.Add(new PdaMenuGroupDto
|
|
{
|
|
groupSort = groupSort,
|
|
title = groupTitle,
|
|
list = pdaMenuDtos
|
|
});
|
|
}
|
|
|
|
return pdaMenuGroupDtos.OrderBy(p => p.groupSort).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 工作组
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("workgroups")]
|
|
public virtual async Task<List<string>> GetWorkGroupsAsync()
|
|
{
|
|
|
|
var userWorkGroupDtos =
|
|
await _userWorkGroupAppService.GetCodsOfCurrentUserAsync().ConfigureAwait(false);
|
|
|
|
return userWorkGroupDtos;
|
|
}
|
|
}
|
|
|