Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
df2fda61
authored
Jun 16, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: auth key
parent
bc250483
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
2 deletions
+34
-2
client/src/pages/number/index.tsx
+9
-2
client/src/utils/plugin/openai.ts
+25
-0
No files found.
client/src/pages/number/index.tsx
View file @
df2fda61
...
@@ -13,7 +13,8 @@ import { useQuery } from '@tanstack/react-query';
...
@@ -13,7 +13,8 @@ import { useQuery } from '@tanstack/react-query';
import
dynamic
from
'next/dynamic'
;
import
dynamic
from
'next/dynamic'
;
import
{
useSelectFile
}
from
'@/hooks/useSelectFile'
;
import
{
useSelectFile
}
from
'@/hooks/useSelectFile'
;
import
{
compressImg
}
from
'@/utils/file'
;
import
{
compressImg
}
from
'@/utils/file'
;
import
{
useCopyData
}
from
'@/utils/tools'
;
import
{
getErrText
,
useCopyData
}
from
'@/utils/tools'
;
import
{
authOpenAiKey
}
from
'@/utils/plugin/openai'
;
import
Loading
from
'@/components/Loading'
;
import
Loading
from
'@/components/Loading'
;
import
Avatar
from
'@/components/Avatar'
;
import
Avatar
from
'@/components/Avatar'
;
...
@@ -83,6 +84,7 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
...
@@ -83,6 +84,7 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
async
(
data
:
UserUpdateParams
)
=>
{
async
(
data
:
UserUpdateParams
)
=>
{
setLoading
(
true
);
setLoading
(
true
);
try
{
try
{
data
.
openaiKey
&&
(
await
authOpenAiKey
(
data
.
openaiKey
));
await
putUserInfo
({
await
putUserInfo
({
openaiKey
:
data
.
openaiKey
,
openaiKey
:
data
.
openaiKey
,
avatar
:
data
.
avatar
avatar
:
data
.
avatar
...
@@ -96,7 +98,12 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
...
@@ -96,7 +98,12 @@ const NumberSetting = ({ tableType }: { tableType: `${TableEnum}` }) => {
title
:
'更新成功'
,
title
:
'更新成功'
,
status
:
'success'
status
:
'success'
});
});
}
catch
(
error
)
{}
}
catch
(
error
)
{
toast
({
title
:
getErrText
(
error
),
status
:
'error'
});
}
setLoading
(
false
);
setLoading
(
false
);
},
},
[
reset
,
setLoading
,
toast
,
updateUserInfo
]
[
reset
,
setLoading
,
toast
,
updateUserInfo
]
...
...
client/src/utils/plugin/openai.ts
View file @
df2fda61
...
@@ -4,6 +4,8 @@ import { ChatRoleEnum } from '@/constants/chat';
...
@@ -4,6 +4,8 @@ import { ChatRoleEnum } from '@/constants/chat';
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
import
Graphemer
from
'graphemer'
;
import
Graphemer
from
'graphemer'
;
import
axios
from
'axios'
;
import
dayjs
from
'dayjs'
;
const
textDecoder
=
new
TextDecoder
();
const
textDecoder
=
new
TextDecoder
();
const
graphemer
=
new
Graphemer
();
const
graphemer
=
new
Graphemer
();
...
@@ -175,3 +177,26 @@ export const openAiSliceTextByToken = ({
...
@@ -175,3 +177,26 @@ export const openAiSliceTextByToken = ({
const
decoder
=
new
TextDecoder
();
const
decoder
=
new
TextDecoder
();
return
decoder
.
decode
(
enc
.
decode
(
encodeText
.
slice
(
0
,
length
)));
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'
);
});
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment