2025-11-06 19:23:42 +08:00
|
|
|
|
using System;
|
2025-11-05 00:22:21 +08:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using DouyinApi.Controllers;
|
|
|
|
|
|
using DouyinApi.IServices;
|
|
|
|
|
|
using DouyinApi.Model.Naming;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DouyinApi.Api.Controllers.MiniProgram
|
|
|
|
|
|
{
|
|
|
|
|
|
[Produces("application/json")]
|
|
|
|
|
|
[Route("api/naming")]
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public class NamingController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly INamingService _namingService;
|
2025-11-06 19:23:42 +08:00
|
|
|
|
private readonly IContentSecurityService _contentSecurityService;
|
2025-11-05 00:22:21 +08:00
|
|
|
|
private readonly ILogger<NamingController> _logger;
|
|
|
|
|
|
|
2025-11-06 19:23:42 +08:00
|
|
|
|
public NamingController(
|
|
|
|
|
|
INamingService namingService,
|
|
|
|
|
|
IContentSecurityService contentSecurityService,
|
|
|
|
|
|
ILogger<NamingController> logger)
|
2025-11-05 00:22:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
_namingService = namingService;
|
2025-11-06 19:23:42 +08:00
|
|
|
|
_contentSecurityService = contentSecurityService;
|
2025-11-05 00:22:21 +08:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("validate-surname")]
|
|
|
|
|
|
public async Task<IActionResult> ValidateSurname([FromBody] SurnameValidationRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok(new { isValid = false, message = "请输入合法姓氏" });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var surname = request.Surname?.Trim() ?? string.Empty;
|
2025-11-06 19:23:42 +08:00
|
|
|
|
var security = await _contentSecurityService.CheckTextAsync(surname);
|
|
|
|
|
|
if (!security.IsSafe)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Ok(new { isValid = false, message = "输入内容存在风险,请重新输入" });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 00:22:21 +08:00
|
|
|
|
var isValid = await _namingService.ValidateSurnameAsync(surname);
|
|
|
|
|
|
return Ok(new
|
|
|
|
|
|
{
|
|
|
|
|
|
isValid,
|
|
|
|
|
|
message = isValid ? string.Empty : "请输入合法姓氏"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("generate")]
|
|
|
|
|
|
public async Task<IActionResult> Generate([FromBody] NamingRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
|
{
|
2025-11-06 19:23:42 +08:00
|
|
|
|
return BadRequest(new { message = "参数不合理" });
|
2025-11-05 00:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var normalizedRequest = NormalizeRequest(request);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-11-06 19:23:42 +08:00
|
|
|
|
var payload = string.Join(" ", new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
normalizedRequest.Surname,
|
|
|
|
|
|
normalizedRequest.Gender,
|
|
|
|
|
|
normalizedRequest.BirthDate,
|
|
|
|
|
|
normalizedRequest.BirthTime,
|
|
|
|
|
|
normalizedRequest.NameLength
|
|
|
|
|
|
}).Trim();
|
|
|
|
|
|
|
|
|
|
|
|
var security = await _contentSecurityService.CheckTextAsync(payload);
|
|
|
|
|
|
if (!security.IsSafe)
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest(new { message = "CONTENT_RISK" });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 00:22:21 +08:00
|
|
|
|
var response = await _namingService.GenerateNamesAsync(normalizedRequest);
|
2025-11-05 17:26:45 +08:00
|
|
|
|
return Ok(new
|
|
|
|
|
|
{
|
|
|
|
|
|
analysis = response.Analysis,
|
|
|
|
|
|
results = response.Results
|
|
|
|
|
|
});
|
2025-11-05 00:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (ArgumentException ex) when (ex.Message == "invalid_surname")
|
|
|
|
|
|
{
|
|
|
|
|
|
return BadRequest(new { message = "INVALID_SURNAME" });
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, "Generate naming failed");
|
|
|
|
|
|
return StatusCode(500, new { message = "GENERATION_FAILED" });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static NamingRequest NormalizeRequest(NamingRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var normalized = new NamingRequest
|
|
|
|
|
|
{
|
|
|
|
|
|
Surname = request.Surname?.Trim() ?? string.Empty,
|
|
|
|
|
|
Gender = string.IsNullOrWhiteSpace(request.Gender) ? "male" : request.Gender.Trim().ToLowerInvariant(),
|
|
|
|
|
|
NameLength = string.IsNullOrWhiteSpace(request.NameLength) ? "double" : request.NameLength.Trim().ToLowerInvariant(),
|
|
|
|
|
|
BirthDate = NormalizeDate(request.BirthDate),
|
|
|
|
|
|
BirthTime = NormalizeTime(request.BirthTime)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return normalized;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string NormalizeDate(string date)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(date))
|
|
|
|
|
|
{
|
|
|
|
|
|
return DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(date, out var parsed))
|
|
|
|
|
|
{
|
|
|
|
|
|
return parsed.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return date;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string NormalizeTime(string time)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(time))
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (TimeSpan.TryParse(time, out var parsed))
|
|
|
|
|
|
{
|
|
|
|
|
|
return DateTime.Today.Add(parsed).ToString("HH:mm", CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return time;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|