2024-09-02 09:00:50 +08:00
|
|
|
|
using Bunny.Dao.Model.Constant.Result;
|
|
|
|
|
using Bunny.Dao.Model.Result;
|
2024-08-10 18:11:48 +08:00
|
|
|
|
using Lazy.Captcha.Core;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Bunny.WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证码测试
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/api/[controller]/[action]")]
|
|
|
|
|
public class CaptchaTestController(ICaptcha _captcha) : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试测试验证码生成
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult GetCaptcha(string id)
|
|
|
|
|
{
|
|
|
|
|
var captchaData = _captcha.Generate(id);
|
|
|
|
|
// 验证码的ID
|
|
|
|
|
var captchaDataId = captchaData.Id;
|
|
|
|
|
// 验证码的代码
|
|
|
|
|
var captchaDataCode = captchaData.Code;
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"验证码的ID:{captchaDataId};验证码的代码:{captchaDataCode}");
|
|
|
|
|
|
|
|
|
|
// 返回验证码图片内容
|
|
|
|
|
var stream = new MemoryStream(captchaData.Bytes);
|
|
|
|
|
return File(stream, "image/gif");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 输入验证码机型校验
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public Result<string> CheckCode(string id, string code)
|
|
|
|
|
{
|
|
|
|
|
// 校验用户输入的验证码是否=正确
|
|
|
|
|
var validate = _captcha.Validate(id, code);
|
|
|
|
|
return validate ? Result<string>.Success() : Result<string>.Error(ErrorConstant.ValidateCodeError);
|
|
|
|
|
}
|
|
|
|
|
}
|