91 lines
3.1 KiB
C#
91 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/notification")]
|
|
|
|
public class NotificationController : BaseController
|
|
{
|
|
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("服务器内部错误"));
|
|
}
|
|
}
|
|
}
|
|
} |