@seayoo-web/captcha 1.0.0

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # Captcha
package/dist/index.js ADDED
@@ -0,0 +1,59 @@
1
+ var d = Object.defineProperty;
2
+ var h = (a, t, e) => t in a ? d(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
+ var n = (a, t, e) => h(a, typeof t != "symbol" ? t + "" : t, e);
4
+ import { useConsole as p, EventBus as w, usePromise as l, loadJS as u } from "@seayoo-web/utils";
5
+ const y = p("CaptchaSDK");
6
+ let s;
7
+ async function i() {
8
+ return "TencentCaptcha" in window && window.TencentCaptcha ? window.TencentCaptcha : s ? await s : (s = u("https://turing.captcha.qcloud.com/TJCaptcha.js").then(() => window.TencentCaptcha ?? null), await s);
9
+ }
10
+ class m {
11
+ /**
12
+ * 创建验证码实例,创建后立即开始加载腾讯天御 SDK
13
+ */
14
+ constructor(t) {
15
+ n(this, "sdk", null);
16
+ n(this, "isReady", !1);
17
+ n(this, "event", new w());
18
+ i().then((e) => {
19
+ if (!e)
20
+ y.error("Load TencentCaptcha Error!");
21
+ else {
22
+ const { captchaAppId: r, ...c } = t;
23
+ this.sdk = new e(
24
+ r,
25
+ (o) => {
26
+ this.event.emit("done", o);
27
+ },
28
+ {
29
+ ...c,
30
+ ready: (o) => {
31
+ this.isReady = !0, this.event.emit("ready", o);
32
+ }
33
+ }
34
+ );
35
+ }
36
+ });
37
+ }
38
+ /** 提前加载腾讯 js sdk */
39
+ static preloadSDK() {
40
+ i();
41
+ }
42
+ async ready() {
43
+ if (this.isReady)
44
+ return;
45
+ const { promise: t, resolve: e } = l();
46
+ this.event.once("ready", e), await t;
47
+ }
48
+ async show() {
49
+ var t;
50
+ await this.ready(), (t = this.sdk) == null || t.show();
51
+ }
52
+ async destroy() {
53
+ var t;
54
+ await this.ready(), (t = this.sdk) == null || t.destroy();
55
+ }
56
+ }
57
+ export {
58
+ m as TencentCaptchaAgent
59
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@seayoo-web/captcha",
3
+ "description": "captcha agent",
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "source": "./index.ts",
9
+ "types": "./types/index.d.ts",
10
+ "files": [
11
+ "dist",
12
+ "types",
13
+ "README.md"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "engines": {
19
+ "node": ">=18"
20
+ },
21
+ "keywords": [
22
+ "captcha"
23
+ ],
24
+ "author": "web@seayoo.com",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "@seayoo-web/scripts": "^1.0.6",
28
+ "@seayoo-web/tsconfig": "^1.0.2",
29
+ "@seayoo-web/utils": "^2.2.0"
30
+ },
31
+ "peerDependencies": {
32
+ "@seayoo-web/utils": "^2.2.0"
33
+ },
34
+ "scripts": {
35
+ "build": "vite build && tsc --emitDeclarationOnly",
36
+ "type-check": "tsc --noEmit",
37
+ "lint": "eslint ./src/**/*.ts",
38
+ "lint:fix": "eslint ./src/**/*.{ts,js} --fix",
39
+ "prepublish": "pnpm lint:fix && pnpm build"
40
+ }
41
+ }
@@ -0,0 +1 @@
1
+ export * from "./src/tencent";
@@ -0,0 +1,127 @@
1
+ /** 对腾讯的sdk做简单声明 */
2
+ declare global {
3
+ interface Window {
4
+ TencentCaptcha?: TencentCaptchaSDK;
5
+ }
6
+ }
7
+ interface VerifyResult {
8
+ /**
9
+ * 验证结果。等于 2 时表示用户关闭,不等于 0 表示失败
10
+ */
11
+ ret: number;
12
+ /**
13
+ * 验证票据,验证成功的票据,当且仅当 ret = 0 时 ticket 有值
14
+ *
15
+ * 请求验证码发生错误,验证码自动返回 trerror_ 前缀的容灾票据
16
+ */
17
+ ticket: string;
18
+ /**
19
+ * 本次验证的随机串
20
+ */
21
+ randstr: string;
22
+ /**
23
+ * 自定义透传参数
24
+ */
25
+ bizState?: unknown;
26
+ /**
27
+ * 验证码校验接口耗时(ms)
28
+ */
29
+ verifyDuration: number;
30
+ /**
31
+ * 操作校验成功耗时(用户动作+校验完成)(ms)
32
+ */
33
+ actionDuration: number;
34
+ /**
35
+ * 链路 sid
36
+ */
37
+ sid: string;
38
+ }
39
+ interface TencentCaptchaSDK {
40
+ new (captchaAppId: string, callback: (result: VerifyResult) => void, option?: Omit<TencentCaptchaOption, "captchaAppId"> & {
41
+ showFn?: () => void;
42
+ }): TencentCaptchaSDKIns;
43
+ }
44
+ interface TencentCaptchaSDKIns {
45
+ /** 显示验证码,可以反复调用 */
46
+ show(): void;
47
+ /** 隐藏验证码,可以反复调用 */
48
+ destroy(): void;
49
+ /** 获取验证成功后的 ticket */
50
+ getTicket(): {
51
+ CaptchaAppId: string;
52
+ ticket: string;
53
+ };
54
+ }
55
+ interface TencentCaptchaOption {
56
+ captchaAppId: string;
57
+ /**
58
+ * 验证码相关资源加载完成的回调,回调参数为验证码实际的宽高(单位:px)
59
+ *
60
+ * 该参数仅为查看验证码宽高使用
61
+ */
62
+ ready?: (info: {
63
+ sdkView: {
64
+ width: number;
65
+ height: number;
66
+ };
67
+ }) => void;
68
+ /**
69
+ * 开启自适应深夜模式或强制深夜模式
70
+ *
71
+ * - true 开启自适应深夜模式
72
+ * - "force" 强制深夜模式
73
+ */
74
+ enableDarkMode?: boolean | "force";
75
+ /**
76
+ * 仅支持移动端原生 webview 调用时传入,用来设置验证码 loading 加载弹窗的大小
77
+ *
78
+ * 💡注意,此参数调整的并非验证码弹窗大小
79
+ */
80
+ sdkOpts?: {
81
+ width: number;
82
+ height: number;
83
+ };
84
+ /**
85
+ * 隐藏帮助按钮或自定义帮助按钮链接
86
+ */
87
+ needFeedBack?: boolean | `https://${string}`;
88
+ /**
89
+ * 是否在验证码加载过程中显示 loading 框。默认 true
90
+ *
91
+ * 💡 如果隐藏 loading,则对应的 loading 半透蒙层也会一并隐藏
92
+ */
93
+ loading?: boolean;
94
+ /**
95
+ * 指定验证码提示文案的语言,优先级高于控制台配置。大小写不敏感。
96
+ */
97
+ userLanguage?: string;
98
+ /**
99
+ * 验证码展示方式,默认 "popup"
100
+ *
101
+ * - popup 弹出式,以浮层形式居中弹出展示验证码
102
+ * - embed 嵌入式,以嵌入指定容器元素中的方式展示验证码
103
+ */
104
+ type?: "popup" | "embed";
105
+ /**
106
+ * CaptchaAppId 加密校验串,详见 [CaptchaAppid 加密校验能力接入指引](https://cloud.tencent.com/document/product/1110/36841#c6a3e517-4962-4932-9bf8-9fb10fd03166)
107
+ */
108
+ aidEncrypted?: string;
109
+ }
110
+ /**
111
+ * 腾讯天御验证码
112
+ */
113
+ export declare class TencentCaptchaAgent {
114
+ private sdk;
115
+ private isReady;
116
+ private event;
117
+ /** 提前加载腾讯 js sdk */
118
+ static preloadSDK(): void;
119
+ /**
120
+ * 创建验证码实例,创建后立即开始加载腾讯天御 SDK
121
+ */
122
+ constructor(option: Omit<TencentCaptchaOption, "ready">);
123
+ ready(): Promise<void>;
124
+ show(): Promise<void>;
125
+ destroy(): Promise<void>;
126
+ }
127
+ export {};