m
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
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');
|
||||
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 key = `captcha:${Date.now()}:${Math.random().toString(36).substring(2, 11)}`;
|
||||
await this.cacheManager.set(key, captcha.text.toLowerCase(), 5 * 60 * 1000);
|
||||
return {
|
||||
svg: captcha.data,
|
||||
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;
|
||||
}
|
||||
const correctCode = await this.cacheManager.get(key);
|
||||
if (!correctCode) {
|
||||
return false;
|
||||
}
|
||||
await this.cacheManager.del(key);
|
||||
return code.toLowerCase().trim() === correctCode.toLowerCase().trim();
|
||||
}
|
||||
};
|
||||
exports.CaptchaService = CaptchaService;
|
||||
exports.CaptchaService = CaptchaService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
||||
__metadata("design:paramtypes", [Object])
|
||||
], CaptchaService);
|
||||
//# sourceMappingURL=captcha.service.js.map
|
||||
Reference in New Issue
Block a user