提交代码
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareWeb.Models;
|
||||
using HardwareWeb.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 系统参数配置 Web 接口。
|
||||
/// 除密码校验外,其余接口都需要在请求头携带 X-Admin-Token。
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/system-config")]
|
||||
public class SystemConfigController : ControllerBase
|
||||
{
|
||||
private const string TokenHeader = "X-Admin-Token";
|
||||
|
||||
private readonly ISystemConfigService _service;
|
||||
private readonly AdminAccessService _access;
|
||||
private readonly ILogger<SystemConfigController> _logger;
|
||||
|
||||
public SystemConfigController(
|
||||
ISystemConfigService service,
|
||||
AdminAccessService access,
|
||||
ILogger<SystemConfigController> logger)
|
||||
{
|
||||
_service = service;
|
||||
_access = access;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>校验管理密码,通过后返回访问令牌。</summary>
|
||||
[HttpPost("auth")]
|
||||
[ProducesResponseType(typeof(AdminAuthPayload), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public IActionResult Auth([FromBody] AdminAuthRequest? request)
|
||||
{
|
||||
var session = _access.SignIn(request?.Password);
|
||||
if (session is null)
|
||||
{
|
||||
_logger.LogWarning("系统参数管理密码校验失败");
|
||||
return Unauthorized(new
|
||||
{
|
||||
error = "Unauthorized",
|
||||
message = "密码错误"
|
||||
});
|
||||
}
|
||||
|
||||
_logger.LogInformation("系统参数管理密码校验通过");
|
||||
return Ok(new AdminAuthPayload(session.Token, session.ExpiresAt));
|
||||
}
|
||||
|
||||
/// <summary>读取系统参数列表,可按关键字过滤。</summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<SystemConfigPayload>), StatusCodes.Status200OK)]
|
||||
public IActionResult List([FromQuery] string? keyword)
|
||||
{
|
||||
if (!IsAuthorized())
|
||||
{
|
||||
return AdminUnauthorized();
|
||||
}
|
||||
|
||||
var items = _service
|
||||
.GetAll(keyword)
|
||||
.Select(SystemConfigPayload.From)
|
||||
.ToList();
|
||||
|
||||
return Ok(items);
|
||||
}
|
||||
|
||||
/// <summary>按主键读取单条系统参数。</summary>
|
||||
[HttpGet("{id:int}")]
|
||||
[ProducesResponseType(typeof(SystemConfigPayload), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public IActionResult Get(int id)
|
||||
{
|
||||
if (!IsAuthorized())
|
||||
{
|
||||
return AdminUnauthorized();
|
||||
}
|
||||
|
||||
var config = _service.GetById(id);
|
||||
return config is null
|
||||
? NotFound(new { error = "NotFound", message = $"系统参数 {id} 不存在" })
|
||||
: Ok(SystemConfigPayload.From(config));
|
||||
}
|
||||
|
||||
/// <summary>新增系统参数。</summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(SystemConfigPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult Create([FromBody] SystemConfigUpsertRequest? request)
|
||||
{
|
||||
if (!IsAuthorized())
|
||||
{
|
||||
return AdminUnauthorized();
|
||||
}
|
||||
|
||||
if (request is null)
|
||||
{
|
||||
return BadRequest(new { error = "InvalidArgument", message = "请求体不能为空" });
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var created = _service.Create(request.ToEntity());
|
||||
return Ok(SystemConfigPayload.From(created));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return ServiceError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>按主键更新系统参数。</summary>
|
||||
[HttpPut("{id:int}")]
|
||||
[ProducesResponseType(typeof(SystemConfigPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult Update(int id, [FromBody] SystemConfigUpsertRequest? request)
|
||||
{
|
||||
if (!IsAuthorized())
|
||||
{
|
||||
return AdminUnauthorized();
|
||||
}
|
||||
|
||||
if (request is null)
|
||||
{
|
||||
return BadRequest(new { error = "InvalidArgument", message = "请求体不能为空" });
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var updated = _service.Update(request.ToEntity(id));
|
||||
return Ok(SystemConfigPayload.From(updated));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return ServiceError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>按主键删除系统参数。</summary>
|
||||
[HttpDelete("{id:int}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
if (!IsAuthorized())
|
||||
{
|
||||
return AdminUnauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return _service.Delete(id)
|
||||
? NoContent()
|
||||
: NotFound(new { error = "NotFound", message = $"系统参数 {id} 不存在" });
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return ServiceError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAuthorized() => _access.Validate(Request.Headers[TokenHeader]);
|
||||
|
||||
private IActionResult AdminUnauthorized() =>
|
||||
Unauthorized(new
|
||||
{
|
||||
error = "Unauthorized",
|
||||
message = "管理员凭证无效或已过期,请重新输入密码"
|
||||
});
|
||||
|
||||
private IActionResult ServiceError(HardwareException ex)
|
||||
{
|
||||
var statusCode = ex.Kind switch
|
||||
{
|
||||
HardwareErrorKind.InvalidArgument => StatusCodes.Status400BadRequest,
|
||||
_ => StatusCodes.Status500InternalServerError,
|
||||
};
|
||||
|
||||
if (statusCode >= 500)
|
||||
{
|
||||
_logger.LogError(ex, "系统参数接口失败:{Message}", ex.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("系统参数接口失败:{Message}", ex.Message);
|
||||
}
|
||||
|
||||
return new ObjectResult(new
|
||||
{
|
||||
error = ex.Kind.ToString(),
|
||||
message = ex.Message
|
||||
})
|
||||
{
|
||||
StatusCode = statusCode
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user