m
This commit is contained in:
+39
-24
@@ -15,22 +15,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CaptchaService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const cache_manager_1 = require("@nestjs/cache-manager");
|
||||
const svgCaptcha = require('svg-captcha');
|
||||
const CHAR_PRESET = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
|
||||
function randomChars(pool, count) {
|
||||
const chars = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
chars.push(pool[Math.floor(Math.random() * pool.length)]);
|
||||
}
|
||||
return chars;
|
||||
}
|
||||
function randomColor() {
|
||||
const h = Math.floor(Math.random() * 360);
|
||||
return `hsl(${h}, 70%, 40%)`;
|
||||
}
|
||||
function createSolidSvg(chars, width, height, fontSize, noise, color) {
|
||||
const text = chars.join('');
|
||||
let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">`;
|
||||
for (let i = 0; i < noise; i++) {
|
||||
const x1 = Math.random() * width;
|
||||
const y1 = Math.random() * height;
|
||||
const x2 = Math.random() * width;
|
||||
const y2 = Math.random() * height;
|
||||
svg += `<line x1="${x1.toFixed(1)}" y1="${y1.toFixed(1)}" x2="${x2.toFixed(1)}" y2="${y2.toFixed(1)}" stroke="${randomColor()}" stroke-width="1" opacity="0.4"/>`;
|
||||
}
|
||||
const padding = width * 0.12;
|
||||
const usableWidth = width - padding * 2;
|
||||
const charStep = usableWidth / chars.length;
|
||||
const startY = height / 2 + fontSize / 3.5;
|
||||
for (let i = 0; i < chars.length; i++) {
|
||||
const x = padding + charStep * (i + 0.5);
|
||||
const rotation = (Math.random() - 0.5) * 15;
|
||||
const dy = (Math.random() - 0.5) * 4;
|
||||
const charColor = color || randomColor();
|
||||
const y = startY + dy;
|
||||
svg += `<text x="${x.toFixed(1)}" y="${y.toFixed(1)}" font-family="Arial, Helvetica, sans-serif" font-size="${fontSize}" font-weight="bold" fill="${charColor}" text-anchor="middle" dominant-baseline="middle" transform="rotate(${rotation.toFixed(1)}, ${x.toFixed(1)}, ${y.toFixed(1)})">${chars[i]}</text>`;
|
||||
}
|
||||
svg += '</svg>';
|
||||
return { data: svg, text };
|
||||
}
|
||||
let CaptchaService = class CaptchaService {
|
||||
cacheManager;
|
||||
constructor(cacheManager) {
|
||||
this.cacheManager = cacheManager;
|
||||
}
|
||||
async generate(options) {
|
||||
const captcha = svgCaptcha.create({
|
||||
size: options?.size ?? 4,
|
||||
ignoreChars: options?.charPreset ?? '23456789ABCDEFGHJKLMNPQRSTUVWXYZ',
|
||||
noise: options?.noise ?? 3,
|
||||
width: options?.width ?? 120,
|
||||
height: options?.height ?? 40,
|
||||
fontSize: options?.fontSize ?? 30,
|
||||
inverse: options?.inverse ?? false,
|
||||
});
|
||||
const chars = randomChars(options?.charPreset ?? CHAR_PRESET, options?.size ?? 4);
|
||||
const captcha = createSolidSvg(chars, options?.width ?? 120, options?.height ?? 40, options?.fontSize ?? 30, options?.noise ?? 3, options?.color);
|
||||
const key = `captcha:${Date.now()}:${Math.random().toString(36).substring(2, 11)}`;
|
||||
await this.cacheManager.set(key, captcha.text.toLowerCase(), 5 * 60 * 1000);
|
||||
return {
|
||||
@@ -38,20 +67,6 @@ let CaptchaService = class CaptchaService {
|
||||
key,
|
||||
};
|
||||
}
|
||||
async generateMath(options) {
|
||||
const captcha = svgCaptcha.createMathExpr({
|
||||
noise: options?.noise ?? 3,
|
||||
width: options?.width ?? 120,
|
||||
height: options?.height ?? 40,
|
||||
fontSize: options?.fontSize ?? 30,
|
||||
});
|
||||
const key = `captcha:${Date.now()}:${Math.random().toString(36).substring(2, 11)}`;
|
||||
await this.cacheManager.set(key, captcha.text, 5 * 60 * 1000);
|
||||
return {
|
||||
svg: captcha.data,
|
||||
key,
|
||||
};
|
||||
}
|
||||
async validate(key, code) {
|
||||
if (code == '8888') {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user