554 lines
20 KiB
C#
554 lines
20 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using FutureMailAPI.DTOs;
|
|
using FutureMailAPI.Services;
|
|
|
|
namespace FutureMailAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/mails")]
|
|
public class MailsController : BaseController
|
|
{
|
|
private readonly IMailService _mailService;
|
|
private readonly ILogger<MailsController> _logger;
|
|
|
|
public MailsController(IMailService mailService, ILogger<MailsController> logger)
|
|
{
|
|
_mailService = mailService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateMail([FromBody] SentMailCreateDto createDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.CreateMailAsync(currentUserId, createDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetMail),
|
|
new { mailId = result.Data!.Id },
|
|
result);
|
|
}
|
|
|
|
// 兼容前端请求格式的创建邮件接口
|
|
[HttpPost("create")]
|
|
public async Task<IActionResult> CreateMailCompat([FromBody] SentMailCreateCompatDto createDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
// 转换为内部DTO
|
|
var internalDto = createDto.ToInternalDto();
|
|
|
|
var result = await _mailService.CreateMailAsync(currentUserId, internalDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetMail),
|
|
new { mailId = result.Data!.Id },
|
|
result);
|
|
}
|
|
|
|
// 直接接收前端原始格式的创建邮件接口
|
|
[HttpPost("create-raw")]
|
|
public async Task<IActionResult> CreateMailRaw()
|
|
{
|
|
try
|
|
{
|
|
// 读取请求体
|
|
var request = HttpContext.Request;
|
|
using var reader = new StreamReader(request.Body);
|
|
var body = await reader.ReadToEndAsync();
|
|
|
|
// 解析JSON
|
|
var rawMail = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(body);
|
|
if (rawMail == null)
|
|
{
|
|
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("请求数据为空"));
|
|
}
|
|
|
|
// 创建兼容DTO
|
|
var compatDto = new SentMailCreateCompatDto();
|
|
|
|
// 解析各个字段
|
|
if (rawMail.ContainsKey("title") && rawMail["title"] != null)
|
|
compatDto.title = rawMail["title"].ToString() ?? string.Empty;
|
|
|
|
if (rawMail.ContainsKey("content") && rawMail["content"] != null)
|
|
compatDto.content = rawMail["content"].ToString() ?? string.Empty;
|
|
|
|
if (rawMail.ContainsKey("recipientType") && rawMail["recipientType"] != null)
|
|
{
|
|
var recipientTypeStr = rawMail["recipientType"].ToString();
|
|
if (Enum.TryParse<RecipientTypeEnum>(recipientTypeStr, true, out var recipientType))
|
|
compatDto.recipientType = recipientType;
|
|
}
|
|
|
|
if (rawMail.ContainsKey("recipientEmail") && rawMail["recipientEmail"] != null)
|
|
compatDto.recipientEmail = rawMail["recipientEmail"].ToString();
|
|
|
|
if (rawMail.ContainsKey("sendTime") && rawMail["sendTime"] != null)
|
|
{
|
|
if (DateTime.TryParse(rawMail["sendTime"].ToString(), out var sendTime))
|
|
compatDto.sendTime = sendTime;
|
|
}
|
|
|
|
if (rawMail.ContainsKey("triggerType") && rawMail["triggerType"] != null)
|
|
{
|
|
var triggerTypeStr = rawMail["triggerType"].ToString();
|
|
if (Enum.TryParse<TriggerTypeEnum>(triggerTypeStr, true, out var triggerType))
|
|
compatDto.triggerType = triggerType;
|
|
}
|
|
|
|
if (rawMail.ContainsKey("triggerCondition"))
|
|
compatDto.triggerCondition = rawMail["triggerCondition"];
|
|
|
|
if (rawMail.ContainsKey("attachments"))
|
|
{
|
|
try
|
|
{
|
|
compatDto.attachments = System.Text.Json.JsonSerializer.Deserialize<List<object>>(rawMail["attachments"].ToString());
|
|
}
|
|
catch
|
|
{
|
|
compatDto.attachments = new List<object>();
|
|
}
|
|
}
|
|
|
|
if (rawMail.ContainsKey("isEncrypted"))
|
|
compatDto.isEncrypted = bool.Parse(rawMail["isEncrypted"].ToString());
|
|
|
|
if (rawMail.ContainsKey("capsuleStyle"))
|
|
compatDto.capsuleStyle = rawMail["capsuleStyle"].ToString() ?? "default";
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
// 转换为内部DTO
|
|
var internalDto = compatDto.ToInternalDto();
|
|
|
|
var result = await _mailService.CreateMailAsync(currentUserId, internalDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetMail),
|
|
new { mailId = result.Data!.Id },
|
|
result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "创建邮件时发生错误");
|
|
return StatusCode(500, ApiResponse<SentMailResponseDto>.ErrorResult("服务器内部错误"));
|
|
}
|
|
}
|
|
|
|
[HttpGet("{mailId}")]
|
|
public async Task<IActionResult> GetMail(int mailId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetSentMailByIdAsync(currentUserId, mailId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return NotFound(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetMails([FromQuery] MailListQueryDto queryDto)
|
|
{
|
|
try
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetSentMailsAsync(currentUserId, queryDto);
|
|
|
|
if (result.Success)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
|
|
return BadRequest(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取邮件列表时发生错误");
|
|
return StatusCode(500, ApiResponse<PagedResponse<SentMailResponseDto>>.ErrorResult("服务器内部错误"));
|
|
}
|
|
}
|
|
|
|
[HttpPut("{mailId}")]
|
|
public async Task<IActionResult> UpdateMail(int mailId, [FromBody] SentMailUpdateDto updateDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<SentMailResponseDto>.ErrorResult("输入数据无效"));
|
|
}
|
|
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SentMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.UpdateMailAsync(currentUserId, mailId, updateDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{mailId}")]
|
|
public async Task<IActionResult> DeleteMail(int mailId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.DeleteMailAsync(currentUserId, mailId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("received")]
|
|
public async Task<IActionResult> GetReceivedMails([FromQuery] MailListQueryDto queryDto)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<PagedResponse<ReceivedMailResponseDto>>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetReceivedMailsAsync(currentUserId, queryDto);
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("received/{id}")]
|
|
public async Task<IActionResult> GetReceivedMail(int id)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<ReceivedMailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetReceivedMailByIdAsync(currentUserId, id);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return NotFound(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("received/{id}/mark-read")]
|
|
public async Task<IActionResult> MarkReceivedMailAsRead(int id)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.MarkReceivedMailAsReadAsync(currentUserId, id);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("{mailId}/revoke")]
|
|
public async Task<IActionResult> RevokeMail(int mailId)
|
|
{
|
|
// 从JWT令牌中获取当前用户ID
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.RevokeMailAsync(currentUserId, mailId);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 存入胶囊 - 创建胶囊邮件
|
|
/// </summary>
|
|
/// <param name="dto">存入胶囊请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("capsule")]
|
|
public async Task<IActionResult> SaveToCapsule([FromBody] SaveToCapsuleDto dto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<SaveToCapsuleResponseDto>.ErrorResult("请求参数无效"));
|
|
}
|
|
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SaveToCapsuleResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.SaveToCapsuleAsync(currentUserId, dto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return CreatedAtAction(
|
|
nameof(GetCapsuleMail),
|
|
new { id = result.Data!.Id },
|
|
result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取胶囊邮件列表
|
|
/// </summary>
|
|
/// <param name="pageIndex">页码</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="status">状态筛选</param>
|
|
/// <param name="recipientType">收件人类型筛选</param>
|
|
/// <param name="keyword">关键词搜索</param>
|
|
/// <param name="startDate">开始日期</param>
|
|
/// <param name="endDate">结束日期</param>
|
|
/// <returns>胶囊邮件列表</returns>
|
|
[HttpGet("capsule")]
|
|
public async Task<IActionResult> GetCapsuleMails(
|
|
[FromQuery] int pageIndex = 1,
|
|
[FromQuery] int pageSize = 10,
|
|
[FromQuery] int? status = null,
|
|
[FromQuery] int? recipientType = null,
|
|
[FromQuery] string? keyword = null,
|
|
[FromQuery] DateTime? startDate = null,
|
|
[FromQuery] DateTime? endDate = null)
|
|
{
|
|
var queryDto = new MailListQueryDto
|
|
{
|
|
PageIndex = pageIndex,
|
|
PageSize = pageSize,
|
|
Status = status,
|
|
RecipientType = recipientType,
|
|
Keyword = keyword,
|
|
StartDate = startDate,
|
|
EndDate = endDate
|
|
};
|
|
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<PagedResponse<CapsuleMailListResponseDto>>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetCapsuleMailsAsync(currentUserId, queryDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取胶囊邮件详情
|
|
/// </summary>
|
|
/// <param name="id">邮件ID</param>
|
|
/// <returns>胶囊邮件详情</returns>
|
|
[HttpGet("capsule/{id}")]
|
|
public async Task<IActionResult> GetCapsuleMail(int id)
|
|
{
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<CapsuleMailDetailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.GetCapsuleMailByIdAsync(currentUserId, id);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return NotFound(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新胶囊邮件
|
|
/// </summary>
|
|
/// <param name="id">邮件ID</param>
|
|
/// <param name="dto">更新请求</param>
|
|
/// <returns>更新后的胶囊邮件详情</returns>
|
|
[HttpPut("capsule/{id}")]
|
|
public async Task<IActionResult> UpdateCapsuleMail(int id, [FromBody] UpdateCapsuleMailDto dto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<CapsuleMailDetailResponseDto>.ErrorResult("请求参数无效"));
|
|
}
|
|
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<CapsuleMailDetailResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.UpdateCapsuleMailAsync(currentUserId, id, dto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 撤销胶囊邮件
|
|
/// </summary>
|
|
/// <param name="id">邮件ID</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("capsule/{id}/revoke")]
|
|
public async Task<IActionResult> RevokeCapsuleMail(int id)
|
|
{
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<bool>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.RevokeCapsuleMailAsync(currentUserId, id);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送至未来 - 将草稿状态的邮件设置为在未来特定时间自动发送
|
|
/// </summary>
|
|
/// <param name="sendToFutureDto">发送至未来请求DTO</param>
|
|
/// <returns>发送至未来响应DTO</returns>
|
|
[HttpPost("send-to-future")]
|
|
public async Task<IActionResult> SendToFuture([FromBody] SendToFutureDto sendToFutureDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ApiResponse<SendToFutureResponseDto>.ErrorResult("请求参数无效"));
|
|
}
|
|
|
|
var currentUserId = GetCurrentUserId();
|
|
|
|
if (currentUserId <= 0)
|
|
{
|
|
return Unauthorized(ApiResponse<SendToFutureResponseDto>.ErrorResult("未授权访问"));
|
|
}
|
|
|
|
var result = await _mailService.SendToFutureAsync(currentUserId, sendToFutureDto);
|
|
|
|
if (!result.Success)
|
|
{
|
|
return BadRequest(result);
|
|
}
|
|
|
|
return Ok(result);
|
|
}
|
|
}
|
|
} |