mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-29 01:40:51 +00:00
feat: auth key
This commit is contained in:
@@ -4,6 +4,8 @@ import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
import Graphemer from 'graphemer';
|
||||
import axios from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
const graphemer = new Graphemer();
|
||||
@@ -175,3 +177,26 @@ export const openAiSliceTextByToken = ({
|
||||
const decoder = new TextDecoder();
|
||||
return decoder.decode(enc.decode(encodeText.slice(0, length)));
|
||||
};
|
||||
|
||||
export const authOpenAiKey = async (key: string) => {
|
||||
return axios
|
||||
.get('https://ccdbwscohpmu.cloud.sealos.io/openai/v1/dashboard/billing/subscription', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${key}`
|
||||
}
|
||||
})
|
||||
.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 已过期');
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
return Promise.reject(err?.response?.data?.error || 'OpenAI 账号无效,请重试或更换 key');
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user