Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .Net6版本/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
4 changes: 4 additions & 0 deletions .Net6版本/VOL.Core/Utilities/VierificationCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Versioning;
using System.Text;

namespace VOL.Core.Utilities
Expand Down Expand Up @@ -31,6 +32,9 @@ public static string RandomText()
}
return code;
}

[SupportedOSPlatform("Windows")]
[Obsolete("仅在 Windows 上支持 System.Drawing.Common,具体请参考:https://docs.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only")]
public static string CreateBase64Imgage(string code)
{
Random random = new Random();
Expand Down
97 changes: 97 additions & 0 deletions .Net6版本/VOL.Core/Utilities/VierificationCodeHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.IO;
using System.Linq;
using SkiaSharp;

namespace VOL.Core.Utilities;

public static class VierificationCodeHelpers
{ //验证码字体集合
private static readonly string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
private static readonly SKColor[] colors = { SKColors.Black, SKColors.Red, SKColors.DarkBlue, SKColors.Green,
SKColors.Orange, SKColors.Brown, SKColors.DarkCyan, SKColors.Purple};

/// <summary>
///
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static string CreateBase64Image(string code)
{
var random = new Random();
var info = new SKImageInfo((int)code.Length * 18, 32);
using var bitmap = new SKBitmap(info);
using var canvas = new SKCanvas(bitmap);

canvas.Clear(SKColors.White);

using var pen = new SKPaint();
pen.FakeBoldText = true;
pen.Style = SKPaintStyle.Stroke;
pen.TextSize = 0.9f * info.Width * pen.TextSize / pen.MeasureText(code);

//绘制随机字符
for (int i = 0; i < code.Length; i++)
{
pen.Color = random.GetRandom(colors);//随机颜色索引值
pen.Typeface = SKTypeface.FromFamilyName(random.GetRandom(fonts), 700, 20, SKFontStyleSlant.Italic);//配置字体
var point = new SKPoint()
{
X = i * 12,
Y = info.Height - ((i + 1) % 2 == 0 ? 2 : 4)
};
canvas.DrawText(code.Substring(i, 1), point, pen);//绘制一个验证字符
}

//绘制噪点
var points = Enumerable.Range(0, 100).Select(
_ => new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height))
).ToArray();
canvas.DrawPoints(
SKPointMode.Points,
points,
pen);

//绘制贝塞尔线条
for (int i = 0; i < 2; i++)
{
var p1 = new SKPoint(0, random.Next(bitmap.Height));
var p2 = new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height));
var p3 = new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height));
var p4 = new SKPoint(bitmap.Width, random.Next(bitmap.Height));

var touchPoints = new SKPoint[] { p1, p2, p3, p4 };

using var bPen = new SKPaint();
bPen.Color = random.GetRandom(colors);
bPen.Style = SKPaintStyle.Stroke;

using var path = new SKPath();
path.MoveTo(touchPoints[0]);
path.CubicTo(touchPoints[1], touchPoints[2], touchPoints[3]);
canvas.DrawPath(path, bPen);
}
return bitmap.ToBase64String(SKEncodedImageFormat.Png);
}

public static T GetRandom<T>(this Random random, T[] tArray)
{
if (random == null) random = new Random();
return tArray[random.Next(tArray.Length)];
}

/// <summary>
/// SKBitmap转Base64String
/// </summary>
/// <param name="bitmap"></param>
/// <param name="format"></param>
/// <returns></returns>
public static string ToBase64String(this SKBitmap bitmap, SKEncodedImageFormat format)
{
using var memStream = new MemoryStream();
using var wstream = new SKManagedWStream(memStream);
bitmap.Encode(wstream, format, 32);
memStream.TryGetBuffer(out ArraySegment<byte> buffer);
return $"{Convert.ToBase64String(buffer.Array, 0, (int)memStream.Length)}";
}
}
1 change: 1 addition & 0 deletions .Net6版本/VOL.Core/VOL.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
<!--<PackageReference Include="MySql.Data" Version="8.0.13" />-->
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
<PackageReference Include="ZKWeb.System.Drawing" Version="4.0.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public IActionResult GetVierificationCode()
string code = VierificationCode.RandomText();
var data = new
{
img = VierificationCode.CreateBase64Imgage(code),
img = VierificationCodeHelpers.CreateBase64Image(code),
uuid = Guid.NewGuid()
};
HttpContext.GetService<IMemoryCache>().Set(data.uuid.ToString(), code, new TimeSpan(0, 5, 0));
Expand Down
29 changes: 29 additions & 0 deletions .Net6版本/VOL.WebApi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN apt-get update; apt-get install libfontconfig1 -y

WORKDIR /app
EXPOSE 9991

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["VOL.WebApi/VOL.WebApi.csproj", "VOL.WebApi/"]
COPY ["VOL.System/VOL.System.csproj", "VOL.System/"]
COPY ["VOL.Core/VOL.Core.csproj", "VOL.Core/"]
COPY ["VOL.Entity/VOL.Entity.csproj", "VOL.Entity/"]
COPY ["VOL.Order/VOL.Order.csproj", "VOL.Order/"]
COPY ["VOL.AppManager/VOL.AppManager.csproj", "VOL.AppManager/"]
COPY ["VOL.Builder/VOL.Builder.csproj", "VOL.Builder/"]
RUN dotnet restore "VOL.WebApi/VOL.WebApi.csproj"
COPY . .
WORKDIR "/src/VOL.WebApi"
RUN dotnet build "VOL.WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "VOL.WebApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "VOL.WebApi.dll"]
31 changes: 19 additions & 12 deletions .Net6版本/VOL.WebApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1309",
"sslPort": 44318
}
},
{
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
Expand All @@ -18,10 +10,25 @@
"VOL.WebApi": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "http://localhost:9991"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true,
"useSSL": true
}
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1309",
"sslPort": 44318
}
}
}
}
3 changes: 3 additions & 0 deletions .Net6版本/VOL.WebApi/VOL.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<UserSecretsId>a3e3c6fb-4b7e-44a6-8dbe-5e5f11af4202</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -20,6 +22,7 @@
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.0.0" />
</ItemGroup>

Expand Down