Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 8611dbe

Browse files
committed
feat: add classic call method
1 parent 965afe9 commit 8611dbe

File tree

3 files changed

+98
-19
lines changed

3 files changed

+98
-19
lines changed

index.html

+26-7
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,33 @@
2929
</head>
3030
<body>
3131
<div id="root"></div>
32-
<script type="module">
33-
import { NewCaptcha } from "/lib/main.tsx";
32+
<!-- Module 调用法 -->
33+
<!-- <script type="module">-->
34+
<!-- import { NewCaptcha } from "/lib/main.tsx";-->
3435

35-
NewCaptcha({
36-
el: document.getElementById("root"),
37-
instance: "http://localhost:8080",
38-
siteKey: "test",
39-
});
36+
<!-- NewCaptcha({-->
37+
<!-- el: document.getElementById("root"),-->
38+
<!-- instance: "http://localhost:8080",-->
39+
<!-- siteKey: "test",-->
40+
<!-- });-->
41+
<!-- </script>-->
42+
43+
<!-- Classic 调用法 -->
44+
<script type="module" async="async" src="/lib/main.tsx"></script>
45+
<script>
46+
// 加载验证码
47+
let loadInterval;
48+
loadInterval = setInterval(() => {
49+
if (window.nyacap) {
50+
window.nyacap.render(document.getElementById("root"), {
51+
sitekey: "http://localhost:8080/widget?sitekey=test",
52+
callback: (key) => {
53+
alert(`验证成功: ${key}`);
54+
},
55+
});
56+
clearInterval(loadInterval);
57+
}
58+
}, 100);
4059
</script>
4160
</body>
4261
</html>

lib/components/Captcha/index.tsx

+28-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ interface CaptchaProps {
1111
siteKey: string;
1212
maxDots: number;
1313
maxFailCount: number;
14-
inputName: string;
14+
15+
// 两种调用方式:构建一个 input ,或者用 callbacks
16+
inputName?: string;
17+
18+
cbSuccess?: (key: string) => void;
19+
cbFail?: () => void;
20+
cbTimeout?: () => void;
1521
}
1622
const Captcha = ({
1723
instance,
1824
siteKey,
1925
maxDots,
2026
maxFailCount,
2127
inputName,
28+
cbSuccess,
29+
cbFail,
30+
// cbTimeout, // TODO
2231
}: CaptchaProps) => {
2332
const [captKey, setCaptKey] = useState("");
2433
const [captStatus, setCaptStatus] = useState<Status>("default");
@@ -69,6 +78,9 @@ const Captcha = ({
6978
if (res.s) {
7079
setCaptStatus("success");
7180
captAutoRefreshCount.current = 0;
81+
if (cbSuccess) {
82+
cbSuccess(captKey);
83+
}
7284
} else {
7385
if (captAutoRefreshCount.current > maxFailCount) {
7486
// 错误次数太多,歇一会吧
@@ -78,6 +90,9 @@ const Captcha = ({
7890
setCaptStatus("error");
7991
captAutoRefreshCount.current += 1;
8092
}
93+
if (cbFail) {
94+
cbFail();
95+
}
8196
}
8297
} catch (e) {
8398
console.log(e);
@@ -163,16 +178,18 @@ const Captcha = ({
163178
</div>
164179
</Fragment>
165180

166-
<input
167-
style={{
168-
visibility: "hidden",
169-
}}
170-
name={inputName}
171-
value={captStatus === "success" ? captKey : ""}
172-
type="password"
173-
required
174-
readOnly
175-
/>
181+
{inputName && (
182+
<input
183+
style={{
184+
visibility: "hidden",
185+
}}
186+
name={inputName}
187+
value={captStatus === "success" ? captKey : ""}
188+
type="password"
189+
required
190+
readOnly
191+
/>
192+
)}
176193
</>
177194
);
178195
};

lib/main.tsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ interface NewCaptchaProps {
99
siteKey: string;
1010
maxDots?: number;
1111
maxFailCount?: number;
12+
13+
// 两种调用方式:构建一个 input ,或者用 callbacks
1214
inputName?: string;
15+
16+
cbSuccess?: (key: string) => void;
17+
cbFail?: () => void;
18+
cbTimeout?: () => void;
1319
}
1420
export const NewCaptcha = ({
1521
el,
1622
instance,
1723
siteKey,
1824
maxDots = 5,
1925
maxFailCount = 3,
26+
2027
inputName = "captcha",
28+
cbSuccess,
29+
cbFail,
30+
cbTimeout,
2131
}: NewCaptchaProps) =>
2232
ReactDOM.createRoot(el).render(
2333
<React.StrictMode>
@@ -27,8 +37,41 @@ export const NewCaptcha = ({
2737
maxDots={maxDots}
2838
maxFailCount={maxFailCount}
2939
inputName={inputName}
40+
cbSuccess={cbSuccess}
41+
cbFail={cbFail}
42+
cbTimeout={cbTimeout}
3043
/>
3144
</React.StrictMode>,
3245
);
3346

34-
export default NewCaptcha;
47+
// Classic captcha wrapper
48+
(window as any).nyacap = {
49+
render: (
50+
el: HTMLElement,
51+
options: {
52+
sitekey: string; // https://mini.nyacap.com/widget?sitekey=demo
53+
theme?: "light" | "dark";
54+
callback?: (key: string) => void;
55+
"expired-callback"?: () => void;
56+
"error-callback"?: () => void;
57+
},
58+
) => {
59+
const parsedSiteKey = new URL(options.sitekey);
60+
const instance = parsedSiteKey.origin;
61+
const siteKey = parsedSiteKey.searchParams.get("sitekey");
62+
63+
if (siteKey === null) {
64+
throw new Error("未定义 sitekey");
65+
}
66+
67+
NewCaptcha({
68+
el,
69+
instance,
70+
siteKey,
71+
inputName: undefined,
72+
cbSuccess: options.callback,
73+
cbFail: options["error-callback"],
74+
cbTimeout: options["expired-callback"],
75+
});
76+
},
77+
};

0 commit comments

Comments
 (0)