@jayfong/x-server 2.20.0 → 2.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_cjs/services/captcha.js +16 -5
- package/lib/services/captcha.d.ts +18 -5
- package/lib/services/captcha.js +16 -5
- package/package.json +1 -1
|
@@ -23,6 +23,7 @@ class CaptchaService {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
async generate(payload) {
|
|
26
|
+
var _payload$retryCount;
|
|
26
27
|
const chars = this.charsetMap[payload.charset];
|
|
27
28
|
let code = '';
|
|
28
29
|
for (let i = 0; i < payload.size; i++) {
|
|
@@ -30,7 +31,9 @@ class CaptchaService {
|
|
|
30
31
|
}
|
|
31
32
|
const token = await _x.x.cache.save({
|
|
32
33
|
code: code,
|
|
33
|
-
scene: payload.scene
|
|
34
|
+
scene: payload.scene,
|
|
35
|
+
retryCount: (_payload$retryCount = payload.retryCount) != null ? _payload$retryCount : 0,
|
|
36
|
+
extra: payload.extra
|
|
34
37
|
}, payload.ttl);
|
|
35
38
|
return {
|
|
36
39
|
token: token,
|
|
@@ -46,8 +49,10 @@ class CaptchaService {
|
|
|
46
49
|
charPreset: this.charsetMap[payload.charset].join('')
|
|
47
50
|
});
|
|
48
51
|
const token = await _x.x.cache.save({
|
|
49
|
-
code,
|
|
50
|
-
scene: payload.scene
|
|
52
|
+
code: code,
|
|
53
|
+
scene: payload.scene,
|
|
54
|
+
retryCount: payload.retryCount || 0,
|
|
55
|
+
extra: payload.extra
|
|
51
56
|
}, payload.ttl);
|
|
52
57
|
const dataUrl = (0, _miniSvgDataUri.default)(svg);
|
|
53
58
|
return {
|
|
@@ -61,8 +66,14 @@ class CaptchaService {
|
|
|
61
66
|
if (expected == null) {
|
|
62
67
|
return false;
|
|
63
68
|
}
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
if (expected.retryCount === 0) {
|
|
70
|
+
await _x.x.cache.remove(options.token);
|
|
71
|
+
} else {
|
|
72
|
+
expected.retryCount--;
|
|
73
|
+
const ttl = await _x.x.cache.ttl(options.token);
|
|
74
|
+
await _x.x.cache.set(options.token, expected, ttl);
|
|
75
|
+
}
|
|
76
|
+
return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
|
|
66
77
|
}
|
|
67
78
|
}
|
|
68
79
|
exports.CaptchaService = CaptchaService;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { type MsValue } from 'vtils';
|
|
2
2
|
import { BaseService } from './base';
|
|
3
3
|
export type CaptchaServiceGenerateCharset = '09' | '19' | 'az' | 'AZ' | 'aZ' | '0z' | '0Z' | '1z' | '1Z';
|
|
4
|
-
export interface CaptchaServiceGeneratePayload {
|
|
4
|
+
export interface CaptchaServiceGeneratePayload<T> {
|
|
5
5
|
/**
|
|
6
6
|
* 场景值
|
|
7
|
+
*
|
|
8
|
+
* 比如:防止后台验证码图片登录用其他场景下的短信验证码
|
|
7
9
|
*/
|
|
8
10
|
scene: string;
|
|
9
11
|
/**
|
|
@@ -18,6 +20,16 @@ export interface CaptchaServiceGeneratePayload {
|
|
|
18
20
|
* 字符集
|
|
19
21
|
*/
|
|
20
22
|
charset: CaptchaServiceGenerateCharset;
|
|
23
|
+
/**
|
|
24
|
+
* 可重试次数
|
|
25
|
+
*
|
|
26
|
+
* @default 0
|
|
27
|
+
*/
|
|
28
|
+
retryCount?: number;
|
|
29
|
+
/**
|
|
30
|
+
* 其他信息
|
|
31
|
+
*/
|
|
32
|
+
extra?: T;
|
|
21
33
|
}
|
|
22
34
|
export interface CaptchaServiceGenerateResult {
|
|
23
35
|
token: string;
|
|
@@ -33,17 +45,18 @@ export interface CaptchaServiceGenerateDataUrlResult {
|
|
|
33
45
|
code: string;
|
|
34
46
|
dataUrl: string;
|
|
35
47
|
}
|
|
36
|
-
export interface CaptchaServiceVerifyOptions {
|
|
48
|
+
export interface CaptchaServiceVerifyOptions<T> {
|
|
37
49
|
token: string;
|
|
38
50
|
code: string;
|
|
39
51
|
scene: string;
|
|
52
|
+
extra?: (extra: T) => boolean;
|
|
40
53
|
}
|
|
41
54
|
export declare class CaptchaService implements BaseService {
|
|
42
55
|
serviceName: string;
|
|
43
56
|
private charsetMap;
|
|
44
|
-
generate(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateResult>;
|
|
45
|
-
generateDataUrl(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateDataUrlResult>;
|
|
46
|
-
verify(options: CaptchaServiceVerifyOptions): Promise<boolean>;
|
|
57
|
+
generate<T extends Record<string, any>>(payload: CaptchaServiceGeneratePayload<T>): Promise<CaptchaServiceGenerateResult>;
|
|
58
|
+
generateDataUrl<T extends Record<string, any>>(payload: CaptchaServiceGeneratePayload<T>): Promise<CaptchaServiceGenerateDataUrlResult>;
|
|
59
|
+
verify<T extends Record<string, any>>(options: CaptchaServiceVerifyOptions<T>): Promise<boolean>;
|
|
47
60
|
}
|
|
48
61
|
declare module '../x' {
|
|
49
62
|
interface X {
|
package/lib/services/captcha.js
CHANGED
|
@@ -18,6 +18,7 @@ export class CaptchaService {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
async generate(payload) {
|
|
21
|
+
var _payload$retryCount;
|
|
21
22
|
const chars = this.charsetMap[payload.charset];
|
|
22
23
|
let code = '';
|
|
23
24
|
for (let i = 0; i < payload.size; i++) {
|
|
@@ -25,7 +26,9 @@ export class CaptchaService {
|
|
|
25
26
|
}
|
|
26
27
|
const token = await x.cache.save({
|
|
27
28
|
code: code,
|
|
28
|
-
scene: payload.scene
|
|
29
|
+
scene: payload.scene,
|
|
30
|
+
retryCount: (_payload$retryCount = payload.retryCount) != null ? _payload$retryCount : 0,
|
|
31
|
+
extra: payload.extra
|
|
29
32
|
}, payload.ttl);
|
|
30
33
|
return {
|
|
31
34
|
token: token,
|
|
@@ -41,8 +44,10 @@ export class CaptchaService {
|
|
|
41
44
|
charPreset: this.charsetMap[payload.charset].join('')
|
|
42
45
|
});
|
|
43
46
|
const token = await x.cache.save({
|
|
44
|
-
code,
|
|
45
|
-
scene: payload.scene
|
|
47
|
+
code: code,
|
|
48
|
+
scene: payload.scene,
|
|
49
|
+
retryCount: payload.retryCount || 0,
|
|
50
|
+
extra: payload.extra
|
|
46
51
|
}, payload.ttl);
|
|
47
52
|
const dataUrl = svgToDataUrl(svg);
|
|
48
53
|
return {
|
|
@@ -56,7 +61,13 @@ export class CaptchaService {
|
|
|
56
61
|
if (expected == null) {
|
|
57
62
|
return false;
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
if (expected.retryCount === 0) {
|
|
65
|
+
await x.cache.remove(options.token);
|
|
66
|
+
} else {
|
|
67
|
+
expected.retryCount--;
|
|
68
|
+
const ttl = await x.cache.ttl(options.token);
|
|
69
|
+
await x.cache.set(options.token, expected, ttl);
|
|
70
|
+
}
|
|
71
|
+
return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
|
|
61
72
|
}
|
|
62
73
|
}
|