perf: share link

This commit is contained in:
archer
2023-07-28 09:38:50 +08:00
parent 36b234c4fd
commit f65a72821b
22 changed files with 171 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { Chat, App, connectToDatabase, Collection, ShareChat } from '@/service/mongo';
import { Chat, App, connectToDatabase, Collection, OutLink } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { authApp } from '@/service/utils/auth';
@@ -35,7 +35,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
});
// 删除分享链接
await ShareChat.deleteMany({
await OutLink.deleteMany({
appId
});

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat } from '@/service/mongo';
import { connectToDatabase, OutLink } from '@/service/mongo';
import { authApp, authUser } from '@/service/utils/auth';
import type { ShareChatEditType } from '@/types/app';
import { customAlphabet } from 'nanoid';
@@ -23,7 +23,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
const shareId = nanoid();
await ShareChat.create({
await OutLink.create({
shareId,
userId,
appId,

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat } from '@/service/mongo';
import { connectToDatabase, OutLink } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
/* delete a shareChat by shareChatId */
@@ -14,7 +14,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const { userId } = await authUser({ req, authToken: true });
await ShareChat.findOneAndRemove({
await OutLink.findOneAndRemove({
_id: id,
userId
});

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat, User } from '@/service/mongo';
import { connectToDatabase, OutLink, User } from '@/service/mongo';
import type { InitShareChatResponse } from '@/api/response/chat';
import { authApp } from '@/service/utils/auth';
import { HUMAN_ICON } from '@/constants/chat';
@@ -20,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
// get shareChat
const shareChat = await ShareChat.findOne({ shareId });
const shareChat = await OutLink.findOne({ shareId });
if (!shareChat) {
return jsonRes(res, {

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat } from '@/service/mongo';
import { connectToDatabase, OutLink } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { hashPassword } from '@/service/utils/tools';
@@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const { userId } = await authUser({ req, authToken: true });
const data = await ShareChat.find({
const data = await OutLink.find({
appId,
userId
}).sort({

View File

@@ -198,7 +198,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
appId,
userId,
source: authType === 'apikey' ? BillSourceEnum.api : BillSourceEnum.fastgpt,
response: responseData
response: responseData,
shareId
});
} catch (err: any) {
if (stream) {

View File

@@ -261,12 +261,12 @@ const Settings = ({ appId }: { appId: string }) => {
router.replace({
query: {
appId,
currentTab: 'share'
currentTab: 'outLink'
}
});
}}
>
</Button>
<Button
size={['sm', 'md']}

View File

@@ -14,7 +14,8 @@ import {
ModalFooter,
ModalBody,
FormControl,
Input
Input,
useTheme
} from '@chakra-ui/react';
import { QuestionOutlineIcon } from '@chakra-ui/icons';
import MyIcon from '@/components/Icon';
@@ -30,6 +31,7 @@ import { useRequest } from '@/hooks/useRequest';
import { formatPrice } from '@/utils/user';
import MyTooltip from '@/components/MyTooltip';
import MyModal from '@/components/MyModal';
import MyRadio from '@/components/Radio';
const Share = ({ appId }: { appId: string }) => {
const { Loading, setIsLoading } = useLoading();
@@ -49,8 +51,6 @@ const Share = ({ appId }: { appId: string }) => {
defaultValues: defaultShareChat
});
const [refresh, setRefresh] = useState(false);
const {
isFetching,
data: shareChatList = [],
@@ -77,7 +77,7 @@ const Share = ({ appId }: { appId: string }) => {
<Box position={'relative'} pt={[0, 5, 8]} px={[5, 8]} minH={'50vh'}>
<Flex justifyContent={'space-between'}>
<Box fontWeight={'bold'}>
<MyTooltip
forceShow
label="可以直接分享该模型给其他用户去进行对话对方无需登录即可直接进行对话。注意这个功能会消耗你账号的tokens。请保管好链接和密码。"
@@ -97,7 +97,7 @@ const Share = ({ appId }: { appId: string }) => {
: {})}
onClick={onOpenCreateShareChat}
>
</Button>
</Flex>
<TableContainer mt={3}>
@@ -118,7 +118,7 @@ const Share = ({ appId }: { appId: string }) => {
<Td>{item.lastTime ? formatTimeToChatTime(item.lastTime) : '未使用'}</Td>
<Td>
<Flex>
<MyTooltip label={'复制分享地址'}>
<MyTooltip label={'复制分享链接'}>
<MyIcon
mr={3}
name="copy"
@@ -127,7 +127,7 @@ const Share = ({ appId }: { appId: string }) => {
_hover={{ color: 'myBlue.600' }}
onClick={() => {
const url = `${location.origin}/chat/share?shareId=${item.shareId}`;
copyData(url, '已复制分享地址');
copyData(url, '已复制分享链接');
}}
/>
</MyTooltip>
@@ -205,4 +205,47 @@ const Share = ({ appId }: { appId: string }) => {
);
};
export default Share;
enum LinkTypeEnum {
share = 'share',
iframe = 'iframe'
}
const OutLink = ({ appId }: { appId: string }) => {
const theme = useTheme();
const [linkType, setLinkType] = useState<`${LinkTypeEnum}`>(LinkTypeEnum.share);
return (
<Box pt={[1, 5]}>
<Box fontWeight={'bold'} fontSize={['md', 'xl']} mb={2} px={[4, 8]}>
使
</Box>
<Box pb={[5, 7]} px={[4, 8]} borderBottom={theme.borders.base}>
<MyRadio
gridTemplateColumns={['repeat(1,1fr)', 'repeat(2, 350px)']}
iconSize={'20px'}
list={[
{
icon: 'outlink_share',
title: '免登录窗口',
desc: '分享链接给其他用户,无需登录即可直接进行使用',
value: LinkTypeEnum.share
}
// {
// icon: 'outlink_iframe',
// title: '网页嵌入',
// desc: '嵌入到已有网页中,右下角会生成对话按键',
// value: LinkTypeEnum.iframe
// }
]}
value={linkType}
onChange={(e) => setLinkType(e as `${LinkTypeEnum}`)}
/>
</Box>
{linkType === LinkTypeEnum.share && <Share appId={appId} />}
</Box>
);
};
export default OutLink;

View File

@@ -20,7 +20,7 @@ const AdEdit = dynamic(() => import('./components/AdEdit'), {
ssr: false,
loading: () => <Loading />
});
const Share = dynamic(() => import('./components/Share'), {
const OutLink = dynamic(() => import('./components/OutLink'), {
ssr: false
});
const API = dynamic(() => import('./components/API'), {
@@ -30,7 +30,7 @@ const API = dynamic(() => import('./components/API'), {
enum TabEnum {
'basicEdit' = 'basicEdit',
'adEdit' = 'adEdit',
'share' = 'share',
'outLink' = 'outLink',
'API' = 'API'
}
@@ -57,7 +57,7 @@ const AppDetail = ({ currentTab }: { currentTab: `${TabEnum}` }) => {
() => [
{ label: '简易配置', id: TabEnum.basicEdit, icon: 'overviewLight' },
{ label: '高级编排', id: TabEnum.adEdit, icon: 'settingLight' },
{ label: '链接分享', id: TabEnum.share, icon: 'shareLight' },
{ label: '外部使用', id: TabEnum.outLink, icon: 'shareLight' },
{ label: 'API访问', id: TabEnum.API, icon: 'apiLight' },
{ label: '立即对话', id: 'startChat', icon: 'chat' }
],
@@ -178,7 +178,7 @@ const AppDetail = ({ currentTab }: { currentTab: `${TabEnum}` }) => {
/>
)}
{currentTab === TabEnum.API && <API appId={appId} />}
{currentTab === TabEnum.share && <Share appId={appId} />}
{currentTab === TabEnum.outLink && <OutLink appId={appId} />}
</Box>
</Flex>
</PageContainer>

View File

@@ -20,7 +20,7 @@ import ChatHeader from './components/ChatHeader';
import ChatHistorySlider from './components/ChatHistorySlider';
import { serviceSideProps } from '@/utils/i18n';
const ShareChat = ({ shareId, chatId }: { shareId: string; chatId: string }) => {
const OutLink = ({ shareId, chatId }: { shareId: string; chatId: string }) => {
const router = useRouter();
const { toast } = useToast();
const { isOpen: isOpenSlider, onClose: onCloseSlider, onOpen: onOpenSlider } = useDisclosure();
@@ -250,4 +250,4 @@ export async function getServerSideProps(context: any) {
};
}
export default ShareChat;
export default OutLink;