105 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			105 lines
		
	
	
		
			3.5 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/notification")]
							 | 
						||
| 
								 | 
							
								    [Authorize]
							 | 
						||
| 
								 | 
							
								    public class NotificationController : ControllerBase
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        private readonly INotificationService _notificationService;
							 | 
						||
| 
								 | 
							
								        private readonly ILogger<NotificationController> _logger;
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        public NotificationController(INotificationService notificationService, ILogger<NotificationController> logger)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _notificationService = notificationService;
							 | 
						||
| 
								 | 
							
								            _logger = logger;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 注册设备
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="request">设备注册请求</param>
							 | 
						||
| 
								 | 
							
								        /// <returns>注册结果</returns>
							 | 
						||
| 
								 | 
							
								        [HttpPost("device")]
							 | 
						||
| 
								 | 
							
								        public async Task<IActionResult> RegisterDevice([FromBody] NotificationDeviceRequestDto request)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            try
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                var userId = GetCurrentUserId();
							 | 
						||
| 
								 | 
							
								                if (userId <= 0)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                if (string.IsNullOrEmpty(request.DeviceType) || string.IsNullOrEmpty(request.DeviceToken))
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return BadRequest(ApiResponse<object>.ErrorResult("设备类型和设备令牌不能为空"));
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                var result = await _notificationService.RegisterDeviceAsync(userId, request);
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                if (result.Success)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Ok(result);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                return BadRequest(result);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _logger.LogError(ex, "注册设备时发生错误");
							 | 
						||
| 
								 | 
							
								                return StatusCode(500, ApiResponse<NotificationDeviceResponseDto>.ErrorResult("服务器内部错误"));
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 获取通知设置
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <returns>通知设置</returns>
							 | 
						||
| 
								 | 
							
								        [HttpGet("settings")]
							 | 
						||
| 
								 | 
							
								        public async Task<IActionResult> GetNotificationSettings()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            try
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                var userId = GetCurrentUserId();
							 | 
						||
| 
								 | 
							
								                if (userId <= 0)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                var result = await _notificationService.GetNotificationSettingsAsync(userId);
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                if (result.Success)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    return Ok(result);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                
							 | 
						||
| 
								 | 
							
								                return BadRequest(result);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            catch (Exception ex)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _logger.LogError(ex, "获取通知设置时发生错误");
							 | 
						||
| 
								 | 
							
								                return StatusCode(500, ApiResponse<NotificationSettingsDto>.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;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |