2025-12-29 14:18:09 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-01-28 11:19:23 +08:00
|
|
|
|
using Service.Message;
|
2025-12-29 14:18:09 +08:00
|
|
|
|
|
|
|
|
|
|
namespace WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
2026-01-10 17:47:09 +08:00
|
|
|
|
public class MessageRecordController(IMessageService messageService, ILogger<MessageRecordController> logger) : ControllerBase
|
2025-12-29 14:18:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取消息列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<PagedResponse<MessageRecord>> GetList([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var (list, total) = await messageService.GetPagedListAsync(pageIndex, pageSize);
|
|
|
|
|
|
return PagedResponse<MessageRecord>.Done(list.ToArray(), (int)total);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取消息列表失败");
|
|
|
|
|
|
return PagedResponse<MessageRecord>.Fail($"获取消息列表失败: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取未读消息数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
public async Task<BaseResponse<long>> GetUnreadCount()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var count = await messageService.GetUnreadCountAsync();
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return count.Ok();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "获取未读消息数量失败");
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return $"获取未读消息数量失败: {ex.Message}".Fail<long>();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 标记已读
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse<bool>> MarkAsRead([FromQuery] long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await messageService.MarkAsReadAsync(id);
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return result.Ok();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "标记消息已读失败,ID: {Id}", id);
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return $"标记消息已读失败: {ex.Message}".Fail<bool>();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 全部标记已读
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse<bool>> MarkAllAsRead()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await messageService.MarkAllAsReadAsync();
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return result.Ok();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "全部标记已读失败");
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return $"全部标记已读失败: {ex.Message}".Fail<bool>();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse<bool>> Delete([FromQuery] long id)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await messageService.DeleteAsync(id);
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return result.Ok();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "删除消息失败,ID: {Id}", id);
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return $"删除消息失败: {ex.Message}".Fail<bool>();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增消息 (测试用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<BaseResponse<bool>> Add([FromBody] MessageRecord message)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await messageService.AddAsync(message);
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return result.Ok();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "新增消息失败");
|
2026-01-04 16:43:32 +08:00
|
|
|
|
return $"新增消息失败: {ex.Message}".Fail<bool>();
|
2025-12-29 14:18:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|