AGSSbackend/AGSS/Controllers/User/UserControllers.cs

46 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.IdentityModel.Tokens.Jwt;
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]/[action]")]
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(JwtRegisteredClaimNames.Sub)!.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);
}
}
}