2026-04-26 04:09:35 +08:00
|
|
|
using SimApi.Communications;
|
2019-12-23 16:32:06 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using System.Linq;
|
2026-04-26 10:03:38 +08:00
|
|
|
using static SimApi.Helpers.SimApiError;
|
2019-12-23 16:32:06 +08:00
|
|
|
|
2024-03-23 07:29:43 +08:00
|
|
|
namespace SimApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 基础控制器,所有控制器均继承本控制器
|
|
|
|
|
/// 1. 自动验证请求参数
|
|
|
|
|
/// 2. 报错返回
|
|
|
|
|
/// 3. 错误回馈页面
|
|
|
|
|
/// </summary>
|
2025-11-01 15:40:55 +08:00
|
|
|
///
|
|
|
|
|
[Consumes("application/json")]
|
|
|
|
|
[Produces("application/json")]
|
2024-03-23 07:29:43 +08:00
|
|
|
public class SimApiBaseController : Controller
|
2019-12-23 16:32:06 +08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2024-03-23 07:29:43 +08:00
|
|
|
/// 当前登录用户的ID
|
2019-12-23 16:32:06 +08:00
|
|
|
/// </summary>
|
2024-07-26 08:11:21 +08:00
|
|
|
protected SimApiLoginItem LoginInfo => (SimApiLoginItem)HttpContext.Items["LoginInfo"]!;
|
2024-03-23 07:29:43 +08:00
|
|
|
|
2026-05-03 19:34:47 +08:00
|
|
|
protected SimApiLoginItem LoginToken => (SimApiLoginItem)HttpContext.Items["LoginToken"]!;
|
|
|
|
|
|
2024-03-23 07:29:43 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 验证请求参数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
2019-12-23 16:32:06 +08:00
|
|
|
{
|
2024-08-03 18:35:01 +08:00
|
|
|
if (context.ModelState.IsValid) return;
|
|
|
|
|
foreach (var item in context.ModelState.Values.Where(item =>
|
|
|
|
|
item.ValidationState == ModelValidationState.Invalid))
|
2019-12-23 16:32:06 +08:00
|
|
|
{
|
2024-08-03 18:35:01 +08:00
|
|
|
Error(400, item.Errors.First().ErrorMessage);
|
|
|
|
|
break;
|
2019-12-23 16:32:06 +08:00
|
|
|
}
|
2024-03-23 07:29:43 +08:00
|
|
|
}
|
2019-12-23 16:32:06 +08:00
|
|
|
}
|