115 lines
4.1 KiB
C#
115 lines
4.1 KiB
C#
using IM_API.Dtos;
|
|
using IM_API.Dtos.User;
|
|
using IM_API.Interface.Services;
|
|
using IM_API.Tools;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
|
|
namespace IM_API.Controllers
|
|
{
|
|
[Authorize]
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class UserController : ControllerBase
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly ILogger<UserController> _logger;
|
|
public UserController(IUserService userService, ILogger<UserController> logger)
|
|
{
|
|
_userService = userService;
|
|
_logger = logger;
|
|
}
|
|
/// <summary>
|
|
/// 获取当前用户信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> Me()
|
|
{
|
|
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
int userId = int.Parse(userIdStr);
|
|
var userinfo = await _userService.GetUserInfoAsync(userId);
|
|
var res = new BaseResponse<UserInfoDto>(userinfo);
|
|
return Ok(res);
|
|
}
|
|
/// <summary>
|
|
/// 修改用户资料
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> Profile(UpdateUserDto dto)
|
|
{
|
|
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
int userId = int.Parse(userIdStr);
|
|
var userinfo = await _userService.UpdateUserAsync(userId, dto);
|
|
var res = new BaseResponse<UserInfoDto>(userinfo);
|
|
return Ok(res);
|
|
|
|
}
|
|
/// <summary>
|
|
/// ID查询用户
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> Find(int userId)
|
|
{
|
|
var userinfo = await _userService.GetUserInfoAsync(userId);
|
|
var res = new BaseResponse<UserInfoDto>(userinfo);
|
|
return Ok(res);
|
|
}
|
|
/// <summary>
|
|
/// 用户名查询用户
|
|
/// </summary>
|
|
/// <param name="username"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> FindByUsername(string username)
|
|
{
|
|
var userinfo = await _userService.GetUserInfoByUsernameAsync(username);
|
|
var res = new BaseResponse<UserInfoDto>(userinfo);
|
|
return Ok(res);
|
|
}
|
|
/// <summary>
|
|
/// 重置用户密码
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> ResetPassword(PasswordResetDto dto)
|
|
{
|
|
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
int userId = int.Parse(userIdStr);
|
|
await _userService.ResetPasswordAsync(userId, dto.OldPassword, dto.Password);
|
|
return Ok(new BaseResponse<object?>());
|
|
}
|
|
/// <summary>
|
|
/// 设置在线状态
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> SetOnlineStatus(OnlineStatusSetDto dto)
|
|
{
|
|
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
int userId = int.Parse(userIdStr);
|
|
await _userService.UpdateOlineStatusAsync(userId, dto.OnlineStatus);
|
|
return Ok(new BaseResponse<object?>());
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(BaseResponse<List<UserInfoDto>>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetUserList([FromBody][Required]List<int> ids)
|
|
{
|
|
var users = await _userService.GetUserInfoListAsync(ids);
|
|
var res = new BaseResponse<List<UserInfoDto>>(users);
|
|
return Ok(res);
|
|
}
|
|
}
|
|
}
|