2022-06-18 16:00:31 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-07-10 15:45:55 +08:00
|
|
|
import React, {useEffect} from "react";
|
2022-06-18 16:00:31 +08:00
|
|
|
|
2022-07-10 15:45:55 +08:00
|
|
|
export const CaptchaWidget = ({captchaType, subType, siteKey, clientSecret, onChange, clientId2, clientSecret2}) => {
|
2022-06-18 16:00:31 +08:00
|
|
|
const loadScript = (src) => {
|
|
|
|
var tag = document.createElement("script");
|
|
|
|
tag.async = false;
|
|
|
|
tag.src = src;
|
|
|
|
var body = document.getElementsByTagName("body")[0];
|
|
|
|
body.appendChild(tag);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
switch (captchaType) {
|
2022-08-04 20:55:04 +08:00
|
|
|
case "reCAPTCHA": {
|
2022-07-10 15:45:55 +08:00
|
|
|
const reTimer = setInterval(() => {
|
|
|
|
if (!window.grecaptcha) {
|
|
|
|
loadScript("https://recaptcha.net/recaptcha/api.js");
|
|
|
|
}
|
|
|
|
if (window.grecaptcha && window.grecaptcha.render) {
|
|
|
|
window.grecaptcha.render("captcha", {
|
|
|
|
sitekey: siteKey,
|
|
|
|
callback: onChange,
|
|
|
|
});
|
|
|
|
clearInterval(reTimer);
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
break;
|
2022-08-04 20:55:04 +08:00
|
|
|
}
|
|
|
|
case "hCaptcha": {
|
2022-07-10 15:45:55 +08:00
|
|
|
const hTimer = setInterval(() => {
|
|
|
|
if (!window.hcaptcha) {
|
|
|
|
loadScript("https://js.hcaptcha.com/1/api.js");
|
|
|
|
}
|
|
|
|
if (window.hcaptcha && window.hcaptcha.render) {
|
|
|
|
window.hcaptcha.render("captcha", {
|
|
|
|
sitekey: siteKey,
|
|
|
|
callback: onChange,
|
|
|
|
});
|
|
|
|
clearInterval(hTimer);
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
break;
|
2022-08-04 20:55:04 +08:00
|
|
|
}
|
|
|
|
case "Aliyun Captcha": {
|
2022-07-10 15:45:55 +08:00
|
|
|
const AWSCTimer = setInterval(() => {
|
|
|
|
if (!window.AWSC) {
|
|
|
|
loadScript("https://g.alicdn.com/AWSC/AWSC/awsc.js");
|
|
|
|
}
|
2022-06-29 11:31:32 +08:00
|
|
|
|
2022-07-10 15:45:55 +08:00
|
|
|
if (window.AWSC) {
|
|
|
|
if (clientSecret2 && clientSecret2 !== "***") {
|
|
|
|
window.AWSC.use(subType, function(state, module) {
|
|
|
|
module.init({
|
|
|
|
appkey: clientSecret2,
|
|
|
|
scene: clientId2,
|
|
|
|
renderTo: "captcha",
|
|
|
|
success: function(data) {
|
|
|
|
onChange(`SessionId=${data.sessionId}&AccessKeyId=${siteKey}&Scene=${clientId2}&AppKey=${clientSecret2}&Token=${data.token}&Sig=${data.sig}&RemoteIp=192.168.0.1`);
|
|
|
|
},
|
2022-06-29 11:31:32 +08:00
|
|
|
});
|
2022-07-10 15:45:55 +08:00
|
|
|
});
|
2022-06-29 11:31:32 +08:00
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
clearInterval(AWSCTimer);
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
break;
|
2022-08-04 20:55:04 +08:00
|
|
|
}
|
|
|
|
case "GEETEST": {
|
|
|
|
let getLock = false;
|
|
|
|
const gTimer = setInterval(() => {
|
|
|
|
if (!window.initGeetest4) {
|
|
|
|
loadScript("https://static.geetest.com/v4/gt4.js");
|
|
|
|
}
|
|
|
|
if (window.initGeetest4 && siteKey && !getLock) {
|
|
|
|
const captchaId = String(siteKey);
|
|
|
|
window.initGeetest4({
|
2022-08-06 23:36:20 +08:00
|
|
|
captchaId,
|
|
|
|
product: "float",
|
2022-08-04 20:55:04 +08:00
|
|
|
}, function(captchaObj) {
|
|
|
|
if (!getLock) {
|
|
|
|
captchaObj.appendTo("#captcha");
|
|
|
|
getLock = true;
|
|
|
|
}
|
|
|
|
captchaObj.onSuccess(function() {
|
|
|
|
const result = captchaObj.getValidate();
|
|
|
|
onChange(`lot_number=${result.lot_number}&captcha_output=${result.captcha_output}&pass_token=${result.pass_token}&gen_time=${result.gen_time}&captcha_id=${siteKey}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
clearInterval(gTimer);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
default:
|
|
|
|
break;
|
2022-06-18 16:00:31 +08:00
|
|
|
}
|
2022-06-29 11:31:32 +08:00
|
|
|
}, [captchaType, subType, siteKey, clientSecret, clientId2, clientSecret2]);
|
2022-06-18 16:00:31 +08:00
|
|
|
|
|
|
|
return <div id="captcha"></div>;
|
|
|
|
};
|