99 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			99 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 
								 | 
							
								using Microsoft.AspNetCore.Mvc;
							 | 
						||
| 
								 | 
							
								using Microsoft.AspNetCore.Authorization;
							 | 
						||
| 
								 | 
							
								using FutureMailAPI.Services;
							 | 
						||
| 
								 | 
							
								using FutureMailAPI.DTOs;
							 | 
						||
| 
								 | 
							
								using System.Security.Claims;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace FutureMailAPI.Controllers
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [ApiController]
							 | 
						||
| 
								 | 
							
								    [Route("api/v1/user")]
							 | 
						||
| 
								 | 
							
								    [Authorize]
							 | 
						||
| 
								 | 
							
								    public class UserController : ControllerBase
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        private readonly IPersonalSpaceService _personalSpaceService;
							 | 
						||
| 
								 | 
							
								        private readonly ILogger<UserController> _logger;
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        public UserController(IPersonalSpaceService personalSpaceService, ILogger<UserController> logger)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _personalSpaceService = personalSpaceService;
							 | 
						||
| 
								 | 
							
								            _logger = logger;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 获取用户订阅信息
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <returns>用户订阅信息</returns>
							 | 
						||
| 
								 | 
							
								        [HttpGet("subscription")]
							 | 
						||
| 
								 | 
							
								        public async Task<IActionResult> GetSubscription()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            try
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                var userId = GetCurrentUserId();
							 | 
						||
| 
								 | 
							
								                if (userId <= 0)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                var result = await _personalSpaceService.GetSubscriptionAsync(userId);
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                if (result.Success)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Ok(result);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                return BadRequest(result);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _logger.LogError(ex, "获取订阅信息时发生错误");
							 | 
						||
| 
								 | 
							
								                return StatusCode(500, ApiResponse<SubscriptionResponseDto>.ErrorResult("服务器内部错误"));
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 获取用户资料
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <returns>用户资料</returns>
							 | 
						||
| 
								 | 
							
								        [HttpGet("profile")]
							 | 
						||
| 
								 | 
							
								        public async Task<IActionResult> GetUserProfile()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            try
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                var userId = GetCurrentUserId();
							 | 
						||
| 
								 | 
							
								                if (userId <= 0)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                var result = await _personalSpaceService.GetUserProfileAsync(userId);
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                if (result.Success)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Ok(result);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                return BadRequest(result);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _logger.LogError(ex, "获取用户资料时发生错误");
							 | 
						||
| 
								 | 
							
								                return StatusCode(500, ApiResponse<UserProfileResponseDto>.ErrorResult("服务器内部错误"));
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 从JWT令牌中获取当前用户ID
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <returns>用户ID</returns>
							 | 
						||
| 
								 | 
							
								        private int GetCurrentUserId()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
							 | 
						||
| 
								 | 
							
								            if (userIdClaim != null && int.TryParse(userIdClaim.Value, out int userId))
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                return userId;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            return 0;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |