-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d122eb3
commit a406973
Showing
13 changed files
with
324 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
frontend/desktop/src/components/signin/Captcha/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import request from '@/services/request'; | ||
import { useConfigStore } from '@/stores/config'; | ||
import { Box, Button, ButtonProps, Link, LinkProps } from '@chakra-ui/react'; | ||
import { delay } from 'lodash'; | ||
import React, { | ||
ReactElement, | ||
forwardRef, | ||
useEffect, | ||
useId, | ||
useImperativeHandle, | ||
useRef, | ||
useState | ||
} from 'react'; | ||
import { v4 } from 'uuid'; | ||
export type TCaptchaInstance = { | ||
getToken: () => Promise<string>; | ||
reset: () => void; | ||
}; | ||
|
||
const HiddenCaptchaComponent = forwardRef(function HiddenCaptchaComponent(props, ref) { | ||
const captchaElementRef = useRef<HTMLDivElement>(null); | ||
const captchaInstanceRef = useRef(null); | ||
const tokenRef = useRef(''); | ||
const buttonRef = useRef<HTMLButtonElement>(null); | ||
const { authConfig } = useConfigStore(); | ||
useImperativeHandle<any, TCaptchaInstance>( | ||
ref, | ||
() => { | ||
return { | ||
getToken: async () => { | ||
buttonRef.current?.click(); | ||
await new Promise((res) => { | ||
setTimeout(res, 1000); | ||
}); | ||
const token = tokenRef.current; | ||
return token; | ||
}, | ||
reset() { | ||
tokenRef.current = ''; | ||
} | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
const [buttonId] = useState('captcha_button_pop'); | ||
const [captchaId] = useState('captcha_' + v4().slice(0, 8)); | ||
|
||
// @ts-ignore | ||
const getInstance = (instance) => { | ||
captchaInstanceRef.current = instance; | ||
}; | ||
|
||
const onBizResultCallback = () => {}; | ||
useEffect(() => { | ||
const initAliyunCaptchaOptions = { | ||
SceneId: authConfig?.captcha.ali.sceneId, | ||
prefix: authConfig?.captcha.ali.prefix, | ||
mode: 'popup', | ||
element: '#' + captchaId, | ||
button: '#' + buttonId, | ||
async captchaVerifyCallback(captchaToken: string) { | ||
try { | ||
tokenRef.current = captchaToken; | ||
return { | ||
captchaResult: true | ||
}; | ||
} catch (err) { | ||
tokenRef.current = ''; | ||
return { | ||
captchaResult: false | ||
}; | ||
} | ||
}, | ||
onBizResultCallback: onBizResultCallback, | ||
getInstance, | ||
slideStyle: { | ||
width: 360, | ||
height: 40 | ||
}, | ||
immediate: false, | ||
language: 'cn', | ||
region: 'cn' | ||
}; | ||
|
||
// @ts-ignore | ||
window.initAliyunCaptcha(initAliyunCaptchaOptions); | ||
|
||
return () => { | ||
captchaInstanceRef.current = null; | ||
}; | ||
}, []); | ||
return ( | ||
<> | ||
<Button | ||
ref={buttonRef} | ||
variant={'unstyled'} | ||
hidden | ||
id={buttonId} | ||
{...(props as ButtonProps)} | ||
/> | ||
<div ref={captchaElementRef} id={captchaId} /> | ||
</> | ||
); | ||
}); | ||
|
||
export { HiddenCaptchaComponent }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.