From 9b18a464565bbfe08a0d9d184044c66c211dfa98 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Fri, 7 Apr 2023 20:58:41 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B4=A6=E5=8F=B7api=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/user/update.ts | 5 +- src/pages/number/setting.tsx | 126 +++++++---------------------------- src/service/models/user.ts | 4 ++ src/service/utils/openai.ts | 4 +- src/types/mongoSchema.d.ts | 2 +- src/types/user.d.ts | 10 +-- 6 files changed, 35 insertions(+), 116 deletions(-) diff --git a/src/pages/api/user/update.ts b/src/pages/api/user/update.ts index 7f1320948..e902a2386 100644 --- a/src/pages/api/user/update.ts +++ b/src/pages/api/user/update.ts @@ -9,7 +9,7 @@ import { UserUpdateParams } from '@/types/user'; /* 更新一些基本信息 */ export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { - const { accounts } = req.body as UserUpdateParams; + const { openaiKey } = req.body as UserUpdateParams; const { authorization } = req.headers; if (!authorization) { @@ -26,8 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< _id: userId }, { - // 限定字段 - ...(accounts ? { accounts } : {}) + openaiKey } ); diff --git a/src/pages/number/setting.tsx b/src/pages/number/setting.tsx index ccf4000b4..dfe556b59 100644 --- a/src/pages/number/setting.tsx +++ b/src/pages/number/setting.tsx @@ -1,22 +1,6 @@ import React, { useCallback, useState } from 'react'; -import { - Card, - Box, - Flex, - Button, - Table, - Thead, - Tbody, - Tr, - Th, - Td, - TableContainer, - Select, - Input, - IconButton -} from '@chakra-ui/react'; -import { DeleteIcon } from '@chakra-ui/icons'; -import { useForm, useFieldArray } from 'react-hook-form'; +import { Card, Box, Flex, Button, Input } from '@chakra-ui/react'; +import { useForm } from 'react-hook-form'; import { UserUpdateParams } from '@/types/user'; import { putUserInfo } from '@/api/user'; import { useToast } from '@/hooks/useToast'; @@ -34,22 +18,15 @@ const PayModal = dynamic(() => import('./components/PayModal')); const NumberSetting = () => { const { userInfo, updateUserInfo, initUserInfo } = useUserStore(); const { setLoading } = useGlobalStore(); - const { register, handleSubmit, control } = useForm({ + const { register, handleSubmit } = useForm({ defaultValues: userInfo as UserType }); const [showPay, setShowPay] = useState(false); const { toast } = useToast(); - const { - fields: accounts, - append: appendAccount, - remove: removeAccount - } = useFieldArray({ - control, - name: 'accounts' - }); const onclickSave = useCallback( async (data: UserUpdateParams) => { + if (data.openaiKey === userInfo?.openaiKey) return; setLoading(true); try { await putUserInfo(data); @@ -61,7 +38,7 @@ const NumberSetting = () => { } catch (error) {} setLoading(false); }, - [setLoading, toast, updateUserInfo] + [setLoading, toast, updateUserInfo, userInfo?.openaiKey] ); useQuery(['init'], initUserInfo); @@ -73,12 +50,10 @@ const NumberSetting = () => { 账号信息 - - - 邮箱: - {userInfo?.email} - - + + 邮箱: + {userInfo?.email} + 余额: @@ -93,76 +68,23 @@ const NumberSetting = () => { 如果填写了自己的 openai 账号,将不会计费 - - {/* 账号信息 */} - - - - 关联账号 - - - {accounts.length === 0 && ( - - )} - - + + openaiKey: + { + if (e.keyCode === 13) { + handleSubmit(onclickSave)(); + } + }} + > - - - - - - - - - - - {accounts.map((item, i) => ( - - - - - - ))} - -
账号类型
- - - - - } - colorScheme={'red'} - onClick={() => { - removeAccount(i); - handleSubmit(onclickSave)(); - }} - /> -
-
+ {/* 充值记录 */} {/* 账单表 */} diff --git a/src/service/models/user.ts b/src/service/models/user.ts index 976e9cab8..70351e3df 100644 --- a/src/service/models/user.ts +++ b/src/service/models/user.ts @@ -19,6 +19,10 @@ const UserSchema = new Schema({ type: Number, default: 0.5 * PRICE_SCALE }, + openaiKey: { + type: String, + default: '' + }, accounts: [ { type: { diff --git a/src/service/utils/openai.ts b/src/service/utils/openai.ts index c876f21c3..c15478778 100644 --- a/src/service/utils/openai.ts +++ b/src/service/utils/openai.ts @@ -12,7 +12,7 @@ import { pushGenerateVectorBill } from '../events/pushBill'; export const getUserApiOpenai = async (userId: string) => { const user = await User.findById(userId); - const userApiKey = user?.accounts?.find((item: any) => item.type === 'openai')?.value; + const userApiKey = user?.openaiKey; if (!userApiKey) { return Promise.reject('缺少ApiKey, 无法请求'); @@ -35,7 +35,7 @@ export const getOpenApiKey = async (userId: string) => { }); } - const userApiKey = user?.accounts?.find((item: any) => item.type === 'openai')?.value; + const userApiKey = user?.openaiKey; // 有自己的key if (userApiKey) { diff --git a/src/types/mongoSchema.d.ts b/src/types/mongoSchema.d.ts index 174583e10..1dff08734 100644 --- a/src/types/mongoSchema.d.ts +++ b/src/types/mongoSchema.d.ts @@ -9,7 +9,7 @@ export interface UserModelSchema { email: string; password: string; balance: number; - accounts: { type: 'openai'; value: string }[]; + openaiKey: string; createTime: number; } diff --git a/src/types/user.d.ts b/src/types/user.d.ts index 7a8021942..9684e9c81 100644 --- a/src/types/user.d.ts +++ b/src/types/user.d.ts @@ -6,19 +6,13 @@ export enum UserNumberEnum { export interface UserType { _id: string; email: string; - accounts: { - type: string; - value: string; - }[]; + openaiKey: string; balance: number; } export interface UserUpdateParams { balance?: number; - accounts?: { - type: string; - value: string; - }[]; + openaiKey: string; } export interface UserBillType {