45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
|
using System.Security.Claims;
|
|||
|
using AGSS.Models.Entities;
|
|||
|
using AGSS.Models.Template;
|
|||
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
using Microsoft.AspNetCore.Identity;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
|||
|
namespace AGSS.Controllers.User;
|
|||
|
|
|||
|
[Authorize]
|
|||
|
[Route("api/v1/[controller]")]
|
|||
|
public class UserControllers:ControllerBase
|
|||
|
{
|
|||
|
|
|||
|
private readonly UserService _userService;
|
|||
|
|
|||
|
public UserControllers(UserService userService, UserManager<UserModel> userManager)
|
|||
|
{
|
|||
|
_userService = userService;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
[HttpGet]
|
|||
|
public async Task<IActionResult> My()
|
|||
|
{
|
|||
|
string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|||
|
if (string.IsNullOrEmpty(userId))
|
|||
|
{
|
|||
|
return Ok(new ReturnTemplate(500,"获取用户失败(JWT解析错误)",null));
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var userProfile = await _userService.GetUserProfileAsync(userId);
|
|||
|
return Ok(new ReturnTemplate(200,"获取成功!",userProfile));
|
|||
|
}
|
|||
|
catch (ArgumentException ex)
|
|||
|
{
|
|||
|
return NotFound(ex.Message);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|