fix: apikey

This commit is contained in:
archer
2023-06-25 13:20:00 +08:00
parent cfb31afbd9
commit 426176db47
3 changed files with 22 additions and 16 deletions

View File

@@ -39,7 +39,12 @@ export async function openaiEmbedding({
input, input,
mustPay = false mustPay = false
}: { userId: string; mustPay?: boolean } & Props) { }: { userId: string; mustPay?: boolean } & Props) {
const apiKey = getSystemOpenAiKey(); const { userOpenAiKey, systemAuthKey } = await getApiKey({
model: 'gpt-3.5-turbo',
userId,
mustPay
});
const apiKey = userOpenAiKey || systemAuthKey;
// 获取 chatAPI // 获取 chatAPI
const chatAPI = getOpenAIApi(apiKey); const chatAPI = getOpenAIApi(apiKey);
@@ -68,7 +73,7 @@ export async function openaiEmbedding({
}); });
pushGenerateVectorBill({ pushGenerateVectorBill({
isPay: mustPay, isPay: !userOpenAiKey,
userId, userId,
text: input.join(''), text: input.join(''),
tokenLen: result.tokenLen tokenLen: result.tokenLen

View File

@@ -54,7 +54,6 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
{ label: '佣金', id: TableEnum.promotion, Component: <PromotionTable /> }, { label: '佣金', id: TableEnum.promotion, Component: <PromotionTable /> },
{ label: '通知', id: TableEnum.inform, Component: <InformTable /> } { label: '通知', id: TableEnum.inform, Component: <InformTable /> }
]); ]);
const [currentTab, setCurrentTab] = useState(tableType);
const router = useRouter(); const router = useRouter();
const { copyData } = useCopyData(); const { copyData } = useCopyData();
@@ -84,7 +83,14 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
async (data: UserUpdateParams) => { async (data: UserUpdateParams) => {
setLoading(true); setLoading(true);
try { try {
data.openaiKey && (await authOpenAiKey(data.openaiKey)); if (data.openaiKey) {
const text = await authOpenAiKey(data.openaiKey);
text &&
toast({
title: text,
status: 'warning'
});
}
await putUserInfo({ await putUserInfo({
openaiKey: data.openaiKey, openaiKey: data.openaiKey,
avatar: data.avatar avatar: data.avatar
@@ -95,7 +101,7 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
}); });
reset(data); reset(data);
toast({ toast({
title: '更新成功', title: '更新数据成功',
status: 'success' status: 'success'
}); });
} catch (error) { } catch (error) {
@@ -195,7 +201,7 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
<Box flex={'0 0 85px'}>openaiKey:</Box> <Box flex={'0 0 85px'}>openaiKey:</Box>
<Input <Input
{...register(`openaiKey`)} {...register(`openaiKey`)}
maxW={'300px'} maxW={'350px'}
placeholder={'openai账号。回车或失去焦点保存'} placeholder={'openai账号。回车或失去焦点保存'}
size={'sm'} size={'sm'}
onBlur={handleSubmit(onclickSave)} onBlur={handleSubmit(onclickSave)}
@@ -251,13 +257,13 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
m={'auto'} m={'auto'}
w={'200px'} w={'200px'}
list={tableList.current} list={tableList.current}
activeId={currentTab} activeId={tableType}
size={'sm'} size={'sm'}
onChange={(id: any) => setCurrentTab(id)} onChange={(id: any) => router.replace(`/number?type=${id}`)}
/> />
<Box minH={'300px'}> <Box minH={'300px'}>
{(() => { {(() => {
const item = tableList.current.find((item) => item.id === currentTab); const item = tableList.current.find((item) => item.id === tableType);
return item ? item.Component : null; return item ? item.Component : null;
})()} })()}

View File

@@ -111,16 +111,11 @@ export const authOpenAiKey = async (key: string) => {
}) })
.then((res) => { .then((res) => {
if (!res.data.access_until) { if (!res.data.access_until) {
return Promise.reject('OpenAI Key 无效,请重试或更换 key'); return Promise.resolve('OpenAI Key 可能无效');
}
const keyExpiredTime = dayjs(res.data.access_until * 1000);
const currentTime = dayjs();
if (keyExpiredTime.isBefore(currentTime)) {
return Promise.reject('OpenAI Key 已过期');
} }
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
return Promise.reject(err?.response?.data?.error || 'OpenAI 账号无效,请重试或更换 key'); return Promise.reject(err?.response?.data?.error?.message || 'OpenAI Key 可能无效');
}); });
}; };