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,
mustPay = false
}: { 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
const chatAPI = getOpenAIApi(apiKey);
@@ -68,7 +73,7 @@ export async function openaiEmbedding({
});
pushGenerateVectorBill({
isPay: mustPay,
isPay: !userOpenAiKey,
userId,
text: input.join(''),
tokenLen: result.tokenLen

View File

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

View File

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