zcm
This commit is contained in:
parent
ad2a431669
commit
61e8f98af8
@ -241,16 +241,12 @@ namespace asg_form.Controllers
|
||||
var captchaService = new CaptchaService();
|
||||
var (captchaImage, captchaCode) = captchaService.GenerateCaptcha();
|
||||
captchaNow=captchaCode;
|
||||
/*var offTime = new DateTime();
|
||||
var offTime = new DateTime();
|
||||
using (var db = new TestDbContext())
|
||||
{
|
||||
|
||||
var query = db.T_captcha_check.AsQueryable();
|
||||
var currentDateTime = DateTime.Now;
|
||||
var fiveMinutesAgo = currentDateTime.AddMinutes(-5);
|
||||
var recordsToDelete = db.T_captcha_check
|
||||
.Where(x => DateTime.TryParse(x.off_time,out offTime) && offTime < fiveMinutesAgo)
|
||||
.ToList();
|
||||
db.T_captcha_check.RemoveRange(recordsToDelete);
|
||||
int maxId = query.Max(n => n.id);
|
||||
var msg = new capData
|
||||
{
|
||||
@ -260,8 +256,8 @@ namespace asg_form.Controllers
|
||||
};
|
||||
db.Add(msg);
|
||||
db.SaveChanges();
|
||||
}*/
|
||||
return Ok(new { code = 200, captchaImage ,captchaCode});
|
||||
}
|
||||
return Ok(new { code = 200, captchaImage});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return Ok(new { code = 500, message = "服务器错误", ex });
|
||||
|
@ -2,49 +2,63 @@
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using SkiaSharp;
|
||||
using System.IO;
|
||||
|
||||
public class CaptchaService
|
||||
{
|
||||
public string generatedCaptchaCode;
|
||||
public int wrp = 0;
|
||||
// 生成验证码图像并返回Base64字符串
|
||||
public (string captchaImage, string captchaCode) GenerateCaptcha()
|
||||
|
||||
|
||||
public (string captchaImage, string captchaCode) GenerateCaptcha()
|
||||
{
|
||||
string wp = "123";
|
||||
try
|
||||
{
|
||||
string wp = "123";
|
||||
try
|
||||
string generatedCaptchaCode = GenerateRandomCode(4); // 生成4位随机字符
|
||||
int width = 100, height = 40;
|
||||
|
||||
using (var bitmap = new SKBitmap(width, height))
|
||||
{
|
||||
System.AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
|
||||
string generatedCaptchaCode = GenerateRandomCode(4); // 生成4位随机字符
|
||||
Bitmap bitmap = new Bitmap(100, 40);
|
||||
wp = "122";
|
||||
using (Graphics g = Graphics.FromImage(bitmap))
|
||||
wp = "111";
|
||||
using (var canvas = new SKCanvas(bitmap))
|
||||
{
|
||||
g.Clear(Color.White);
|
||||
using (Font font = new Font("Arial", 20))
|
||||
wp = "333";
|
||||
canvas.Clear(SKColors.White);
|
||||
using (var paint = new SKPaint())
|
||||
{
|
||||
g.DrawString(generatedCaptchaCode, font, Brushes.Black, 10, 10);
|
||||
paint.Color = SKColors.Black;
|
||||
paint.TextSize = 20;
|
||||
paint.IsAntialias = true;
|
||||
canvas.DrawText(generatedCaptchaCode, 10, 30, paint);
|
||||
}
|
||||
}
|
||||
wp = "233";
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
wp = "222";
|
||||
using (var image = SKImage.FromBitmap(bitmap))
|
||||
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
data.SaveTo(ms);
|
||||
string base64Image = Convert.ToBase64String(ms.ToArray());
|
||||
return (base64Image, generatedCaptchaCode);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error generating captcha: {ex.Message}");
|
||||
return (ex.Message, wp);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error generating captcha: {ex.Message}");
|
||||
return (ex.Message, wp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 生成随机字符
|
||||
private string GenerateRandomCode(int length)
|
||||
|
||||
// 生成随机字符
|
||||
private string GenerateRandomCode(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345689";
|
||||
Random random = new Random();
|
||||
|
@ -80,8 +80,6 @@ options.AddDefaultPolicy(builder => builder.WithOrigins(urls)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
IServiceCollection services = builder.Services;
|
||||
|
||||
|
||||
@ -160,7 +158,7 @@ app.UseKnife4UI(c =>
|
||||
});
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
|
||||
|
||||
|
||||
//发布时服务器注册静态资源
|
||||
@ -242,4 +240,5 @@ new Thread(o =>
|
||||
{ IsBackground = true }.Start();
|
||||
|
||||
|
||||
|
||||
app.Run();
|
@ -65,10 +65,15 @@
|
||||
|
||||
<PackageReference Include="Panda.SimpleExcel" Version="1.3.2" />
|
||||
<PackageReference Include="RestSharp" Version="106.12.0" />
|
||||
<PackageReference Include="SkiaSharp" Version="2.88.8" />
|
||||
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.8" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.0" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||
<PackageReference Include="Zack.EventBus" Version="1.1.3" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
|
||||
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user