2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-05-22 20:57:55 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-06-27 00:50:14 +08:00
|
|
|
import {Button, Col, Input, Modal, Row} from "antd";
|
2021-05-22 20:57:55 +08:00
|
|
|
import React from "react";
|
|
|
|
import i18next from "i18next";
|
|
|
|
import * as UserBackend from "../backend/UserBackend";
|
2021-11-28 20:18:03 +08:00
|
|
|
import {SafetyOutlined} from "@ant-design/icons";
|
2022-06-18 16:00:31 +08:00
|
|
|
import {authConfig} from "../auth/Auth";
|
|
|
|
import { CaptchaWidget } from "./CaptchaWidget";
|
2021-05-22 20:57:55 +08:00
|
|
|
|
2021-06-27 00:50:14 +08:00
|
|
|
const { Search } = Input;
|
|
|
|
|
2021-05-22 20:57:55 +08:00
|
|
|
export const CountDownInput = (props) => {
|
2021-11-28 20:57:14 +08:00
|
|
|
const {disabled, textBefore, onChange, onButtonClickArgs} = props;
|
2021-05-22 20:57:55 +08:00
|
|
|
const [visible, setVisible] = React.useState(false);
|
|
|
|
const [key, setKey] = React.useState("");
|
|
|
|
const [captchaImg, setCaptchaImg] = React.useState("");
|
|
|
|
const [checkType, setCheckType] = React.useState("");
|
|
|
|
const [checkId, setCheckId] = React.useState("");
|
2021-12-23 19:41:26 +08:00
|
|
|
const [buttonLeftTime, setButtonLeftTime] = React.useState(0);
|
|
|
|
const [buttonLoading, setButtonLoading] = React.useState(false);
|
2022-06-18 16:00:31 +08:00
|
|
|
const [buttonDisabled, setButtonDisabled] = React.useState(true);
|
|
|
|
const [clientId, setClientId] = React.useState("");
|
2022-06-29 11:31:32 +08:00
|
|
|
const [subType, setSubType] = React.useState("");
|
|
|
|
const [clientId2, setClientId2] = React.useState("");
|
|
|
|
const [clientSecret2, setClientSecret2] = React.useState("");
|
2021-05-22 20:57:55 +08:00
|
|
|
|
2021-12-23 19:41:26 +08:00
|
|
|
const handleCountDown = (leftTime = 60) => {
|
|
|
|
let leftTimeSecond = leftTime
|
|
|
|
setButtonLeftTime(leftTimeSecond)
|
|
|
|
const countDown = () => {
|
|
|
|
leftTimeSecond--;
|
|
|
|
setButtonLeftTime(leftTimeSecond)
|
|
|
|
if (leftTimeSecond === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(countDown, 1000);
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
2021-12-23 19:41:26 +08:00
|
|
|
setTimeout(countDown, 1000);
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
setVisible(false);
|
2021-12-23 19:41:26 +08:00
|
|
|
setButtonLoading(true)
|
2021-11-28 20:57:14 +08:00
|
|
|
UserBackend.sendCode(checkType, checkId, key, ...onButtonClickArgs).then(res => {
|
2021-06-27 01:34:25 +08:00
|
|
|
setKey("");
|
2021-12-23 19:41:26 +08:00
|
|
|
setButtonLoading(false)
|
2021-05-22 20:57:55 +08:00
|
|
|
if (res) {
|
2021-12-23 19:41:26 +08:00
|
|
|
handleCountDown(60);
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
setVisible(false);
|
2021-06-27 01:34:25 +08:00
|
|
|
setKey("");
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
const loadCaptcha = () => {
|
|
|
|
UserBackend.getCaptcha("admin", authConfig.appName, false).then(res => {
|
2021-05-22 20:57:55 +08:00
|
|
|
if (res.type === "none") {
|
2022-06-22 22:22:40 +08:00
|
|
|
UserBackend.sendCode("none", "", "", ...onButtonClickArgs).then(res => {
|
|
|
|
if (res) {
|
|
|
|
handleCountDown(60);
|
|
|
|
}
|
|
|
|
});
|
2022-06-18 16:00:31 +08:00
|
|
|
} else if (res.type === "Default") {
|
2021-05-22 20:57:55 +08:00
|
|
|
setCheckId(res.captchaId);
|
|
|
|
setCaptchaImg(res.captchaImage);
|
2022-06-18 16:00:31 +08:00
|
|
|
setCheckType("Default");
|
|
|
|
setVisible(true);
|
2022-06-29 11:31:32 +08:00
|
|
|
} else {
|
2022-06-18 16:00:31 +08:00
|
|
|
setCheckType(res.type);
|
|
|
|
setClientId(res.clientId);
|
|
|
|
setCheckId(res.clientSecret);
|
2021-05-22 20:57:55 +08:00
|
|
|
setVisible(true);
|
2022-06-29 11:31:32 +08:00
|
|
|
setSubType(res.subType);
|
|
|
|
setClientId2(res.clientId2);
|
|
|
|
setClientSecret2(res.clientSecret2);
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderCaptcha = () => {
|
|
|
|
return (
|
|
|
|
<Col>
|
|
|
|
<Row
|
|
|
|
style={{
|
|
|
|
backgroundImage: `url('data:image/png;base64,${captchaImg}')`,
|
|
|
|
backgroundRepeat: "no-repeat",
|
|
|
|
height: "80px",
|
|
|
|
width: "200px",
|
|
|
|
borderRadius: "3px",
|
|
|
|
border: "1px solid #ccc",
|
|
|
|
marginBottom: 10
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Row>
|
2021-12-23 19:41:26 +08:00
|
|
|
<Input autoFocus value={key} prefix={<SafetyOutlined/>} placeholder={i18next.t("general:Captcha")} onPressEnter={handleOk} onChange={e => setKey(e.target.value)}/>
|
2021-05-22 20:57:55 +08:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
const onSubmit = (token) => {
|
|
|
|
setButtonDisabled(false);
|
|
|
|
setKey(token);
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:57:55 +08:00
|
|
|
const renderCheck = () => {
|
2022-06-18 16:00:31 +08:00
|
|
|
if (checkType === "Default") {
|
|
|
|
return renderCaptcha();
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<CaptchaWidget
|
|
|
|
captchaType={checkType}
|
2022-06-29 11:31:32 +08:00
|
|
|
subType={subType}
|
2022-06-18 16:00:31 +08:00
|
|
|
siteKey={clientId}
|
2022-06-29 11:31:32 +08:00
|
|
|
clientSecret={checkId}
|
2022-06-18 16:00:31 +08:00
|
|
|
onChange={onSubmit}
|
2022-06-29 11:31:32 +08:00
|
|
|
clientId2={clientId2}
|
|
|
|
clientSecret2={clientSecret2}
|
2022-06-18 16:00:31 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-11-28 20:18:03 +08:00
|
|
|
<React.Fragment>
|
2021-06-27 00:50:14 +08:00
|
|
|
<Search
|
|
|
|
addonBefore={textBefore}
|
|
|
|
disabled={disabled}
|
2021-12-23 19:41:26 +08:00
|
|
|
prefix={<SafetyOutlined/>}
|
2021-11-28 20:57:14 +08:00
|
|
|
placeholder={i18next.t("code:Enter your code")}
|
2021-06-27 00:50:14 +08:00
|
|
|
onChange={e => onChange(e.target.value)}
|
|
|
|
enterButton={
|
2021-12-23 19:41:26 +08:00
|
|
|
<Button style={{fontSize: 14}} type={"primary"} disabled={disabled || buttonLeftTime > 0} loading={buttonLoading}>
|
|
|
|
{buttonLeftTime > 0 ? `${buttonLeftTime} s` : buttonLoading ? i18next.t("code:Sending Code") : i18next.t("code:Send Code")}
|
2021-06-27 00:50:14 +08:00
|
|
|
</Button>
|
|
|
|
}
|
2022-06-18 16:00:31 +08:00
|
|
|
onSearch={loadCaptcha}
|
2021-06-27 00:50:14 +08:00
|
|
|
/>
|
|
|
|
<Modal
|
2021-06-27 01:34:25 +08:00
|
|
|
closable={false}
|
|
|
|
maskClosable={false}
|
2021-06-27 01:49:52 +08:00
|
|
|
destroyOnClose={true}
|
2021-06-27 01:34:25 +08:00
|
|
|
title={i18next.t("general:Captcha")}
|
2021-06-27 00:50:14 +08:00
|
|
|
visible={visible}
|
2021-06-27 01:34:25 +08:00
|
|
|
okText={i18next.t("user:OK")}
|
|
|
|
cancelText={i18next.t("user:Cancel")}
|
2021-06-27 00:50:14 +08:00
|
|
|
onOk={handleOk}
|
2021-11-28 14:11:09 +08:00
|
|
|
onCancel={handleCancel}
|
2022-06-18 16:00:31 +08:00
|
|
|
okButtonProps={{disabled: key.length !== 5 && buttonDisabled}}
|
|
|
|
width={348}
|
2021-06-27 00:50:14 +08:00
|
|
|
>
|
2021-11-28 14:11:09 +08:00
|
|
|
{
|
|
|
|
renderCheck()
|
|
|
|
}
|
2021-06-27 00:50:14 +08:00
|
|
|
</Modal>
|
2021-11-28 20:18:03 +08:00
|
|
|
</React.Fragment>
|
2021-05-22 20:57:55 +08:00
|
|
|
);
|
2021-06-27 00:50:14 +08:00
|
|
|
}
|