* feishu app release (#85) * Revert "lafAccount add pat & re request when token invalid (#76)" (#77) This reverts commit 83d85dfe37adcaef4833385ea52ee79fd84720be. * perf: workflow ux * system config * feat: feishu app release * chore: sovle the conflicts files; fix the feishu entry * fix: rename Feishu interface to FeishuType * fix: fix type problem in app.ts * fix: type problem * fix: style problem --------- Co-authored-by: Archer <545436317@qq.com> * perf: publish channel code * change system variable position (#94) * perf: workflow context * perf: variable select * hide publish * perf: simple edit auto refresh * perf: simple edit data refresh * fix: target handle --------- Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com> Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import { useState, useMemo, useCallback } from 'react';
|
|
import { sendAuthCode } from '@/web/support/user/api';
|
|
import { UserAuthTypeEnum } from '@fastgpt/global/support/user/auth/constants';
|
|
import { useToast } from '@fastgpt/web/hooks/useToast';
|
|
import { getErrText } from '@fastgpt/global/common/error/utils';
|
|
import { useTranslation } from 'next-i18next';
|
|
import { useSystemStore } from '@/web/common/system/useSystemStore';
|
|
|
|
let timer: any;
|
|
|
|
export const useSendCode = () => {
|
|
const { t } = useTranslation();
|
|
const { toast } = useToast();
|
|
const { feConfigs } = useSystemStore();
|
|
const [codeSending, setCodeSending] = useState(false);
|
|
const [codeCountDown, setCodeCountDown] = useState(0);
|
|
const sendCodeText = useMemo(() => {
|
|
if (codeSending) return t('support.user.auth.Sending Code');
|
|
if (codeCountDown >= 10) {
|
|
return `${codeCountDown}s后重新获取`;
|
|
}
|
|
if (codeCountDown > 0) {
|
|
return `0${codeCountDown}s后重新获取`;
|
|
}
|
|
return '获取验证码';
|
|
}, [codeCountDown, codeSending, t]);
|
|
|
|
const sendCode = useCallback(
|
|
async ({ username, type }: { username: string; type: `${UserAuthTypeEnum}` }) => {
|
|
if (codeCountDown > 0) return;
|
|
setCodeSending(true);
|
|
try {
|
|
await sendAuthCode({
|
|
username,
|
|
type,
|
|
googleToken: await getClientToken(feConfigs.googleClientVerKey)
|
|
});
|
|
setCodeCountDown(60);
|
|
timer = setInterval(() => {
|
|
setCodeCountDown((val) => {
|
|
if (val <= 0) {
|
|
clearInterval(timer);
|
|
}
|
|
return val - 1;
|
|
});
|
|
}, 1000);
|
|
toast({
|
|
title: '验证码已发送',
|
|
status: 'success',
|
|
position: 'top'
|
|
});
|
|
} catch (error: any) {
|
|
toast({
|
|
title: getErrText(error, '验证码发送异常'),
|
|
status: 'error'
|
|
});
|
|
}
|
|
setCodeSending(false);
|
|
},
|
|
[codeCountDown, feConfigs?.googleClientVerKey, toast]
|
|
);
|
|
|
|
return {
|
|
codeSending,
|
|
sendCode,
|
|
sendCodeText,
|
|
codeCountDown
|
|
};
|
|
};
|
|
|
|
export function getClientToken(googleClientVerKey?: string) {
|
|
if (!googleClientVerKey || typeof window.grecaptcha === 'undefined' || !window.grecaptcha?.ready)
|
|
return '';
|
|
return new Promise<string>((resolve, reject) => {
|
|
window.grecaptcha.ready(async () => {
|
|
try {
|
|
const token = await window.grecaptcha.execute(googleClientVerKey, {
|
|
action: 'submit'
|
|
});
|
|
resolve(token);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|