2024-09-02 21:28:21 +08:00
|
|
|
|
using Bunny.Dao.Common.Constant.Result;
|
|
|
|
|
using Bunny.Dao.Common.Result;
|
2024-08-10 18:11:48 +08:00
|
|
|
|
using Lazy.Captcha.Core;
|
2024-09-02 23:20:28 +08:00
|
|
|
|
using log4net;
|
2024-09-02 09:31:34 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2024-08-10 18:11:48 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Bunny.WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证码测试
|
2024-09-05 14:26:31 +08:00
|
|
|
|
/// 2024-9-11 09:02:18
|
2024-08-10 18:11:48 +08:00
|
|
|
|
/// </summary>
|
2024-09-05 14:26:31 +08:00
|
|
|
|
[Microsoft.AspNetCore.Components.Route("/api/[controller]/[action]")]
|
2024-09-02 09:31:34 +08:00
|
|
|
|
public class CaptchaTestController : ControllerBase
|
2024-08-10 18:11:48 +08:00
|
|
|
|
{
|
2024-09-02 23:20:28 +08:00
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(CaptchaTestController));
|
2024-09-02 09:31:34 +08:00
|
|
|
|
[Inject] public required ICaptcha Captcha { get; set; }
|
|
|
|
|
|
2024-08-10 18:11:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试测试验证码生成
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult GetCaptcha(string id)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var captchaData = Captcha.Generate(id);
|
2024-08-10 18:11:48 +08:00
|
|
|
|
// 验证码的ID
|
|
|
|
|
var captchaDataId = captchaData.Id;
|
|
|
|
|
// 验证码的代码
|
|
|
|
|
var captchaDataCode = captchaData.Code;
|
|
|
|
|
|
2024-09-02 23:20:28 +08:00
|
|
|
|
Log.Debug($"验证码的ID:{captchaDataId};验证码的代码:{captchaDataCode}");
|
2024-08-10 18:11:48 +08:00
|
|
|
|
|
|
|
|
|
// 返回验证码图片内容
|
|
|
|
|
var stream = new MemoryStream(captchaData.Bytes);
|
|
|
|
|
return File(stream, "image/gif");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 输入验证码机型校验
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public Result<string> CheckCode(string id, string code)
|
|
|
|
|
{
|
|
|
|
|
// 校验用户输入的验证码是否=正确
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var validate = Captcha.Validate(id, code);
|
|
|
|
|
return validate
|
|
|
|
|
? Result<string>.Success()
|
|
|
|
|
: Result<string>.Error(null!, ErrorConstant.ValidateCodeError);
|
2024-08-10 18:11:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|