110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
using F9WebAiTest.Application.Common;
|
|
using F9WebAiTest.Application.Dtos;
|
|
using F9WebAiTest.Application.Dtos.Auth;
|
|
using F9WebAiTest.Application.Services;
|
|
using F9WebAiTest.Application.Services.Auth;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace F9WebAiTest.Api.Controllers.Auth;
|
|
|
|
[ApiController]
|
|
[Route("api/auth")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
[HttpPost("login")]
|
|
public async Task<IActionResult> Login([FromBody] LoginDto dto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _authService.LoginAsync(dto);
|
|
return Ok(ApiResult<LoginResultDto>.Ok(result, "登录成功"));
|
|
}
|
|
catch (ServiceException ex)
|
|
{
|
|
return StatusCode(ex.Code, ApiResult<object>.Fail(ex.Message, ex.Code));
|
|
}
|
|
}
|
|
|
|
[HttpPost("refresh")]
|
|
public async Task<IActionResult> Refresh([FromBody] RefreshTokenDto dto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _authService.RefreshTokenAsync(dto.RefreshToken);
|
|
return Ok(ApiResult<LoginResultDto>.Ok(result, "刷新成功"));
|
|
}
|
|
catch (ServiceException ex)
|
|
{
|
|
return StatusCode(ex.Code, ApiResult<object>.Fail(ex.Message, ex.Code));
|
|
}
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPost("logout")]
|
|
public async Task<IActionResult> Logout([FromBody] RefreshTokenDto dto)
|
|
{
|
|
await _authService.RevokeRefreshTokenAsync(dto.RefreshToken);
|
|
return Ok(ApiResult<object>.Ok(null, "已退出登录"));
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("me")]
|
|
public async Task<IActionResult> GetCurrentUser()
|
|
{
|
|
var id = GetUserId();
|
|
var user = await _authService.GetCurrentUserAsync(id);
|
|
return Ok(ApiResult<EmployeeInfoDto>.Ok(user));
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("menus")]
|
|
public async Task<IActionResult> GetMenus()
|
|
{
|
|
var id = GetUserId();
|
|
var menus = await _authService.GetUserMenusAsync(id);
|
|
return Ok(ApiResult<List<MenuDto>>.Ok(menus));
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet("permissions")]
|
|
public async Task<IActionResult> GetPermissions()
|
|
{
|
|
var id = GetUserId();
|
|
var perms = await _authService.GetUserPermissionsAsync(id);
|
|
return Ok(ApiResult<List<string>>.Ok(perms));
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPut("password")]
|
|
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordDto dto)
|
|
{
|
|
try
|
|
{
|
|
await _authService.ChangePasswordAsync(GetUserId(), dto);
|
|
return Ok(ApiResult<object>.Ok(null, "密码修改成功"));
|
|
}
|
|
catch (ServiceException ex)
|
|
{
|
|
return StatusCode(ex.Code, ApiResult<object>.Fail(ex.Message, ex.Code));
|
|
}
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpPut("profile")]
|
|
public async Task<IActionResult> UpdateProfile([FromBody] UpdateProfileDto dto)
|
|
{
|
|
await _authService.UpdateProfileAsync(GetUserId(), dto);
|
|
return Ok(ApiResult<object>.Ok(null, "资料更新成功"));
|
|
}
|
|
|
|
private long GetUserId() => long.Parse(User.FindFirst("id")!.Value);
|
|
}
|